R-Markdown: input and output with relative channels

I am working on a project that is being developed by our team. We pass the codes to the repository. Each team member uses their own machine with their working directories. That is why we use relative paths in our projects. Usually we use something like

setwd("MyUser/MyProject/MyWD/myCodesDir") # local
...
MyReportingPath <- "../ReportsDir" # in repository

Now I'm trying to make a markdown report in this directory:

rmarkdown::render(input = "relevantPath/ReportingHTML.Rmd",
                output_file = paste0(MyReportingPath, "/ReportingHTML.html"))

This does not work. It only works if I enter the full path to the output file ("/home/User/..../ReportingHTML.html") This is one of the problems I would like to clarify: is there any way to use relative paths for markdown?

The second problem is that if I enter a nonzero directory in the output_file, pandoc gives me an error instead of creating this directory with my output file. Is it possible to create a dynamic output directory? (except for executing the system (paste0 ("mkdir", reportPath), intern = T) before rendering)

PS It’s important for me to create a document with markup in a separate function R, where I create the whole environment that is inherited by my Markdown document.

+4
source share
1 answer

The trivial problem is, since you are using paste0, you need to provide a separator /between your output directory and the output file.

You wrote:

rmarkdown::render(input = "relevantPath/ReportingHTML.Rmd",
            output_file = paste0(MyReportingPath, "ReportingHTML.html"))

Try instead:

rmarkdown::render(input = "relevantPath/ReportingHTML.Rmd",
            output_file = paste0(MyReportingPath, "/", "ReportingHTML.html"))

More broadly:

( ) - here::here(). , :

parent_dir <- paste(head(unlist(strsplit(here::here(), "/", fixed = TRUE)), -1), collapse = "/")
grandparent_dir <- paste(head(unlist(strsplit(here::here(), "/", fixed = TRUE)), -2), collapse = "/")

- , , :

project_dir <- here::here()
codefile <- paste(project_dir, "code", "myreport.Rmd", sep = "/")
outfile <- paste(project_dir, "results", "myreport.html", sep = "/")
rmarkdown::render(input = codefile,
            output_file = outfile))

( ) - dir.create("MyReportingPath", recursive = TRUE) . , , showWarnings = FALSE.

+1

Source: https://habr.com/ru/post/1696008/


All Articles