output markdown in r code chunk - r

I have a R markdown file that I want to output rmarkdown from the script itself. For example, I would have the following simple code in an Rmd file.
---
title: "test"
author: "johndoe"
date: "September 5, 2015"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r cars}
paste("## This is a Heading in Code")
summary(cars)
```
I want "This is a Heading in Code" to render in rmarkdown. There is a solution in an R script to generate markdown as per http://rmarkdown.rstudio.com/r_notebook_format.html. But I am trying to figure out how to do this in a Rmarkdown file. Any help appreciated. Thanks.

Why build the header markup (either in markdown or HTML) manually? Try inline R expressions or some helper functions in pander (to generate markdown programatically):
---
title: "test"
author: "johndoe"
date: "September 5, 2015"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## `r 'This is a Heading in Code'`
```{r title, results='asis'}
library(pander)
pandoc.header("This is a Heading in Code", level = 2)
```
```{r cars, results='asis'}
summary(cars)
```

I searched for a good answer for this for some time after using cat("## Heading") inside results='asis' code chucks. I have seen many people dissatisfied by the results='asis' setting in the code chunk because it sets all results of the code chunk to not be wrapped in a code markup block. We have many cases when we want to output the heading along with results that should be wrapped in markup (e.g. a kable table that renders to a html table).
Here is the solution I found by simply specifying the "asis" attribute to the text object with knitr::asis_output and keeping the code chunk in the default 'markup' setting.
---
title: "test"
author: "johndoe"
date: "September 5, 2015"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r cars}
knitr::asis_output("## This is a Heading in Code")
summary(cars)
knitr::kable(summary(cars))
```
Unfortunately, at the current time knitr::asis_output only works in top-level R expressions, and it will not work when it is called inside another expression, such as a for-loop.

Related

Extracting a list of LaTeX figure and caption labels from RMarkdown to copy into Overleaf

