RevolutionAnalytics / checkpoint

Install R packages from snapshots on checkpoint-server
164 stars 38 forks source link

scanForPackages() fails when run inside an RMarkdown document being knitted #277

Closed connorp closed 4 years ago

connorp commented 5 years ago

I am hoping to use scanForPackages() to identify all packages used to generate an RMarkdown document, within the document. When run in the console, scanForPackages(use.knitr = TRUE) is able to detect all the packages loaded in the .Rmd file. But when knitted, it returns without any packages.

Minimal example, saved as test.Rmd:

    ---
    title: "Test Doc"
    author: "Connor P. Jackson"
    date: "7/22/2019"
    output:
      pdf_document:
        keep_md: yes
    ---

    ```{r setup}
    library(pander)
    library(checkpoint)
```{r citations}
scanForPackages(use.knitr = TRUE)
```

Expected output, as generated when run in the console: 

$pkgs [1] "checkpoint" "pander"

$error character(0)


But when the document is knitted, the results are:

No file at path '/var/folders/6q/wp5m99kx41z7txrfxwywf2wr0000gn/T//RtmpbZ4NGz/file32a67e5f45d9.Rmd'.

$pkgs

character(0)

$error

[1] "test.Rmd"



Any ideas why this file does not work within a document being knitted? 
grssnbchr commented 5 years ago

Without looking specifically at your problem, I suggest you have a look at my reproducible template that uses RMarkdown. It basically uses a workaround that temporarily writes the library(xyz) calls into a file and then checkpoint reads from that file to load/install the packages. See this part of the code.

connorp commented 5 years ago

That is relevant to the problem, but this issue nevertheless seems like something that should be addressed. A workaround is not a fix.

grssnbchr commented 5 years ago

Absolutely agree!

hongooi73 commented 4 years ago

For this to work correctly, run rmarkdown::render("yourfile.Rmd") in the REPL.

hongooi73 commented 4 years ago

Also note that fundamentally, checkpoint uses directories to define projects. This is problematic with Rmd files as:

So, best practice would be to put your Rmds in a directory of their own, and call rmarkdown::render with the knit_root_dir argument set appropriately.