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))
```
Related
The following Test.Rmd will produce an interactive learning widget for r using knitr (see the link).
---
title: "Example Document"
author: "Your name here"
output:
html_document:
self_contained: false
---
```{r, include = FALSE}
tutorial::go_interactive()
```
By default, `tutorial` will convert all R chunks.
```{r}
a <- 2
b <- 3
a + b
```
Edited
Wondering how to an get an interactive learning widget for r using quarto. Need help for YAML of .qmd, my attempt is
---
title: "Test"
author: "MYaseen208"
format:
html:
self_contained: false
---
```{r}
#| echo: false
#| include: false
tutorial::go_interactive()
```
```{r}
#| echo: false
1 + 1
```
You can add options to executable code like this
```{r}
#| echo: false
2 * 2
```
The `echo: false` option disables the printing of code (only output is displayed).
However, it throws the following error:
ERROR: Validation of YAML front matter failed.
ERROR: (line 5, columns 3--7) Field "html" has empty value but it must instead be an object
4: format:
5: html:
~
6: ---
ERROR: Render failed due to invalid YAML.
Another attempt which render but don't produce the required output:
---
title: "Test1"
author: "MYaseen208"
format:
html:
output-file: "Test1.html"
self-contained: false
---
To fix the error, you should use self-contained instead of self_contained in quarto like this:
---
title: "Test"
author: "MYaseen208"
format:
html:
self-contained: false
---
```{r}
#| echo: false
#| include: false
tutorial::go_interactive()
```
```{r}
#| echo: false
1 + 1
```
You can add options to executable code like this
```{r}
#| echo: false
2 * 2
```
The `echo: false` option disables the printing of code (only output is displayed).
Output:
I don't think the package is currently supported on Quarto, since the latest release of the package was in 2016. You might contact them about this.
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)))
}
```
:::
I am developing vignette for a package in r and wish to include some citations in the document. I want the citations to look like this `Author (year) unfortunately what I have is (Author year)
---
title: "MNP: My New Package"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{MNP}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
bibliography: "economia.bib"
logo: logo.png
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
```{r setup}
library(MNP)
```
## Statement of the Research Problem
### Introduction
- [#Hall1995] and [#Politis2004]
This is what I have
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")
```
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.