I use this rmd:
---
title: "Some Title"
author: "Some Author"
date: "`r format(Sys.time(), '%d-%m-%Y')`"
---
## A function that generates sections
```{r setup, include = FALSE}
library(pander)
create_section <- function() {
# Inserts "## Title (auto)"
pander::pandoc.header('Title (auto)', level = 2)
# Section contents
# e.g. a random plot
plot(sample(1000, 10))
# a list, formatted as Markdown
# adding also empty lines, to be sure that this is valid Markdown
pander::pandoc.p('')
pander::pandoc.list(letters[1:3])
pander::pandoc.p('')
}
```
## Generate sections
```{r, results='asis', echo=FALSE}
n_sections <- 3
for (i in seq(n_sections)) {
create_section()
}
```
and then:
library(knitr);
library(rmarkdown);
setwd("C:/bla")
knit('test_md.Rmd')
rmarkdown::pandoc_convert("test_md.md", to = "pdf", output = "test_pdf.pdf")
This kind of works but the plots are all rendered after the sections:
Each section should contain the plot. Any ideas? Thanks!
PS:
Wrapping:
plot(sample(1000, 10))
in print:
print(plot(sample(1000, 10)))
forces output to be produced. Unfortunately, NULL is also printed underneath the plot.
Could you just add pdf_document in the YAML and then generate the PDF by knitting?
---
title: "Some Title"
author: "Some Author"
date: "`r format(Sys.time(), '%d-%m-%Y')`"
output: pdf_document
---
I was able to get the same result you described when running the rmarkdown::pandoc_convert() on the .md file, all the plots at the end of the .PDF file.
Related
I am making my code more modular and would like to run multiple RMarkdown files from one overall RMarkdown. I believe I could do this if I translated all my RMarkdown files to .R scripts and used source(), but I like the document-like nature of RMarkdown and I can describe what I'm doing as I'm doing it in plain text.
The goal is to wrangle data and export a usable .sav file. I want to run clean.rmd from run.rmd, but I don't want any HTML/pdf/etc. output. Removing the output line in the YAML header doesn't prevent output. If there is a way to do this without translating everything to .R scripts, I would be very appreciative. Thank you.
clean.rmd: Script that does the cleaning
---
title: "clean"
author: "jrcalabrese"
date: "12/30/2021"
output: html_document
---
```{r}
library(tidyverse)
library(haven)
```
```{r}
data(cars)
cars <- cars %>%
mutate(newvar = speed + dist)
```
```{r}
write_spss(cars, "~/Documents/cars_new.sav", compress = FALSE)
```
run.rmd: Script that runs clean.rmd
---
title: "run"
author: "jrcalabrese"
date: "12/30/2021"
output: html_document
---
```{r}
rmarkdown::render("~/Documents/clean.rmd")
```
Thank you for your help! This function works:
---
title: "run"
author: "jrcalabrese"
date: "12/30/2021"
#output: html_document
---
```{r}
source_rmd = function(file, ...) {
tmp_file = tempfile(fileext=".R")
on.exit(unlink(tmp_file), add = TRUE)
knitr::purl(file, output=tmp_file)
source(file = tmp_file, ...)
}
```
```{r}
source_rmd("~/Documents/clean.rmd")
```
Im new to Rmarkdown and I would like to create dynamic reports where every report section is generated from a template (child) document. Each section will then start with a newpage in the rendered pdf.
My approach is currently based on this post which shows how to generate dynamically text in the child (which works), however I am not able to transfer the contents of the loop into a R-Codeblock, probably because the params are not well defined in the way that I tried to do it.
This is how my parent document looks like:
---
title: "Dynamic RMarkdown"
output: pdf_document
---
```{r setup, include=FALSE}
library("knitr")
options(knitr.duplicate.label = "allow")
```
# Automate Chunks of Analysis in R Markdown
Blahblah Blabla
\newpage
```{r run-numeric-md, include=FALSE}
out = NULL
for (i in as.character(unique(iris$Species))) {
out = c(out, knit_expand('template.Rmd'))
params <- list(species = i)
}
```
`r paste(knit(text = out), collapse = '\n')`
and this is how the child looks like
---
title: "template"
output: html_document
params:
species: NA
---
# This is the reporting section of Species {{i}}
This is a plot of Sepal length and width based on species {{i}}.
```{r plot2}
paste(params$species)
# plot doesnt work work
# plot(iris$Sepal.Length[iris$Species=={{i}}],
# iris$Sepal.Width[iris$Species=={{i}}]
# )
```
\newpage
To my understanding the parameter that is actually passed is the last species from the dataset generated in the loop but I'm not sure why the plot would't work then. Can anybody help me out on how to fix this issue?
Ok. No need to go through params. The solution was simply to put i between brackets AND parenthesis in the child-document.
Parent:
---
title: "Dynamic RMarkdown"
output: pdf_document
---
```{r setup, include=FALSE}
library("knitr")
options(knitr.duplicate.label = "allow")
```
# Automate Chunks of Analysis in R Markdown
Blahblah Blahblah Main text before individual sections
\newpage
```{r run-numeric-md, include=FALSE}
out = NULL
for (i in as.character(unique(iris$Species))) {
out = c(out, knit_expand('template.Rmd'))
}
```
`r paste(knit(text = out), collapse = '\n')`
Child
---
title: "template"
output: html_document
---
# This is the reporting page of Species {{i}}
This is a plot of Sepal length and width based on species {{i}}.
```{r plot2}
paste("This will be a plot of Sepal Length and Witdh from", '{{i}}')
plot(iris$Sepal.Length[iris$Species=='{{i}}'],
iris$Sepal.Width[iris$Species=='{{i}}']
)
```
\newpage
Original solution found here.
Is is possible to hide some comments in code when kniting using knitr / R markdown? Example:
---
title: "SOSO"
author: "SO"
date: '2017-06-06'
output: pdf_document
---
```{r}
# Generate some data
rnorm(2)
## But keep this comment
```
When kniting I would like the first comment to disapear, but keep the second one somehow.
Here is a quick example of modifying the hook to change knitr behavior.
---
title: "SOSO"
author: "SO"
date: 2017-06-06
output: pdf_document
---
```{r setup-hook, echo=FALSE}
hook_in <- function(x, options) {
x <- x[!grepl("^#\\s+", x)]
paste0("```r\n",
paste0(x, collapse="\n"),
"\n```")
}
knitr::knit_hooks$set(source = hook_in)
```
```{r}
# Generate some data
# Lines that starts with `# ` will be removed from the rendered documents
rnorm(2)
## But keep this comment
## But lines that starts with `## ` will be kept
```
produces this
In fact, you can choose to show any lines of R code by passing numeric indices to the chunk option echo, e.g.
---
title: "SOSO"
author: "SO"
date: '2017-06-06'
output: pdf_document
---
```{r echo=4:7}
# Generate some data
rnorm(2)
## But keep this comment
```
See knitr documentation for more information.
I am generating a pdf from an Rmd file with output = pdf and xelatex.
Some figures are large and cover the entire page. So the generated pdf creates an empty page. To avoid that I decided to reduce the size of the figure using out.width='90%'.
Here is my .Rmd file:
---
title: "test out width"
author: "Courvoisier"
date: "8 November 2016"
output:
pdf_document:
fig_height: 8
fig_width: 12
keep_tex: yes
latex_engine: xelatex
number_sections: yes
toc: yes
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Including Plots
```{r pressure, echo=FALSE}
plot(pressure)
```
```{r pressure2, echo=FALSE, out.width='90%'}
plot(pressure)
```
I used render to create the pdf. Here is the .R code:
rm(list = ls())
library(knitr)
library(rmarkdown)
RmdFile = "testoutwidth.Rmd"
outputpdfFilePathoutPdf = "testoutwidth.pdf"
renderoutputpdfFilePath = render(input = RmdFile, output_file = outputpdfFilePathoutPdf)
This works fine and creates the following .TeX code for the figures:
\includegraphics{testoutwidth_files/figure-latex/pressure-1.pdf}
\includegraphics[width=0.9\linewidth]{testoutwidth_files/figure-latex/pressure2-1}
However, when choosing an output_dir in the render function such as:
renderoutputpdfFilePath = render(input = RmdFile, output_dir = "out pdf/", output_file = outputpdfFilePathoutPdf)
The includegraphics commands in the .TeX generated become
\includegraphics{C:/work/testRenderRmd/out pdf/testoutwidth_files/figure-latex/pressure-1.pdf}
\includegraphics[width=0.9\linewidth]{C:\work\testRenderRmd\out pdf\testoutwidth_files/figure-latex/pressure2-1}
In the second \includegraphics (the one that has [width=0.9\linewidth]) the path separators are "\" instead of "/" and so the latex compilation fails. (it gives C:\work... instead of C:/work/... ).
What should I use to solve this?
thanks
Note: it seems there is an issue open about this on the rmarkdown github :
https://github.com/rstudio/rmarkdown/issues/808
Just wanted to insert two images in to the rmarkdown pdf document. When knitting it gives the error
pandoc.exe: Could not find image paste0(Figs,%20%22Fig1.png%22)',
skipping... pandoc.exe: Unable to convert
paste0(Figs,%20%22Fig1.png%22)' for use with pdflatex. ! Missing
\endcsname inserted.
Below is the code
---
title: "Some title"
author: Arvin
date: "October 20, 2016"
output: pdf_document
fig_caption: yes
---
```{r, echo=FALSE}
Figs <- 'C:/Users/arvin/Figs/'
```
![Fig1](paste0(Figs, "Fig1.png"))
I used knitr_1.14 and r_markdown_1.0.
It is clear, why your code does not work: you used the function paste outside the R-environment. So pandoc searches for paste0(Figs,%20%22Fig1.png%22) as a filename.
You can't do it in this way you wanna do this. The regular usage is
---
title: "Some title"
author: Arvin
date: "October 20, 2016"
output:
pdf_document:
fig_caption: yes
---
![Fig1](C:/Users/arvin/Figs/Fig1.png)
BUT there is another solution, where you have the full control of image size, and you can plot like a regular R plot, using grid.raster from the grid package. And here your approach will work, because we are in a R environment:
```{r fig.width=10, fig.height=10, echo=FALSE}
library(png)
library(grid)
Figs <- 'C:/Users/arvin/Figs/'
img <- readPNG(paste0(Figs, "Fig1.png"))
grid.raster(img)
```
Inline R code should be put in `r `:
---
title: "Some title"
author: Arvin
date: "October 20, 2016"
output:
pdf_document
fig_caption: yes
---
```{r, echo=FALSE}
Figs <- 'C:/Users/arvin/Figs/'
```
![Fig1](`r paste0(Figs, "Fig1.png")`)
BTW, an unsolicited pro-tip: Do not use absolute paths. Use relative paths.