1

I want to run several R files using make. I have the following problems:

  1. The output files are not saved in the corresponding folders. I tried using cd but it didn't work.
  2. I cannot create the pdf plots, the plots.R file doesn't run with my makefile. I only get:

    Rscript src/example.R
    [Finished in 0.7s]
    
  3. I would like to load the data in the plot file only using load("data.Rdata"). For that, I should set the directory first, and then run the code, right?

  4. Do I have to load all the libraries I will use in each R file?

Makefile

RDIR = ./src
TDATA = ./output/transform_data
PLOTDIR = ./output/figures

$(TDATA)/data.Rdata : $(RDIR)/example.R
    Rscript $<

$(PLOTDIR)/%.pdf : $(RDIR)/plots.R $(TDATA)/data.Rdata
    Rscript $<

Example:

dat <- data.frame(a = rnorm(20), b = rnorm(20))
save(dat, file = "output/transform_data/data.Rdata")

Plot:

load("output/tranform_data/data.Rdata")

pdf("output/figures/a.pdf")
print(plot(dat$a))
dev.off()

pdf("output/figures/b.pdf")
print(plot(dat$b))
dev.off()

Here the files of the example.

sdaza
  • 1,032
  • 13
  • 29
  • 1
    For those of us unfamiliar with R, could you explain how you would do this *without* Make, what commands you would use and what they would do? – Beta Jul 18 '16 at 18:44
  • (1 and 2) Possibly related to Q4. (3) If by "plot file" you really mean "Rdata file with data for a plot", then that should work, assuming your working directory is correct. (4) Yes; without it, the script should error out. Are you able to manually run `Rscript plots.R` and get the output you expect? – r2evans Jul 18 '16 at 20:39
  • @r2evans By plot file I meant a pdf file. The Rscript is working, my problem is with the makefile. So I should define a working directory first, I've always seen only relative paths in the examples I've checked on the web. – sdaza Jul 18 '16 at 20:56
  • So you're asking how to load *data* stored in the *pdf*? BTW: if `Rscript` is not being called in the current working directory, then you might need to manually set the working directory (perhaps using http://stackoverflow.com/a/16046056/3358272 to find where you think you should be) within each script. (I think the better method would be to have the working directory set before calling `Rscript`, perhaps `cd $(PLOTDIR) ; Rscript $<`.) – r2evans Jul 18 '16 at 21:36
  • This is just a toy example where I create a dataset, then using another file I want to create some plots and save them in a directory... So far I can create the dataset using make, but nothing happens with the plots. :( – sdaza Jul 18 '16 at 21:52
  • Here is the answer: http://stackoverflow.com/questions/26070162/running-makefile-but-the-last-file-is-skipped-r?rq=1 – sdaza Jul 18 '16 at 22:00

0 Answers0