benmarwick / rrtools

rrtools: Tools for Writing Reproducible Research in R
Other
671 stars 85 forks source link

[Question] How do I access an object stored in the package from the paper.RMD file? #46

Closed wolass closed 6 years ago

wolass commented 6 years ago

Hi I figured that the files are nicely saving in the directories (all scripts in R folder and figures where they are supposed to!) Thats great.

So next thing that occured to me is that I do not need to call every script per source() function in the paper.Rmd file. I only need to call the package here! That's great too!

My question is now can I call a variable that was created in the R/analysis.R script to be evaluated in the paper.Rnw file?

To illustrate R/analysis.R:

df <- data.frame(x=1,y=2)

analysis/paper/paper.Rmd

library(CREATED_PACKAGE)
paste0("There were", df$x, " events") 

The above code produces an error

I'm sure that I am missing an info how to properly build a package... any help here?

I really love this package and it has been super-helpful!!!!1 Please do not stop developing it!!!

wolass commented 6 years ago

I think I got this solved by using

devtools::load_all(".")

This is run isted or library() function in the analysis/paper/paper.Rmd File

benmarwick commented 6 years ago

That seems like a good solution, thanks for the update and kind words. I usually put all the R code in the Rmd document, or put long functions as regular package functions in the /R directory, and then use library() to make them available. So I'm quite interested to see your approach since I haven't tried that yet in my own work but I can see it might be useful. Can you share the URL to your repository where you're working on this?

wolass commented 6 years ago

Allright but its still messy :) Its a review paper That I am currently writing, and the search strategy is made more (or less) reproducible with the use of the RISmed package which allows for querying Pubmed.

https://github.com/wolass/refractoryANA

benmarwick commented 6 years ago

Thanks for sharing the URL to your review paper repo. I browsed with great interest! I see you put your R scripts in the /R directory. I would instead put them in analysis/code, because only formal functions (like your which.sources) can go into the /R directory if the pkg is to be buildable and pass the basic pkg tests.

I spotted a few absolute paths in the scripts, e.g.

Ideally those should be changed to relative paths.

wolass commented 6 years ago

You are right I forgot to change them.

I had to use absolute paths because I had path issues when executing scripts in the R folder. (working directory of the package is different from the working directory of the paper). Then after I used the knit button to generate the paper - (with the scripts sourced in the paper.Rmd file) the scripts would have been triggered from a different working directory and it constantly threw errors.

That is why I wanted to introduce the devtools::load_all() - after this, the scripts are run from their own folder and only produce objects that are afterward used in the paper.Rmd.

No path issues anymore :) All the R files are nicely sitting in the R folder and analysis can be separate from the actual write up. This is important for collaboration. When I do the exploratory analysis I tend to generate a lot of output that I do not wish to see in the final paper. But I want to use these files that were used for the output. Plus - Your colleagues do not need to see all of the code (that they hate) for every possible analysis :)

But you are right. I forgot to change them paths afterwards. :)

pon., 11 gru 2017 o 15:32 użytkownik Ben Marwick notifications@github.com napisał:

Thanks for sharing the URL to your review paper repo. I browsed with great interest! I see you put your R scripts in the /R directory. I would instead put them in analysis/code, because only formal functions (like your which.sources) can go into the /R directory if the pkg is to be buildable and pass the basic pkg tests.

I spotted a few absolute paths in the scripts, e.g.

  • d <- read.csv("~/Documents/refractoryANA/analysis/data/raw_data/refractoryAnaphylaxis.csv") in data_handling.R
  • load("~/Documents/anaphylaxis/data3.R") in analysis.R
  • export_graph(g,file_name = "~/Documents/refractoryANA/analysis/figures/algo.png", file_type = "png", title = NULL, width = NULL, height = NULL) in refracrory_graph.R

Ideally those should be changed to relative paths.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/benmarwick/rrtools/issues/46#issuecomment-350740443, or mute the thread https://github.com/notifications/unsubscribe-auth/ACPzx1iEJPMX58874CKAwbZOTeGUrmAKks5s_TzhgaJpZM4Q0fJf .