I am working with coauthors. We want to produce all figures and tables (including an appendix) in a single RMarkdown document but write the paper jointly in Overleaf (excellent for simultaneous editing, which we don't need for the statistical code).
Here is an example Rmd which knits to a pdf. (The pdf will be appended manually to the main paper.)
---
title: "Nice document"
output: pdf_document
date: '2022-08-12'
---
```{r}
library(kableExtra)
```
```{r pressure, echo=FALSE, fig.cap="\\label{fig:pressure} Nice caption."}
plot(pressure)
```
```{r echo=FALSE, fig.cap="\\label{fig:diffpressure} Better caption."}
kable(head(mtcars), longtable = T, booktabs = T, caption = "Cool table", label = "tab:carssummary")
```
\appendix
\setcounter{figure}{0}
\setcounter{table}{0}
\renewcommand{\thefigure}{S\arabic{figure}}
\renewcommand{\thetable}{S\arabic{table}}
```{r echo=FALSE, fig.cap="\\label{fig:diffpressure} Better caption."}
plot(pressure/3.5)
```
I want a extract a character vector from the Rmd that is the following:
\begin{figure}
\caption{empty}
\label{fig:pressure}
\end{figure}
\begin{table}
\caption{empty}
\label{tab:carssummary}
\end{table}
\appendix
\setcounter{figure}{0}
\setcounter{table}{0}
\renewcommand{\thefigure}{S\arabic{figure}}
\renewcommand{\thetable}{S\arabic{table}}
\begin{figure}
\caption{empty}
\label{fig:pressure2}
\end{figure}
I will then copy and paste this to the bottom of the Overleaf doc, enabling us to do the automated cross referencing with minimal hassle (and easy updating if and when the analysis output changes).
How can I extract that LaTeX code from the Rmd?
Assuming your markdown document is called test.rmd and you included the keep_tex: true option to the header
---
title: "Nice document"
output:
pdf_document:
keep_tex: true
date: '2022-08-12'
---
You can upload the test.aux file to overleaf and then include the cross-refs in your overleaf document with the xr-hyper package:
\documentclass{article}
\usepackage{xr-hyper}
\externaldocument{test}
\usepackage{hyperref}
\begin{document}
some cross-ref: \ref{fig:diffpressure}
\end{document}

`knitr::include_url` ignored in output

After updating R to version 3.6.0, and re-installing the required packages, knitting Rmd documents now seems to ignore the knitr::include_url function.
The following is a minimal example, in a file named test.Rmd -
---
title: "test"
author: "Michael Dorman"
date: "May 1, 2019"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
```{r}
knitr::include_url("https://www.wikipedia.org/")
```
```{r}
knitr::include_graphics("https://www.wikipedia.org/portal/wikipedia.org/assets/img/Wikipedia-logo-v2.png")
```
And here is a screenshot of the output, after pressing Knit in Rstudio -
The knitr::include_url function is ignored. No <iframe> element was produced. The knitr::include_graphics function still works, so the image does appear in the output.
Thanks for reading, I appreciate any help!

How do I use themes for R Markdown with github_document as output?

I was told to write a report in R Markdown with the following template:
---
title: "Data Dictionary"
output: github_document
date: "Last Updated: `r format(Sys.time(), '%d, %B, %Y at %H:%M')`"
---
I want to use readthedown theme from rmdformats package, however I could not find a tutorial on how to do so properly. My question is, is it possible to use a theme with this template? Any help would be appreciated!
Assuming you already have installed the package, remotes::install_github("juba/rmdformats"), just pass rmdformats::readthedown to the output YAML:
---
title: "Untitled"
date: "Last Updated: `r format(Sys.time(), '%d, %B, %Y at %H:%M')`"
output: rmdformats::readthedown
---
```{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.

Passing parameters in rmarkdown to the text or headings

Is there any way to pass parameters in an rmarkdown document outside of a code chunk? For example, I'd love to have the ability to have a parameter value be a heading.
Here is a short example .Rmd file:
---
title: "param_test"
author: "test"
date: "September 14, 2017"
output: pdf_document
params:
param_test: this_text
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## I want this heading to be the value of params$param_test
```{r cars}
params$param_test
print(params$param_test)
```
Does anyone have any ideas on this?
You just need to add results='asis' to the chunk then you can print out the header from within the code.
cat("#", params$param_test, "\n")
Another option would be to use the pander library and run
pandoc.header(params$param_test)

Code in Columns in RMarkdown presentation

Often when I include r code in a rmarkdown pdf presentation, I want to use the space of the whole slide and therefore want to present the code and the output side-by-side.
In pure LaTex I would go for \begin{columns}...\end{columns} and then include the code/output manually using lstlistings or some other code-highlighting library. But that proves tedious if I exceed a couple of code examples.
Now I want to use RMarkdown for the presentations and would like to achieve a similar result.
However, the following code throws an error:
## This is slide 1
\begin{columns}[t]
\begin{column}{0.5\textwidth}
```{r, eval=F}
plot(1:10)
```
\end{column}
\begin{column}{0.5\textwidth}
```{r, echo=F}
plot(1:10)
```
\end{column}
\end{columns}
Leaving out the knitr code-chunks and replacing them with text works.
I am aware that it has something to do with the pandoc engine (see here), but wanted to ask if anybody has found a way around this issue.
Well, I may should have looked with a wider focus.
Here is a solution that works for Python, but can easily be adapted to Rmarkdown: https://stackoverflow.com/a/26069751/3048453
I ended up with this code:
in header.tex
\newcommand{\columnsbegin}{\begin{columns}}
\newcommand{\columnsend}{\end{columns}}
in presentation.Rmd
---
title: "Two-column codes in RMarkdown"
author: "My Name"
date: "February 4, 2017"
output:
beamer_presentation:
includes:
in_header: header.tex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Testslide with Columns
\columnsbegin
\column{.5\textwidth}
```{r, eval=F}
plot(mtcars[, 1:3])
```
\column{.5\textwidth}
```{r, echo=F}
plot(mtcars[, 1:3])
```
\columnsend
Which results in this

Resources