How can I generate a narrative functionally in RMarkdown? For instance, say I want to generate 3 headers like
##First
##Second
#Third
from a vector c('First', 'Second', 'Third'), then knit to pdf
If you're trying to knit to pdf, this solution calls on LaTeX within an R code chunk to create the headers as you requested. Just add this into an .Rmd file:
---
title: "Untitled"
author: "Ryan Runge"
date: "8/30/2017"
output: pdf_document
---
```{r eval=TRUE, results='asis' }
headers <- c('First', 'Second', 'Third')
cat(paste("\\section{",headers,"}"))
```
The output is:
And if you don't want the R code chunk to show in the pdf, just supply echo=FALSE in the code chunk options.
You can reference a pre-defined list of headers in RMarkdown as follows:
```{r}
list_of_headers <- c("Title 1", "Title 2", "Title 3")
```
This is an example.
### `r list_of_headers[1]`
Stuff under the first header
### `r list_of_headers[2]`
Etc
### `r list_of_headers[3]`
Related
I am producing plotly charts in Rmarkdown and give them name and caption. but as soon as I cross reference them in the document, their name is ruined.
Here is the code:
---
title: "Preliminary Statistics"
author: "Babak"
date: "6/28/2021"
output:
bookdown::html_document2:
fig_caption: true
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(
echo = FALSE,
fig.width=9,
fig.height=5 ,
message=FALSE,
warning=FALSE)
```
```{r Area_duplicate, fig.cap = "Area of duplicated and unique properties by municipality"}
<PLOT CODES>
```
Plot \#ref(fig:Area_duplicate) shows the area od duplicated properties in the dataset grouped by municipality.
result:
screenshot of the caption
What should I do to resolve this issue?
Replace the underscore in the name with a "-".
I use the package captioner to automatically reference tables, figures, supplementary data, codes, etc in my normal work. Althoug I can use it efficiently, I would like to create a section in the RMarkdown document which contains the list (index) of all the elements of the same style.
Let's say I have 2 tables in a RMarkdown document.
---
title: "Untitled"
author: "Mario Modesto Mata"
date: "6/7/2020"
output:
pdf_document: default
html_document: default
---
```{r setup, include=FALSE}
library(captioner)
table_number <- captioner(prefix = "Table")
```
The data frame of mtcars is in `r table_number("table1", display="cite")`.
```{r}
data(mtcars)
print(mtcars)
```
`r table_number(name = "table1", caption = "Caption of table1 (mtcars)")`
The data frame of mtcars is in `r table_number("table2", display="cite")`.
```{r}
data(iris)
print(iris)
```
`r table_number(name = "table2", caption = "Caption of table2 (iris)")`
I would like to include at the beginning an index of the tables that were defined with the function table_number().
Is there a way to do this? I would prepare later the equivalent index for figures and other topics...
I am looking to print multiple flextables in a single R Markdown document. This is not challenging when using html output, but I need Word output.
With html output, the following R Markdown code (example taken from https://davidgohel.github.io/flextable/articles/offcran/examples.html#looping-in-r-mardown-documents) produces a document with multiple Flextables:
---
output:
html_document: default
---
```{r}
library(htmltools)
library(flextable)
ft <- flextable(head(iris))
tab_list <- list()
for(i in 1:3){
tab_list[[i]] <- tagList(
tags$h6(paste0("iteration ", i)),
htmltools_value(ft)
)
}
tagList(tab_list)
```
I have been unable to get an equivalent output using Word document output.
The solution proposed in
How to knit_print flextable with loop in a rmd file similarly works fine with html output, but I have failed to get this to render correctly with Word output.
Any advice on a R Markdown Word document output equivalent of the above example would be very helpful!
You need to use flextable::docx_value and the chunk option results='asis'
---
title: "Untitled"
output: word_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(flextable)
```
```{r results='asis'}
ft <- flextable(head(iris))
for(i in 1:3){
cat("<w:p/>")## add this to add an empty new paragraph between tables
flextable::docx_value(ft)
}
```
I am trying to modify the appearance of a table using kable and kableExtra packages in R Markdown knitting to PDF output.
In particular, I would like to remove all borders from the table and add only one horizontal line under the header and one vertical line next to the row names.
At the moment, I have this code:
---
header-includes: \setmainfont[Path = C:/Windows/Fonts/]{Arial}
\usepackage{colortbl}
\arrayrulecolor{white}
output:
pdf_document:
latex_engine: xelatex
word_document: default
---
```{r echo=FALSE, message=FALSE, warning=F, paged.print=TRUE}
library(kableExtra)
library(magrittr)
DATA<- data.frame(jen=c(1,2,3,4,5), feb=c(2,3,4,5,3), mar=c(0,2,4,1,2))
rownames(DATA)<-c("first","second","third","fourth","fifth")
kable(DATA, "latex")
```
Which generates this table:
Table
To delete all borders, I set default borders to be white as the answer to this question suggested.
Now, I would like to add an horizontal blue line under the header and a vertical blue line at the right side of the row names.
I can manage to get the horizontal line with the following:
---
header-includes: \setmainfont[Path = C:/Windows/Fonts/]{Arial}
\usepackage{colortbl}
\arrayrulecolor{white}
output:
pdf_document:
latex_engine: xelatex
---
```{r echo=FALSE, message=FALSE, warning=F, paged.print=TRUE}
library(kableExtra)
library(magrittr)
DATA<- data.frame(jen=c(1,2,3,4,5), feb=c(2,3,4,5,3), mar=c(0,2,4,1,2))
rownames(DATA)<-c("first","second","third","fourth","fifth")
kable(DATA, "latex") %>%
row_spec(0, extra_latex_after = "\\arrayrulecolor[rgb]{0,.275,.725}") %>%
row_spec(1:nrow(DATA), extra_latex_after = "\\arrayrulecolor{white}")
```
Obtaining this.
I would like to do the same with a vertical line but there is no such command as 'extra_latex_after' in the function 'column_spec'. It only accepts 'extra_css' commands which obviously do not work for a PDF output.
My goal is to get something like this.
I know I can get the same result with other packages for tables, but I am wondering if it is possible to get something like this using kable.
Does anyone know if there is a solution for this?
This is indeed not supported so far. You could make a feature request on github.
You can hack this using a regular expression to replace the alignment options in your tabular:
---
title: "Test"
date: 2019-02-13
output: pdf_document
---
```{r header, echo= FALSE, include = T, warning=F}
library(knitr)
tbl <- kable(mtcars[1:4, 1:3], format = "latex")
# here we search for the begin command and its options and replace them
gsub(pattern = "(begin\\{tabular\\})(\\{.*?\\})",
repl = "\\1{l|r !{\\\\color{red}\\\\vrule width 1pt} r|r|}",
x = tbl)
```
I’m using R Markdown in RStudio to create a report that mixes Markdown and R output. I know how to use inline R expressions in the Markdown, but I’m wondering how to do the converse, i.e., use Markdown within the R code. I want to loop through a series of calculations and get Markdown headings for each one. I want headings for formatting purposes (e.g., bold titles etc) and also to be able to specify (sub)sections in the resulting PDF (which is a nice feature of the way RMarkdown handles # and ## etc).
I know I can do the following:
---
title: "test"
output: pdf_document
---
#Section 1
```{r, echo=FALSE}
print(1+1)
```
#Section 2
```{r, echo=FALSE}
print(2+2)
```
#Section 3
```{r, echo=FALSE}
print(3+3)
```
Which gives something looking (roughly) like this:
Section 1
## [1] 2
Section 2
## [1] 4
Section 3
## [1] 6
Is it possible to achieve the same output using something along the lines of this:
---
title: "test2"
output: pdf_document
---
```{r, echo=FALSE}
for (i in 1:3)
{
print(paste("#Section",i))
print(i+i)
}
```
As #scoa pointed out, you have to set the chunk option results='asis'. You should also place two \n both before and after your header.
---
title: "test"
output: pdf_document
---
```{r, echo=FALSE, results='asis'}
for (i in 1:3) {
cat(paste0("\n\n# Section", i, "\n\n"))
print(i+i)
cat("\n\n\\newpage")
}
```
As a more general answer, it could be useful to a look at the markdownreports package that parses markdown code (and output) from your R variables.