This question is similar, but not identical to this one.
Basically, I have a number of tables that I would like to show in tabsets using DT::datatable(). Unfortunately, I can't figure out how.
The following code works, but I need to manually type all the code:
---
title: "Untitled"
format: html
---
```{r}
library(DT)
```
::: {.panel-tabset}
### table no. 1
```{r}
#| results: asis
datatable(mtcars)
```
### table no. 2
```{r}
#| results: asis
datatable(mtcars)
```
:::
The following works, but instead of datatable() uses a simple markdown table from pander which does not give the desired effect.
---
title: "Untitled"
format: html
---
```{r}
library(pander)
```
::: {.panel-tabset}
```{r}
#| results: asis
for(i in 1:2) {
cat(sprintf("\n### table no. %d\n\n", i))
cat(pander(mtcars))
}
```
:::
The following code does not work, and I don't know how to make it work:
---
title: "Untitled"
format: html
---
```{r}
library(DT)
```
::: {.panel-tabset}
```{r}
#| results: asis
for(i in 1:2) {
cat(sprintf("\n### table no. %d\n\n", i))
print(datatable(mtcars))
}
```
:::
This is a known issue in the context of RMarkdown. But as it turns out the solution for RMarkdown also works for Quarto.
Following this answer related to the same issue in RMarkdown [Discalimer: The answer is from me] you could achieve your desired result by first making sure that the javascript dependencies needed for DT are included in your document and by wrapping the datatable call inside htmltools::tagList:
---
title: "Untitled"
format: html
---
```{r}
library(DT)
```
```{r, include=FALSE}
# Init Step to make sure that the dependencies are loaded
htmltools::tagList(datatable(mtcars))
```
::: {.panel-tabset}
```{r}
#| results: asis
for(i in 1:2) {
cat(sprintf("\n### table no. %d\n\n", i))
print(htmltools::tagList(datatable(mtcars)))
}
```
:::
Related
How to use code chunk from child document in another Rmd file? I have one parent document named figures.Rmd which have following structure
`{r child='force.Rmd',echo=F,ref.label='pressure'}
`
some text
`{r child='force.Rmd',echo=F,ref.label='cars'}
and the child document named force.Rmd which have following structure
`{r, cars,echo=F}
options(knitr.duplicate.label = 'allow')
plot(cars)
`
`{r, pressure,echo=F}
options(knitr.duplicate.label = 'allow')
summary(pressure)
`
I want to read some chunks from the force.Rmd in the figures.Rmd. I am getting some output but it is producing the output 2 times. So basically I have 2 or 3 chunks in force.Rmd which I want to use at different places in figures.Rmd . How can I do that without getting the output 2 times?
This is parent.Rmd:
---
title: "parent"
author: "Me"
date: "2023-01-25"
output: html_document
---
## Parent
This is parent
## Some part of child
```{r echo=FALSE}
library(knitr)
invisible(knitr::purl("child.Rmd", output="temp", quiet=TRUE))
read_chunk("temp")
```
```{r ref.label='cars'}
```
## Some text
Here I am.
## Some other part of child
## Some part of child
```{r ref.label='pressure'}
```
```{r echo=FALSE}
unlink("temp")
```
This is child.Rmd
```{r cars}
summary(cars)
```
```{r pressure}
plot(pressure)
```
I can call each single child chunk at various positions:
I would like to add a bibtex reference to the notes for a gt table in Quarto, but using #citationkey does not seem to work. Does anyone know how to do it?
Here's a reproducible example:
---
title: "Untitled"
format: pdf
bibliography: refs.bib
---
```{r}
#| include: false
library(dplyr)
library(gt)
```
Here I can cite #dirac
But below I get only the literal "#dirac". If I do not include the quotation marks, the code does not compile.
```{r}
#| include: true
#| echo: false
head(mtcars) %>%
gt() %>%
tab_source_note("#dirac")
```
# References {-}
And the reference in refs.bib:
#book{dirac,
title={The Principles of Quantum Mechanics},
author={Paul Adrien Maurice Dirac},
isbn={9780198520115},
series={International series of monographs on physics},
year={1981},
publisher={Clarendon Press},
keywords = {physics}
}
Following this post you could read your bib file via bibtex::read.bib. Afterwards you could add your citation to the gt table via cite like so:
---
title: "Untitled"
format: pdf
bibliography: refs.bib
---
```{r}
#| include: false
library(dplyr)
library(gt)
library(bibtex)
```
```{r}
biblio <- bibtex::read.bib("refs.bib")
```
Here I can cite #dirac
But below I get only the literal "#dirac". If I do not include the quotation marks, the code does not compile.
```{r}
#| include: true
#| echo: false
head(mtcars) %>%
gt() %>%
tab_source_note(cite("dirac", biblio, textual = TRUE))
```
After upgrading Rstudio to 1.4 version, when I render this rmarkdown doc
---
title: "Raw HTML"
output: html_document
---
```{r setup, include=FALSE}
library(knitr)
library(htmltools)
knitr::opts_chunk$set(echo = F)
```
```{r}
f <- function(text){
cat(asis_output(htmltools::htmlPreserve(paste("<span style=\"color:green\">", text, "</span>"))))
return(0)
}
```
```{r}
x <- f('Hello!')
```
I get this html doc
But before the update this code worked as expected, rendering this html doc
How can I get the second html doc?
cat removes the knit_asis class.
You can either avoid cat or use results='asis' chunck option :
---
title: "Raw HTML"
output: html_document
---
```{r setup, include=FALSE}
library(knitr)
library(htmltools)
knitr::opts_chunk$set(echo = F)
```
```{r}
side.effect <- function(text){
cat(asis_output(htmltools::htmlPreserve(paste("<span style=\"color:green\">", text, "</span>"))))
return(0)
}
direct <- function(text){
asis_output(htmltools::htmlPreserve(paste("<span style=\"color:green\">", text, "</span>")))
}
```
```{r}
direct('direct Hello!')
```
```{r,results ='asis'}
x <- side.effect('side effect Hello!')
```
When I want to print some tables in html report from knitr, sometime I use knitr::kable(), and other times I use htmltable::htmltable.
When I use bookdown::html_document2 in the YAML numbering of tables from kable is automatic. However, it is not for htmltable, and I need to use options(table_counter = TRUE), which generates another numbering mechanism. Is there a way to unify it?
Example:
---
title: "Untitled"
author: "Guilherme"
date: "10/26/2020"
output:
bookdown::html_document2
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
options(table_counter = TRUE)
```
```{r cars}
library(htmlTable)
htmlTable(mtcars[1:5,1:5],
caption = "XX")
```
```{r}
htmlTable(mtcars[1:5,1:5],
caption = "XX")
```
```{r}
library(knitr)
kable(mtcars[1:5,1:5],
caption = "XX")
```
Outputs:
Thanks!
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.