Run shiny document with table of content - r

When I add the following lines to add table of content to a shiny document:
toc: true
toc_float: true
The document fails to run. It runs fine without them. I get the following error:
Error Scanner error: mapping values are not allowed in this context at line 5, column 8 (line 5 is toc: true)
How can I resolve this issue?
The code I ran:
---
title: "Untitled"
runtime: shiny
output:
html_document
toc: true
toc_float: true
---
### section 1
```{r, echo=FALSE}
sliderInput("slider", "Number of observations:", 1, 100, 50)
renderText({paste0(rep('blah', 100), collapse = '\n')})
```
### section 2
```{r, echo=FALSE}
renderPlot({hist(rnorm(500)[seq_len(input$slider)])})
```

You're just missing a : from html_document. It should be html_document:
---
title: "Untitled"
runtime: shiny
output:
html_document:
toc: true
toc_float: true
---

Related

An interactive learning widget for R using Quarto

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.

How can I get the order of plots correct in an rmd? [How can I control the print-time of messages?]

As the minimal reproducible example below shows, plot 2 appears too late.
How can I fix this?
It seems, the message screws everthing up. Most likely related: this and this.
---
title: "Run all test-cases"
author: "STCH"
date: "`r Sys.Date()`"
output:
html_document:
keep_md: yes
number_sections: yes
toc: yes
toc_depth: 2
code_folding: hide
pdf_document:
number_sections: yes
toc: yes
toc_depth: '2'
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.width = 10)
testfun <- function() {
message("in testfun")
Sys.sleep(10)
plot(3, main = "testfun 3")
return(0)
}
```
# All test cases `decreasing.benefits = TRUE`
```{r}
for (i in 1){#c(1:7)) {
cat("====================================================================\n")
plot(1, main = "1")
print("After plot 1, before plot 2")
plot(2, main = "2")
print("After plot 2, before plot 3 (Wrong!!??, appears BEFORE plot 2)")
r <- testfun()
}
```
From the docs ?message:
message(..., domain = NULL, appendLF = TRUE)
so it seems you can only control the domain (which has to with translation) and the line ending.

Can't knit the R file that I am trying to make

So I have a document where I'm trying to knit, but I get an error where it says that
Error: object 'r' not found
Execution halted
Here is my full code:
---
title: "Test"
author: "Test Test"
date: "05.15.2022"
output:
html_document:
toc: yes
toc_float: yes
editor_options:
markdown:
wrap: sentence
---
# Header
##chunks
```{r}
summary(cars)
```
```{r}
knitr::opts_chunk$set(message = FALSE,warning = FALSE, error = FALSE)
```
```{r}
sapply(cars,class)
```
Any help? I am really new to R.

How to change the font used inside the chunks when knitting as tuffte_handout pdf?

I am working in a book, using R and the Tufte library.
I am using the following YAML for each .Rmd Chapter:
---
title: "Chapter 2: A First Linear Program"
header-includes:
\usepackage{longtable}
\usepackage{caption}
output:
tufte::tufte_handout:
citation_package: natbib
latex_engine: xelatex
highlight: monochrome
tufte::tufte_html: default
tufte::tufte_book:
citation_package: natbib
latex_engine: xelatex
---
\pagestyle{headings}
```{r setup, include=FALSE}
library(tufte)
library(tint)
library(knitr)
library(gridExtra)
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(tidy = F)
knitr::opts_chunk$set(cache = TRUE)
knitr::opts_chunk$set(width = 30)
```
And the following one is the YAML for the index .Rmd file:
---
title: "Operations Research Using R<br />"
author: "Timothy R. Anderson"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
documentclass: book
bibliography: ["Master4Bookdowns.bib"]
---
# Preface {-}
I have tried different options like adding the sansfont and mainfont in YAML:
output:
tufte::tufte_handout:
citation_package: natbib
latex_engine: xelatex
highlight: monochrome
sansfont: Calibri Light
mainfont: Calibri Light
But it didn't change the font inside the chunk, only the text outside.
Here a simpler example document showing my attempt to change monofont as suggested in the comments:
---
title: "Chapter 2: A First Linear Program"
header-includes:
\usepackage{longtable}
\usepackage{caption}
output:
tufte::tufte_handout:
citation_package: natbib
latex_engine: xelatex
toc: TRUE
number_sections: true
highlight: monochrome
monofont: Times New Roman
tufte::tufte_html: default
tufte::tufte_book:
citation_package: natbib
latex_engine: xelatex
---
\pagestyle{headings}
```{r setup, include=FALSE}
library(tufte)
library(tint)
library(knitr)
library(gridExtra)
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(tidy = F)
knitr::opts_chunk$set(cache = TRUE)
knitr::opts_chunk$set(width = 30)
```
This is an example, where we can see that text in the book is affected by the use of command
monofont: Times New Roman in the YAML. However, we can see that next chunk, the oucome when generating the tuffte_handout pdf is using a different font and also using multiple colors (no only black as in the main body).
```{r base_case_no_pipes_step_4}
model0d <- c("example", 6 + 2 <= 2000)
#fabrication
model0e <- c("example", 8 + 6 <= 2000)
#assembly
```
You have to bring the YAML headers to the right level:
monofont et al. are top-level headers
highlight is below the output format, not below output
The following example uses "Times New Roman" for the code block and XeLaTeX's default font (Latin Modern) for the main text. You can change that with mainfont. In addition, there is no syntax highlighting:
---
title: "Chapter 2: A First Linear Program"
header-includes:
- \usepackage{longtable}
- \usepackage{caption}
monofont: Times New Roman
output:
tufte::tufte_handout:
citation_package: natbib
latex_engine: xelatex
toc: TRUE
number_sections: true
highlight: monochrome
tufte::tufte_html: default
tufte::tufte_book:
citation_package: natbib
latex_engine: xelatex
---
\pagestyle{headings}
```{r setup, include=FALSE}
library(tufte)
library(tint)
library(knitr)
library(gridExtra)
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_chunk$set(tidy = F)
knitr::opts_chunk$set(cache = TRUE)
knitr::opts_chunk$set(width = 30)
```
This is an example, where we can see that text in the book is affected by the use of command
monofont: Times New Roman in the YAML. However, we can see that next chunk, the oucome when generating the tuffte_handout pdf is using a different font and also using multiple colors (no only black as in the main body).
```{r base_case_no_pipes_step_4}
model0d <- c("example", 6 + 2 <= 2000)
#fabrication
model0e <- c("example", 8 + 6 <= 2000)
#assembly
```
Result:

How can I control fontsize and linestretch of code chunks independently from the main text in bookdown?

Using bookdown to output a .pdf document the YAML within the index.Rmd looks like this currently:
---
title: "My title"
author:
- 'me'
output:
bookdown::pdf_document2:
includes:
in_header: latex/preamble.tex
keep_tex: yes
site: bookdown::bookdown_site
documentclass: book
geometry: "left=3.5cm, right=2.5cm, top=2.5cm, bottom=2.5cm"
fontsize: 12pt
linestretch: 1.5
bibliography: [packages.bib, referencias.bib]
linkcolor: NavyBlue
biblio-style: apalike
link-citations: yes
toc-depth: 2
lof: True
lot: True
---
How can I control fontsize and linestretch of code chunks independently from the main text? This answer provides a solution to control font size, but not line spacing.
It's is the same idea as here but now we just alter the source hook:
```{r setup, include=FALSE}
def.source.hook <- knitr::knit_hooks$get("source")
knitr::knit_hooks$set(source = function(x, options) {
x <- def.source.hook(x, options) # apply default source hook
ifelse(!is.null(options$linestretch), # if linestretch is not NULL, apply linestretch
paste0("\\linespread{", options$linestretch,"}\n", x, "\n\n\\linespread{1}"), # reset linestretch after the chunk!
x)
})
```
Now you can copy and paste the ifelse statement from the other answer into this hook as well and you can control both.
Full example:
---
title: "Linestretch"
date: "20 December 2018"
header-includes:
- \usepackage{lipsum}
output:
bookdown::pdf_document2:
keep_tex: true
linestretch: "`r (lstr <- 1.5)`"
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(eval = F)
def.source.hook <- knitr::knit_hooks$get("source")
knitr::knit_hooks$set(source = function(x, options) {
x <- def.source.hook(x, options)
x <- ifelse(!is.null(options$linestretch),
paste0("\\linespread{", options$linestretch,"}\n", x, "\n\n\\linespread{", lstr,"}"),
x)
ifelse(!is.null(options$size),
paste0("\\", options$size,"\n\n", x, "\n\n \\normalsize"),
x)
})
```
## R Markdown
\lipsum[30]
```{r, linestretch = 1, size="Large"}
head(mtcars)
head(mtcars)
```
\lipsum[30]

Resources