Matthew J. Oldach
Matthew J. Oldach

Reputation: 656

drake readd function not working for plots

I'm trying to trouble shoot why Drake plots are not showing up with readd() - the rest of the pipeline seem's to have worked though.

Not sure if this is caused by minfi::densityPlot or some other reason; my thoughts are the later as it's also not working for the barplot function which is base R.

In the RMarkdown report I have readd(dplot1) etc. in the chunks but the output is NULL

This is the code I have in my R/setup.R file:

library(drake)
library(tidyverse)
library(magrittr)
library(minfi)
library(DNAmArray)
library(methylumi)
library(RColorBrewer)
library(minfiData)
pkgconfig::set_config("drake::strings_in_dots" = "literals") # New file API

# Your custom code is a bunch of functions.
make_beta <- function(rgSet){
        rgSet_betas = minfi::getBeta(rgSet)
}

make_filter <- function(rgSet){
        rgSet_filtered = DNAmArray::probeFiltering(rgSet)
}

This is my R/plan.R file:

# The workflow plan data frame outlines what you are going to do
plan <- drake_plan(
        baseDir = system.file("extdata", package = "minfiData"),
        targets = read.metharray.sheet(baseDir),
        rgSet = read.metharray.exp(targets = targets),
        mSetSq = preprocessQuantile(rgSet),
        detP = detectionP(rgSet),
        dplot1 = densityPlot(rgSet, sampGroups=targets$Sample_Group,main="Raw", legend=FALSE),
        dplot2 = densityPlot (getBeta (mSetSq), sampGroups=targets$Sample_Group, main="Normalized", legend=FALSE),
        pal = RColorBrewer::brewer.pal (8,"Dark2"),
        dplot3 = barplot (colMeans (detP[,1:6]), col=pal[ factor (targets$Sample_Group[1:6])], las=2, cex.names=0.8, ylab="Mean detection p-values"),
        report = rmarkdown::render(
                knitr_in("report.Rmd"),
                output_file = file_out("report.html"),

                quiet = TRUE
        )
)

After using make(plan) it looks like everything ran smoothly:

config <- drake_config(plan)
vis_drake_graph(config)

enter image description here

I am able to use loadd() to load the objects needed for one of these plots and then make the plots, like this:

loadd(rgSet)
loadd(targets)
densityPlot(rgSet, sampGroups=targets$Sample_Group,main="Raw", legend=FALSE)

But the readd() command doesn't work?

The output in the .html for dplot3 looks weird...

enter image description here

Upvotes: 1

Views: 133

Answers (1)

landau
landau

Reputation: 5841

Fortunately, this is expected behavior. drake targets are return values of commands, and so the value of dplot3 is supposed to be the return value of barplot(). The return value of barplot() is actually not a plot. The "Value" section of the help file (?barplot) explains the return value.

A numeric vector (or matrix, when beside = TRUE), say mp, giving the coordinates of all the bar midpoints drawn, useful for adding to the graph.

If beside is true, use colMeans(mp) for the midpoints of each group of bars, see example.

So what is going on? As with most base graphics functions, the plot from barplot() is actually a side effect. barplot() sends the plot to a graphics device and then returns something else to the user.

Have you considered ggplot2? The return value of ggplot() is actually a plot object, which is more intuitive. If you want to stick with base graphics, maybe you could save the plot to an output file.

plan <- drake_plan(
  ...,
  dplot3 = {
    pdf(file_out("dplot3.pdf"))
    barplot(...)
    dev.off()
  }
)

Upvotes: 1

Related Questions