Possible to use knitr cache chunk in interactive rmarkdown doc? - r

I've noticed that when I have an Rmd with runtime: shiny in the YAML, code chunks don't seem to be read from cache. I'm wondering if using the shiny engine for rmarkdown just doesn't support chunk caching, or am I doing something wrong?
Example Rmd file:
---
title: "Cache test"
output: html_document
---
```{r cache=TRUE}
Sys.sleep(10)
```
If you run this 5 times, only the first time will take 10 seconds, and any subsequent run will be fast.
But if you add the runtime: shiny option to the YAML, then every single run will take 10 seconds.
(PS question: any better way to test whether or not code chunks cache is being used?)

i ran into the same problem where, in runtime: shiny, the cache switch was ignored.
Nowadays there is a workaround, using runtime: shiny_prerendered and context="data" with cache=TRUE:
---
title: "Cache test"
output: html_document
runtime: shiny_prerendered
---
```{r,context="data", cache=TRUE}
Sys.sleep(10)
```
this behaves as expected; on the first run, rendering takes 10 seconds; on all subsequent runs, the cached chunk is used.

Related

Rmarkdown beamer_presentation: "missing \item" LaTex error if an R chunk is included and fancybox used

I want to use the LaTeX \ovalbox{} command from the fancybox package in an Rmarkdown beamer_presentation, knitted from within RStudio. However, as soon as I add a simple R chunk, I get an "! LaTeX Error: Something's wrong--perhaps a missing \item."
This is a minimum reproducible example:
---
output:
beamer_presentation
header-includes: |
\usepackage{fancybox}
---
## Slide 1
\ovalbox{an oval box}
```{r}
print("x")
```
I can hand-solve the problem afterwards in the .tex file by moving the \usepackage{fancybox} command before the \usepackage{fancyvrb} command which is automatically inserted during the knitting.
Any suggestions how to avoid this problem in the first place?
Seems the packages are fighting over how to mess with verbatim environments.
Normally one could simply load them in a different order, but of course rmarkdown makes such a simple task a lot more complicate. You can make your document compile using this hack (don't try to use any of the verbatim commands from fancybox, this might explode...):
---
output:
beamer_presentation
header-includes: |
\let\VerbatimEnvironmentsave\VerbatimEnvironment
\usepackage{fancybox}
\let\VerbatimEnvironment\VerbatimEnvironmentsave
---
## Slide 1
\ovalbox{an oval box}
```{r}
print("x")
```

rmarkdown read code from file and display with highlight

I have two RMarkdown files. main.Rmd which is the main file which is rendered as well as example.Rmd which holds a longer example and is used elsewhere (hence it lives in its own document).
I want to include example.Rmd in the main.Rmd file with code highlighting of its RMarkdown code but the code of example.Rmd does not need to be executed, as if I set eval=FALSE and copied all code into the chunk by hand.
An example MWE is
main.Rmd
---
title: This is main.rmd
output: html_document
---
```{r}
# attempt that doesnt work
cat(readLines("example.Rmd"), sep = "\n")
```
and in example.Rmd
---
title: This is example.rmd
output: html_document
---
```{r}
# code that is not executed but shown in main.Rmd
data <- ...
```
Set eval=FALSE in the example.Rmd file and then include it in main.Rmd using child chunk option.
example.Rmd
---
title: This is example.Rmd
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(eval = FALSE)
```
```{r}
# This is from example.Rmd
x <- rnorm(10)
y <- rnorm(10)
lm(y ~ x)
```
```{r}
# some comments
print("This is from example.Rmd")
```
main.Rmd
---
title: This is main.Rmd
output:
html_document:
highlight: haddock
---
```{r example, child="example.Rmd"}
```
Edit
To show full source-code of the Rmarkdown file, one possible option could be reading that Rmd file and then cat it with chunk option comment="".
Now about the syntax highlighting; there's a chunk option class.output with which it is possible to specify a language name for which pandoc supports syntax highlighting.
You can get the list of language names for which pandoc has syntax highlighting support by running the following,
pandoc --list-highlight-languages
(Note, if you don't have pandoc installed separately, you can also use the pandoc installed with Rstudio itself. Run rmarkdown::pandoc_exec() to get the pandoc executable path)
Now, the file we are trying to include actually contains not just R code, but also markdown and yaml syntaxes. So it's a kind of mixed thing and pandoc has no syntax highlighting support out of the box for this. Still I have chosen c as syntax highlighting language just to show the possibility. (Also tried r, but syntax-highlighting is not so distinctive)
---
title: This is main.Rmd
output:
html_document:
highlight: tango
---
## Rmarkdown
```{r example, echo=FALSE, class.output="c", comment=""}
cat(readLines("example.Rmd"), sep = "\n")
```
But still if you want a specific syntax-highlighting for Rmarkdown, you can actually create one. See here from the pandoc documentation itself and also this answer on SO about this.

vtree object is renderred in Rmarkdown but not in quarto

I use updated quarto and Rmarkdown and vtree ‘5.4.6’:
In the same project and same session:
Rmarkdown does what quarto does not:
Rmarkdown renders vtree(iris, "Species") and quarto not (allthough quarto renders inline)
What could be the problem for this?
See here a reproducible example:
1. Rmarkdown
---
title: "test"
author: "Me"
date: '2022-06-18'
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(vtree)
```
## R Markdown
```{r cars}
vtree(iris, "Species")
```
2. Quarto
---
title: "Untitled"
format: html
editor: visual
---
## Quarto
```{r}
library(vtree)
vtree(iris, "Species")
```
When I click the Render button: -> it is NOT renderred:
When I render inline: -> it is renderred:
Quarto works a bit differently than R Markdown as it will run R only for the knitting process, and not for the conversion to output format, here HTML.
From the error message, vtree is writing to file the output in the R session temp folder. This folder is not persistent after the R session is closed, which happens once Quarto have run all the computation output. This means that the png file does not exists anymore when converting to HTML, hence the no image shown in HTML.
(regarding knitr warning, I believe it is knitr version matter, but that does not change the output).
Looking at vtree::vtree() function, it seems when knitting to HTML is detected R temp folder is used if none provided. IMO this should ne behave that way and it is an issue in vtree package.
To overcome the issue, you need to not let vtree do the default provide yourself a path. You can do that with folder argument. So this should work
---
title: "Untitled"
format: html
keep-md: true
---
## Quarto
```{r}
library(vtree)
if (!dir.exists("vtree_save")) dir.create("vtree_save")
vtree(iris, "Species", folder = "vtree_save")
```
I would report an issue in vtree repo about this behavior.
I am the author of the vtree package. I have just made a change to the code which I believe fixes the problem. See https://github.com/nbarrowman/vtree where there are instructions for downloading version 5.5.8 of the vtree package, which includes the fix.

Can I have run and knit give the same results with external code?

Since I have some fairly large code chunks (plantuml charts, sql), I'd prefer to keep those as external files. I see in the knitr manual I can do something like code=readLines(...) to bring in outside sources.
When using external code this way, I'm seeing a difference in behavior between running the chunk in RStudio with the green play button, and knitting the code.
With these two files:
main.R:
---
title: "test"
author: "Me"
date: "6/17/2020"
output: html_document
---
```{r setup, include=FALSE}
library(here)
library(xfun)
```
```{r, code=xfun::read_utf8(here::here('test.R'))}
print('world')
```
and test.R:
print('hello')
when I knit, hello is printed. When I run the code chunks using run in RStudio, or restart R and run all chunks, then the preview says world. The print('world') here is just to illustrate that the inline code is being ran; in my case, the inline code would be empty.
I see in the comments that "Knitr will replace the code in the chunk with the code in the code option", but does that mean that separate chunks are not run by knitr by another environment? Can I do something to have the external code be executed by both rather than one executing the inline code and the other the external code?

Shiny inside the new learnr package does not work

The documentation of the new learnr-package states that
it’s also possible to add other forms of interactivity using Shiny (e.g. for teaching a statistical concept interactively).
I tried it with the provided example but I could not manage it. The slider does appear but not the plot. I used:
---
title: "Tutorial"
output:
learnr::tutorial:
progressive: true
allow_skip: true
runtime: shiny_prerendered
tutorial:
version: 0.1
---
```{r setup, include=FALSE}
library(learnr)
library(checkr)
library(shiny)
knitr::opts_chunk$set(exercise.checker = checkr::checkr_tutor)
```
The example did work with
---
title: "R Notebook"
output: html_notebook
runtime: shiny
---
I tracked the problem down: It was completely my own fault. I used the same name for the input variable in another R chunk with shiny code.
I did not get any error message, the output just did not show up.

Resources