Extract only text from Rmd documents - r

Not even sure if this is possible, but is there a way to extract only the raw text portion of the .Rmd file and discard any code?
Or basically converting an .Rmd file into .txt file within R?
I've tried the function readLines, but this makes a huuuuuge character with all kinds of (to me) useless meta-data.

You can knit document without evaluating and including code.
Here's an example of dummy document foo.Rmd:
# Header 1
foo
## Header 2
bar
## Header 22
foobar
```{r}
1
```
text text text
```{r}
print(2)
```
We can knit this document using knitr::knit("foo.Rmd"), but in this case code chunks will be included in text. To deal with this we need to set knitr options:
library(knitr)
opts_chunk$set(list(echo = FALSE, eval = FALSE))
knit("foo.Rmd")
This command will create output document foo.md only with text.

Related

Include a chunk in an R Markdown footnote

this is the closest question I have found to mine, but it looks a bit dated.
I want to add a code chunk with eval = FALSE into the footnote of an R Markdown html document. I have tried the standard notation as in 2.5.1 here, but knitting ignores it.
Thanks for your help.
It should work if it your footnote looks like this:
Random Numbers[^a-random-footnote-label]
Other text for your document.
Other text for your document.
Other text for your document.
[^a-random-footnote-label]:
```{r, echo = F}
runif(1:10)
```
So you don't define the footnote directly via Random Numbers^[content footnote]. Instead you name it and define the contents later.

Dynamically building RMarkdown

I have a non-fixed List of *.Rmd files and want to dynamically render them into a single html File with RMarkdown.
Like this:
reportFiles <- list()
reportFiles[[1]] <- "F:\\report1.Rmd"
reportFiles[[2]] <- "F:\\report2.Rmd"
outputPath <- "F:\\report.html"
rmarkdown::render(input = reportFiles, output_file = outputPath)
But that doesnt work and i couldnt find a solution on how to do something like this. In all scenarios it either creates multiple files or you have to know what files yuo want to render beforehand or you have to create a temporary *.Rmd file.
One can combine multiple Rmd files into a single output document by modifying the code posted with the question.
First, the documents must be combined into a single Rmd before processing with rmarkdown::render(). Second, all files combined must take account of the following constraints.
Only the first Rmd file can contain document header information
Section labels must be unique across all Rmd files combined into a single Rmd for rendering.
The general approach is to read the files into a character vector, write the vector to a temporary Rmd file, then render the combined document.
library(rmarkdown)
# list of files to be combined
reports <- c("report1.Rmd","report2.Rmd")
# read the files & combine into a single character vector
theReports <- unlist(lapply(reports,readLines))
# use writeLines() to combine into single Rmd
tmpFile <- writeLines(theReports,"tmpReport.Rmd")
# render the combined document
render(input = "tmpReport.Rmd")
When rendered to an HTML document, the output looks like this:
Additional Considerations
We used a character vector instead of a list() to store the file names because the additional complexity of a list() was not needed to drive lapply() in this situation.
Use of a character vector allows the solution to be modified to potentially retrieve a list of files from a subdirectory with list.files(), as in:
reports <- list.files(path="./myReportDir/",
pattern="report[[:digit:]]+.Rmd",full.names=TRUE)
Also, one can segregate the header information into a file that contains only header info, such as report_header.Rmd.
Next, in order to automate retrieval of the files from a directory, one must ensure that the sort order of the file names matches the intended order of inclusion in the output document. H/T to Petr Kajzar for the regular expression to extract only files with a numbered report name from list.files().
Finally, also as suggested by Petr Kajzar in the comments, one can use a truly temporary file to drive rmarkdown::render() as follows.
tmpfile <- tempfile(fileext=".Rmd")
writeLines(theReports,tmpfile)
render(input = tmpfile)
Appendix
To make the example completely reproducible, we include the text of report1.Rmd and report2.Rmd. These files must be copied & saved to a local computer in order for the script above to read, write, and render them.
report1.Rmd
---
title: "report 1"
author: "lg"
date: "7/6/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
```{r cars}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.
report2.Rmd
Notice that the content in the second report conforms to the two constraints listed above.
## Report number 2
This is some text for the second markdown document. Considerations to make concatenation of multiple Rmd files into a single output document work:
1. Files 2 thru N must not have Rmd header information
2. Files that are combined into a single Rmd must not have duplicate section labels
```{r cars2}
summary(cars)
```
## Including Plots
You can also embed plots, for example:
```{r pressure2, echo=FALSE}
plot(pressure)
```
Note that the `echo = FALSE` parameter was added to the code chunk to prevent printing of the R code that generated the plot.

Use variable in Rmarkdown text

I am creating an Rmarkdown document.
My code chunk checks for the number of columns/rows in the dataset and saves the information in a variable.
{r, echo=FALSE, warning=FALSE}
df_dimenzions <- dim(demo)
I want to use the data/information stored in the variable in the rmarkdown text.
For example... outside of the code chunk, to write plain text such as:
The number of columns is {{df_dimensions[1]}} and the number of rows
is {{df_dimension[2]}}
Is something like this possible in rmarkdown? Again, I'm asking for data that is processed within the rmarkdown, not stored outside of the document?
Also, I am aware that I can paste a concatinated string with the code-chunk. That is not what I am trying to achieve.
Use `r df_dimensions[1]` in the main text.

Remove all code chunks from R Markdown document [duplicate]

Not even sure if this is possible, but is there a way to extract only the raw text portion of the .Rmd file and discard any code?
Or basically converting an .Rmd file into .txt file within R?
I've tried the function readLines, but this makes a huuuuuge character with all kinds of (to me) useless meta-data.
You can knit document without evaluating and including code.
Here's an example of dummy document foo.Rmd:
# Header 1
foo
## Header 2
bar
## Header 22
foobar
```{r}
1
```
text text text
```{r}
print(2)
```
We can knit this document using knitr::knit("foo.Rmd"), but in this case code chunks will be included in text. To deal with this we need to set knitr options:
library(knitr)
opts_chunk$set(list(echo = FALSE, eval = FALSE))
knit("foo.Rmd")
This command will create output document foo.md only with text.

Using R Markdown chunks in R Sweave(Knitr)

I have a R Markdown file that has my notes and chunks of code. I now want to write a R Sweave(Knitr) document to publish a paper using those chunks. I do not want to cut and paste the chunks, I rather call them directly. That way if I update the chunks, I don't have to do it in two places. It seems like it would be simple enough, but I can not figure it out. My code is as follows, test.rmd is my mark down document, foo is the chunk in the rmd file.
Test.rnw
<<Setup>>===
read_chunk('test.rmd')
#
<<foo>>==
#
Test.rmd
```{r foo, echo=TRUE}
print(summary(cars))
```
I would expect a summary of cars to be displayed in the output of the compilation of test.rnw into a PDF. But I don't. Any help is greatly appreciated.
read_chunk reads chunks from r script so call purl before read_chunk:
<<Setup>>=
knit_patterns$set(all_patterns[["md"]])
purl("test.Rmd")
knit_patterns$set(all_patterns[["rnw"]])
read_chunk("test.R")
#
<<foo>>=
#

Resources