How can I include highlighted R code chunks in r-exams? - r-exams

I have tried to create an exercise that includes formatted r-code.
Question
= = = = = = = =
You want to load the tidyverse package. Please complete the following r code.
```r
_______(tidyverse)
```
Now the respective environments shaded, etc. are missing from rmarkdown.
I have tried using the header argument in the exams2nops function. But this will only include strings in the document body. How can I edit the preamble of the the output pdf?
Thanks :)

I found a solution. I have created a inject.tex file that I pass to the header argument after scanning it. The file contains the following
\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
\usepackage{framed}
\definecolor{shadecolor}{RGB}{248,248,248}
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{#1}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\ImportTok}[1]{#1}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{#1}}}
\newcommand{\BuiltInTok}[1]{#1}
\newcommand{\ExtensionTok}[1]{#1}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.77,0.63,0.00}{#1}}
\newcommand{\RegionMarkerTok}[1]{#1}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{0.94,0.16,0.16}{#1}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.64,0.00,0.00}{\textbf{#1}}}
\newcommand{\NormalTok}[1]{#1}

Related

Using R/Markdown fails inside learnr question

Motivation: I want to write an interface that uses questions from the R package exams in learnr questions/quizzes. In R/exams each question is either an R/Markdown (Rmd) or R/LaTeX (Rnw) file with a certain structure specifying question, solution, and further meta-information. The questions can contain R code to make them dynamic, e.g., sampling numbers or certain text building blocks etc. Hence, the workflow is that first the questions are run through knitr::knit or utils::Sweave and then embedded in a suitable output format.
Problem: When I rmarkdown::run("learnr+rexams.Rmd") a learnr tutorial that dynamically produces a question or quiz from an Rmd exercise I get the error:
Error in if (grepl(not_valid_char_regex, label)) { :
argument is of length zero
The code for a simple reproducible example learnr+rexams.Rmd is included below.
The reason for the error appears to be that learnr runs a function verify_tutorial_chunk_label() that tries to assure the the learnr R chunk labels are well formatted. However, confusion is caused by the chunks that are run by the R/exams package, unnecessarily leading to the error above.
Workarounds: I can disable the verify_tutorial_chunk_label() in the learnr namespace and then everything works well. Or I can use Rnw instead of Rmd exercises and then learnr does not conflict with Sweave(). Also, when I run my code outside of a learnr tutorial it works fine.
Question: Can I do anything less invasive to make exams cooperate with learnr? For example, setting some appropriate knitr options or something like that?
Example: This is the source for the minimal learnr tutorial learnr+rexams.Rmd that replicates the problem. Note that everything is very much simplified and only works for certain R/exams exercises, here using the function exercise template that ships with R/exams.
---
title: "learnr & R/exams"
output: learnr::tutorial
runtime: shiny_prerendered
---
```{r exams2learnr, include = FALSE}
exams2learnr <- function(file) {
x <- exams::xexams(file)[[1]][[1]]
x <- list(text = x$question, type = "learnr_text",
learnr::answer(x$metainfo$solution, correct = TRUE))
do.call(learnr::question, x)
}
## assignInNamespace("verify_tutorial_chunk_label", function() return(), ns = "learnr")
```
```{r rfunctions, echo = FALSE, message = FALSE}
exams2learnr("function.Rmd")
```
Running this tutorial (as noted above) replicates the error. To avoid it I can either uncomment the assignInNamespace() call or alternatively replace "function.Rmd" by "function.Rnw".
The problem is that by the time learnr::question() is called, knitr is no longer able to find the chunk label for the chunk where exams2learnr() was called. You can get around this by setting the current chunk label before calling do.call(learnr_question, x):
exams2learnr <- function(file, label = knitr::opts_current$get("label")) {
force(label)
x <- exams::xexams(file)[[1]][[1]]
x <- list(
text = x$question,
type = "learnr_text",
learnr::answer(x$metainfo$solution, correct = TRUE)
)
knitr::opts_current$set(label = label)
do.call(learnr::question, x)
}
This also lets you set the label dynamically if you want, which becomes the ID of the question in learnr.

Why figures are not being pulled into pdf from R markdown

I'm using RStudio and knitr to create reproducable PDF reports on work. However, figures are not pulled into the document - instead there is "figure/unnamed-chunk-" where the image should be.
Images are produced and saved to 'home/figure/'.
The code I use to create the PDF is:
Rfile = "/Users/user/Documents/folder/file.R"
setwd(dirname(Rfile))
spin(Rfile, format = 'Rmd', report=F)
render(paste(substring(Rfile,0,(nchar(file)-1)),"md",sep=""), pdf_document(toc = TRUE, toc_depth=6, number_sections= TRUE),
output_file = paste(substring(file,0,(nchar(file)-2)),".pdf",sep=""))
In the md file, there is a line for each figure that is
figure/unnamed-chunk-X-X.pdf
I've tried adding the lines below after reading the answers at https://groups.google.com/forum/#!topic/knitr/_sw4sAtLkoQ - but they don't make a difference.
opts_knit$set(base.dir = dirname(file))
opts_knit$set(fig.path = '/figure/')
I'm sure there is a simple fix to this but I can't see what it might be.

Source code from Rmd file within another Rmd

I'm attempting to make my code more modular: data loading and cleaning in one script, analysis in another, etc. If I were using R scripts, this would be a simple matter of calling source on data_setup.R inside analysis.R, but I'd like to document the decisions I'm making in an Rmarkdown document for both data setup and analysis. So I'm trying to write some sort of source_rmd function that will allow me to source the code from data_setup.Rmd into analysis.Rmd.
What I've tried so far:
The answer to How to source R Markdown file like `source('myfile.r')`? doesn't work if there are any repeated chunk names (a problem since the chunk named setup has special behavior in Rstudio's notebook handling). How to combine two RMarkdown (.Rmd) files into a single output? wants to combine entire documents, not just the code from one, and also requires unique chunk names. I've tried using knit_expand as recommended in Generate Dynamic R Markdown Blocks, but I have to name chunks with variables in double curly-braces, and I'd really like a way to make this easy for my colaborators to use as well. And using knit_child as recommended in How to nest knit calls to fix duplicate chunk label errors? still gives me duplicate label errors.
After some further searching, I've found a solution. There is a package option in knitr that can be set to change the behavior for handling duplicate chunks, appending a number after their label rather than failing with an error. See https://github.com/yihui/knitr/issues/957.
To set this option, use options(knitr.duplicate.label = 'allow').
For the sake of completeness, the full code for the function I've written is
source_rmd <- function(file, local = FALSE, ...){
options(knitr.duplicate.label = 'allow')
tempR <- tempfile(tmpdir = ".", fileext = ".R")
on.exit(unlink(tempR))
knitr::purl(file, output=tempR, quiet = TRUE)
envir <- globalenv()
source(tempR, local = envir, ...)
}

Non-valid bibtex from knitr write_bib

I'm trying to get knitr to create a bibtex file that includes all the packages that I am using as shown (and answered) in this question. That part is working fine but a single R package, foreach, results in non-valid bibtex code.
Here is a minimal .Rnw example that produces the error
\documentclass[11pt]{article}
\usepackage{natbib}
\begin{document}
This is a test
<<>>=
library(foreach)
# Write the bibtex file:
write_bib("foreach", file="test.bib")
#
\nocite{*}
\bibliographystyle{apalike}
\bibliography{test}
\end{document}
kniting this file produces this test.bib file
#Manual{R-foreach,
title = {foreach: Provides Foreach Looping Construct for R},
author = {{Revolution Analytics} and Steve Weston}},
year = {2015},
note = {R package version 1.4.3},
url = {https://CRAN.R-project.org/package=foreach},
}
Note that the author line has two curly braces towards the end of the author line so using this bibtex file without removing the curly brace results in a bunch of problems. I've only encountered this problem with the foreach package.
Can anyone tell me what I can do to prevent the error (currently I have a one-line sed code to remove the extra brace)?
The citation("foreach") output looks fine and dandy, so I'm guessing the error may be deep down in utils:::toBibtex.bibentry but I haven't been able to figure it out.

knitr chunks within .bib file

I am using the LaTeX package problems to create solution sets from a database of problems. The database is structured like a bibliography database, in a .bib file. The whole system works beautifully for regular LaTeX, but now some of my solutions have R code (in knitr chunks) in them.
The default sequence of knitting/TeXing/BibTeXing in RStudio is not working-- the code ends up in the document verbatim, along with mangled versions of the chunk delimiters. So, I need to find the right workflow of steps to ensure that the code makes it through.
This package seems to be very set on having two files, one for the database and one for the .tex/.rnw, so I can't make a working example, but something like this:
\documentclass{article}
\usepackage[solution]{problems}
\Database{
#QUESTION{1.1,
problem = {1.1},
solution = {This solution only uses TeX, like $\bar{x}$. It works fine.}}
#QUESTION{1.2,
problem = {1.2},
solution = {This solution includes code
<<>>=
head(iris)
#
It does not work.
}}}
\begin{document}
Problems for this week were 1.1 and 1.2
\problems{1.1}
\problem{1.2}
\end{document}
You will have to knit the .bib file first and then run LaTeX and BibTeX.
While you usually have a .Rnw file that is knitted to .tex and then let LaTeX tools process the .tex and the .bib file you will have to start with a (let's call it) .Rbib file that is knitted to .bib and then processed by LaTeX.
For simplicity, I give the file I called .Rbib above the name bibliography.Rnw but you can choose any extension you like. I chose .Rnw because the syntax used inside is the same as in .Rnw files.
As dummy entries for the bib-file I use data from verbosus.com and added some knitr code.
The first chunk sets global chunk options to prevent knitr from adding the code of the chunks or any markup to the output file. The next chunk shows how for example the title field could be filled with generated content and the \Sexpr{} part is an example how this could be used to add some dynamic text.
<<setup>>=
library(knitr)
opts_knit$set(
echo = FALSE,
results = "asis"
)
#
article{article,
author = {Peter Adams},
title = {
<<echo=FALSE, results = "asis">>=
cat("The title of the work")
#
},
journal = {"The title of the journal"},
year = 1993,
number = 2,
pages = {201-213},
month = 7,
note = {An optional note},
volume = 4
}
#book{book,
author = {Peter Babington},
title = {\Sexpr{"The title of the work"},
publisher = {The name of the publisher},
year = 1993,
volume = 4,
series = 10,
address = {The address},
edition = 3,
month = 7,
note = {An optional note},
isbn = {3257227892}
}
It is important to have the chunk option results = "asis" and to use cat() instead of print(). Otherwise, there would be unwanted characters in the output.
If this is saved as bibliography.Rnw the following is enough to get a .bib file with the chunks evaluated:
knit(input = "bibliography.Rnw", output = "bibliography.bib")
After that only standard LaTeX compilation remains.

Resources