Is there an analogue of R markdowns \ SweaveInput {} to generate a modular report?

One of the features that I really like about Sweave is the ability to have \ SweaveInput {} separate Sweave files, to have a more "modular" report and just be able to comment out parts of the report that I don’t want which should be generated using one #\SweaveInput{part_x} instead of commenting or blocking entire blocks of code. I recently decided to move to R Markdown for several reasons, mainly practicality, the possibility of interactive (brilliant) integration in the report, and the fact that I really do not need advanced LaTeX formatting options. I found that technically pandoc is able to combine several Rmd files into one html output by simply concatenating them, but it would be nice if this behavior could be called from the "master" Rmd file.

Any response will be greatly appreciated, even if it simply "returns to Sweave, this is not possible in Markdown."

I am using R 3.1.1 for Windows and Linux, as well as Rstudio 0.98.1056 and Rstudio server 0.98.983.

+5
source share
2 answers

In the main document, use something like this:

 ```{r child="CapsuleRInit.Rmd"} ``` ```{r child="CapsuleTitle.Rmd", eval=TRUE} ``` ```{r child="CapsuleBaseline.Rmd", eval=TRUE} ``` 

Use eval=FALSE to skip one of them.

For RStudio users, you can define the main document for latex output, but this does not work for RMD documents, so you always need to switch to the main document for processing. Please support my request for the RStudio function; I've tried it twice already, but it seems to me that too few people use child documents to put them higher on the priority list.

+6
source

I don't quite understand some of the terms in the answer above, but the solution relates to defining a custom jersey: hook in the YAML header. For multi-party documents, this allows, for example:

  • Have a 'main' or 'root' Rmarkdown file with the output: markdown_document YAML output: markdown_document
  • return all child documents from Rmd β‡’ md before calling render , or not if this is a time limit
  • merge several files (with the option of a child code) into one (for example, for chapters in a report)
  • write output: html_document (or another format) YAML headers for this compilation output "on the fly", adding to the markdown effectively writing a new Rmarkdown file
    • ... then visualize this Rmarkdown to get the result, deleting the intermediate files in the process, if you want

The code for all of the above ( dumped here ) is described here , a message that I wrote after you recently developed using custom knit: header knit: YAML ( here ).

In the above example, the custom function knit: (i.e. replacing rmarkdown::render ):

 (function(inputFile, encoding) { input.dir <- normalizePath(dirname(inputFile)) rmarkdown::render(input = inputFile, encoding = encoding, quiet=TRUE, output_file = paste0(input.dir,'/Workbook-tmp.Rmd')) sink("Workbook-compiled.Rmd") cat(readLines(headerConn <- file("Workbook-header.yaml")), sep="\n") close(headerConn) cat(readLines(rmdConn <- file("Workbook-tmp.Rmd")), sep="\n") close(rmdConn) sink() rmarkdown::render(input = paste0(input.dir,'/Workbook-compiled.Rmd'), encoding = encoding, output_file = paste0(input.dir,'/../Workbook.html')) unlink(paste0(input.dir,'/Workbook-tmp.Rmd')) }) 

... but everyone is compressed on 1 line!

The rest of the file 'master' / 'root' / 'control' or whatever you want to call takes care of writing the above YAML for the final HTML output that goes through the intermediate Rmarkdown file, and its second chunk code programmatically adds child documents via a call list.files()

 ```{r include=FALSE} header.input.file <- "Workbook-header.yaml" header.input.dir <- normalizePath(dirname(header.input.file)) fileConn <- file(header.input.file) writeLines(c( "---", paste0('title: "', rmarkdown::metadata$title,'"'), "output:", " html_document:", " toc: true", " toc_depth: 3 # defaults to 3 anyway, but just for ease of modification", " number_sections: TRUE", paste0(" css: ",paste0(header.input.dir,'/../resources/workbook_style.css')), ' pandoc_args: ["--number-offset=1", "--atx-headers"]', "---", sep="\n"), fileConn) close(fileConn) ``` ```{r child = list.files(pattern = 'Notes-.*?\\.md')} # Use file names with strict numeric ordering, eg Notes-001-Feb1.md ``` 

The directory structure will contain a top level folder with

  • Final output of Workbook.html
  • A resources containing workbook_style.css
  • A documents containing the specified main file "Workbook.Rmd" along with files named "Notes-001.Rmd", "Notes-002.Rmd", etc. (to ensure that files are searched on list.files(pattern = "Notes-.*?\\.Rmd) and therefore makes them children in the correct order when rendering the main Workbook.Rmd file)

For proper file numbering, each component file "Notes-XXX.Rmd" must contain the following YAML style header:

 --- title: "March: Doing x, y, and z" knit: (function(inputFile, encoding) { input.dir <- normalizePath(dirname(inputFile)); rmarkdown::render(input = inputFile, encoding = encoding, quiet=TRUE)}) output: md_document: variant: markdown_github pandoc_args: "--atx-headers" --- ```{r echo=FALSE, results='asis', comment=''} cat("##", rmarkdown::metadata$title) ``` 

The code block at the top of the Rmarkdown document introduces the YAML header as the second-level header in the evaluation. results='asis' indicates returning a text string, not

 [1] "A text string" 

You would knit each of them before knitting the main file - it would be easier to remove the requirement for rendering all the child documents and just add their preliminary markdown release.

I described all of this from the links above, but I thought that it would be a bad way not to leave the actual code with my answer.

I don’t know how effective a website with a request for an RStudio function can be ... Personally, it was not difficult for me to study the source code of these functions, which, fortunately, are open source , and if there really is something missing, but undocumented, requesting functions with internal processing is probably much more efficient by one of their software developers.

I am not familiar with Sweave, was that what you were aiming for? If I understand correctly, you just want to control the inclusion of documents in a modular way. The child = list.files() operator can take care that: if not through a file push, you can align the list files like child = c("file1.md","file2md") ... and switch this operator so that change them. You can also control the TRUE / FALSE switches with YAML, as a result of which a custom header will include some children, for example,

 potentially.absent.variable: TRUE 

... above a document with a quiet include=FALSE hiding the fraud of the first fragment:

 ```{r include=FALSE} !all(!as.logical(rmarkdown::metadata$potentially.absent.variable) # ==> FALSE if potentially.absent.variable is absent # ==> FALSE if anything other than TRUE # ==> TRUE if TRUE checkFor <- function(var) { return !all(!as.logical(rmarkdown::metadata[[var]]) } ``` ```{r child = "Optional_file.md", eval = checkFor(potentially.absent.variable)} ``` 
+1
source

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


All Articles