Warning in knitr - r

I'm using knitr 0.5 for my analysis and it throws this warning
Warning in parse_params(params) :
(*) NOTE: I saw options " label = TrtScores-SD-Response, echo = FALSE, results = asis"
are you using the old Sweave syntax? go http://yihui.name/knitr/options
for this chunck
<< label = TrtScores-SD-Response, echo = FALSE, results = asis >>=
R code
#
and knitr 0.5 becomes very slow. Any idea to overcome this warning. Thanks

You just need to add quotes around the results argument, like this:
<< label = TrtScores-SD-Response, echo = FALSE, results = "asis" >>=
R code
#
As documented at http://yihui.name/knitr/options (the web page to which the error message thoughtfully directed you):
All option values, except the chunk label, must be valid R expressions just like how we write function arguments. For example, options that take character values must be quoted as you do in R (e.g. should write fig.path="abc" instead of fig.path=abc, and out.width='\textwidth' instead of out.width=\textwidth)
And then later on the same page:
results: ('markup'; character) takes three possible values
See also the "Transition from Sweave to knitr" page, which addresses this and other differences between Sweave and knitr. Like all of Yihui's documentation, it's excellently put together.

Related

kable all tables in markdown report [duplicate]

Using knitr and R Markdown, I can produce a tabularised output from a matrix using the following command:
```{r results='asis'}
kable(head(x))
```
However, I’m searching for a way to make the kable code implicit since I don’t want to clutter the echoed code with it. Essentially, I want this:
```{r table=TRUE}
head(x)
```
… to yield a formatted tabular (rather than the normal output='markdown') output.
I actually thought this must be pretty straightforward since it’s a pretty obvious requirement, but I cannot find any way to achieve this, either via the documentation or on the web.
My approach to create an output hook fails because once the data arrives at the hook, it’s already formatted and no longer the raw data. Even when specifying results='asis', the hook obtains the output as a character string and not as a matrix. Here’s what I’ve tried:
default_output_hook <- knit_hooks$get('output')
knit_hooks$set(output = function (x, options)
if (! is.null(options$table))
kable(x)
else
default_output_hook(x, options)
)
But like I said, this fails since x is not the original matrix but rather a character string, and it doesn’t matter which value for the results option I specify.
Nowadays one can set df_print in the YAML header:
---
output:
html_document:
df_print: kable
---
```{r}
head(iris)
```
I think other answers are from a time when the following didn't work, but now we can just do :
```{r results='asis', render=pander::pander}
head(x)
```
Or set this for all chunks in the setup chunk, for instance :
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, render=pander::pander)
```
Lacking a better solution I’m currently re-parsing the character string representation that I receive in the hook. I’m posting it here since it kind of works. However, parsing a data frame’s string representation is never perfect. I haven’t tried the following with anything but my own data and I fully expect it to break on some common use-cases.
reparse <- function (data, comment, ...) {
# Remove leading comments
data <- gsub(sprintf('(^|\n)%s ', comment), '\\1', data)
# Read into data frame
read.table(text = data, header = TRUE, ...)
}
default_output_hook <- knit_hooks$get('output')
knit_hooks$set(output = function (x, options)
if (is.null(options$table))
default_output_hook(x, options)
else {
extra_opts <- if (is.list(options$table)) options$table else list()
paste(kable(do.call(reparse, c(x, options$comment, extra_opts))),
collapse = '\n')
}
)
This will break if the R markdown comment option is set to a character sequence containing a regular expression special char (e.g. *), because R doesn’t seem to have an obvious means of escaping a regular expression.
Here’s a usage example:
```{r table=TRUE}
data.frame(A=1:3, B=4:6)
```
You can pass extra arguments to the deparse function. This is necessary e.g. when the table contains NA values because read.table by default interprets them as strings:
```{r table=list(colClasses=c('numeric', 'numeric'))}
data.frame(A=c(1, 2, NA, 3), B=c(4:6, NA))
```
Far from perfect, but at least it works (for many cases).
Not exactly what you are looking for, but I am posting an answer here (that could not fit in a comment) as your described workflow is really similar to what my initial goal and use-case was when I started to work on my pander package. Although I really like the bunch of chunk options that are available in knitr, I wanted to have an engine that makes creating documents really easy, automatic and without any needed tweaks. I am aware of the fact that knitr hooks are really powerful, but I just wanted to set a few things in my Rprofile and let the literate programming tool its job without further trouble, that ended to be Pandoc.brew for me.
The main idea is to specify a few options (what markdown flavour are you using, what's your decimal mark, favorite colors for your charts etc), then simply write your report in a brew syntax without any chunk options, and the results of your code would be automatically transformed to markdown. Then convert that to pdf/docx/odt etc. with Pandoc.

Resources for knitr chunks

I've been trying to find this on the web but haven't had any luck. I'm working on creating a report with R using knitr and was wondering if anyone knows of a good resource for all options involving <<>>=. I've seen some examples like <<setup, include=FALSE, cache=FALSE>>= but I don't know what these mean and would like to know what else I can do.
To let you close the question - everything is here: http://yihui.name/knitr/options/#chunk_options, I will just paste the most important (in my opinion) options below:
Code Evaluation
eval: (TRUE; logical) whether to evaluate the code chunk; it can also be a numeric vector to select which R expression(s) to evaluate, e.g. eval=c(1, 3, 4) or eval=-(4:5)
Text Results
echo: (TRUE; logical or numeric) whether to include R source code in the output file; besides TRUE/FALSE which completely turns on/off the source code, we can also use a numeric vector to select which R expression(s) to echo in a chunk, e.g. echo=2:3 means only echo the 2nd and 3rd expressions, and echo=-4 means to exclude the 4th expression
results: ('markup'; character) takes these possible values
markup: mark up the results using the output hook, e.g. put results in a special LaTeX
asis: output as-is, i.e., write raw results from R into the output document
hold: hold all the output pieces and push them to the end of a chunk
hide hide results; this option only applies to normal R output (not warnings, messages or errors)
collapse: (FALSE; logical; applies to Markdown output only) whether to, if possible, collapse all the source and output blocks from one code chunk into a single block (by default, they are written to separate <pre></pre> blocks)
warning: (TRUE; logical) whether to preserve warnings (produced by warning()) in the output like we run R code in a terminal (if FALSE, all warnings will be printed in the console instead of the output document); it can also take numeric values as indices to select a subset of warnings to include in the output
error: (TRUE; logical) whether to preserve errors (from stop()); by default, the evaluation will not stop even in case of errors!! if we want R to stop on errors, we need to set this option to FALSE
message: (TRUE; logical) whether to preserve messages emitted by message() (similar to warning)
include: (TRUE; logical) whether to include the chunk output in the final output document; if include=FALSE, nothing will be written into the output document, but the code is still evaluated and plot files are generated if there are any plots in the chunk, so you can manually insert figures; note this is the only chunk option that is not cached, i.e., changing it will not invalidate the cache.
Cache
cache: (FALSE; logical) whether to cache a code chunk; when evaluating code chunks, the cached chunks are skipped, but the objects created in these chunks are (lazy-) loaded from previously saved databases (.rdb and .rdx) files, and these files are saved when a chunk is evaluated for the first time, or when cached files are not found (e.g. you may have removed them by hand)
Plots
fig.path: ('figure/'; character) prefix to be used for figure filenames (fig.path and chunk labels are concatenated to make filenames); it may contain a directory like figure/prefix- (will be created if it does not exist); this path is relative to the current working directory
fig.width, fig.height: (both are 7; numeric) width and height of the plot, to be used in the graphics device (in inches) and have to be numeric
dev: ('pdf' for LaTeX output and 'png' for HTML/markdown; character) the function name which will be used as a graphical device to record plots
An examplary chunk:
```{r global_options, include = FALSE}
knitr::opts_chunk$set(fig.width = 9, fig.height = 4, fig.path = "Figs/", dev = "svg",
echo = FALSE, warning = FALSE, message = FALSE,
cache = FALSE, tidy = FALSE, size = "small")
```

A Way in Knitr to Copy a Chunk?

Knitr Mavens,
Background: Using knitr to report a report with many embedded graphs. In the body of the report, all that's appropriate is the graph, not the code.
For example:
```{r graph_XYZ_subset, echo = FALSE, message = TRUE,
fig.cap = "Text that explains the graph"}
graph.subset <- ggplot() + ...
```
This part works just fine.
However, there is a need to display the key parts of the code (e.g., key statistical analyses and key graph generations)...but in an Addendum.
Which leads to this question: is there a way to copy a knitr chunk from the early parts of a script to a later part?
To ensure accuracy, it's ideal that the code in the Addendum list (display) all the code that was actually executed in the report.
For example:
# ADDENDUM - Code Snippets
### Code to Generate Subset Graph
\\SOMEHOW COPY the code from graph_XYZ_subset to here without executing it.
### Code to Compute the Mean of Means of the NN Factors
\\Copy another knitr chunk which computes the mean of means, etc.
### And So On...
\\Copy chunks till done
* * * * * * * *
Any ideas? Is there a way in knitr to perform these types of chunk copies?
There are several options, four of them listet and shortly explained below. Yihui's explanations in How to reuse chunks might also help.
\documentclass{article}
\begin{document}
\section{Output}
<<mychunk, echo = FALSE>>=
print("Hello World!")
#
\section{Source code}
Option 1: Use an empty chunk with the same label.
<<mychunk, eval = FALSE>>=
#
Option 2: Embed in other chunk (no advantage in this case). Note that there is no equality sign and no at for the inner chunk.
<<myOtherChunk, eval = FALSE>>=
<<mychunk>>
#
Option 3: Use \texttt{ref.label}.
<<ref.label = "mychunk", eval = FALSE>>=
#
Option 4: Define the chunk in an external file you read in using \texttt{read\_chunk}. Then use Option 1--3 to execute the chunk (with \texttt{eval = TRUE}; default) or show it's code (with \texttt{eval = FALSE}).
\end{document}
I usually prefer Option 4. This allows you to separate the programming logic from writing the document.
At the place mychunk is to be exectued and the graph will appear in the PDF, you only have <<mychunk>>= in your Rnw file and don't have to bother with all the code that generates your graph. Developing your code is also easier, because in an interactive session you have all your code at one spot and don't have to scroll through all the text of the report when going from one chunk to the next one.
EDIT:
The options mentioned above have in common that you need to manually maintain a list of the chunks to show in the appendix. Here two options to avoid this; unfortunately, both have some drawbacks:
Option 1: Automatically create a list of all chunks that have been executed and show their code.
This can be achieved using a chunk hook that registers all chunk names. Include the following chunk before all other chunks in the document:
<<echo = FALSE>>=
library(knitr)
myChunkList <- c()
listChunks <- function(before, options, envir) {
if (before) {
myChunkList <<- c(myChunkList, options$label)
}
return(NULL)
}
knit_hooks$set(recordLabel = listChunks) # register the new hook
opts_chunk$set(recordLabel = TRUE) # enable the new hook by default
#
Where you want to show the code (for example in the appendix), insert the following chunk:
<<showCode, ref.label = unique(myChunkList), eval = FALSE>>=
#
Unfortunately, there will be no margin or any other visual separation between the chunks.
Option 2: Using the chunk hook is not always necessary because there is the function all_labels() that returns a list of all chunk labels. However, there might be chunks in your file that don't get executed and you probably don't want to see their code. Moreover, option 1 allows skipping certain chunks simply by setting recordLabel = FALSE in their chunk options.

Knit function that returns dot (graphviz) code as string

Using pure dot inside a knitr chunk with engine = "dot" is straightforward, but you have to write the dot code yourself.
<<r dot-ex, engine = "dot", echo=FALSE>>=
digraph test123 {
A -> B
}
#
I want a function to do it for me.
dotFun <- function() {
dotCode <- 'digraph test123 {
A -> B
}'
return(dotCode)
}
and then call this inside a knit chunk similar to a function that returns LaTeX code and knit with result = 'as.is'
<<r dot-ex, engine = "dot">>=
cat(dotFun())
#
but this results in :Error in (knit_engines$get(options$engine))(options):
setting chunk option results = 'asis' yields the same error message.
Is there a way to do this?
It is not possible with the current version of knitr (v1.5), but will be possible in the next version (v1.6), which has not been released yet. If you use the development version on Github, you can actually assign the source code to a code chunk via the code option, e.g.
<<dot-ex, engine = "dot", code = dotFun()>>=
#
More on this in the news for v1.6.
Any particular reason this has to be evaluated within a knitr chunk with that engine? Why not do it directly from R with some system calls? You can write the command to a file, and use system to call dot on that file, and read the results back into R.
This is, in fact, what knitr does. You can probably pretty easily take the knitr dot engine and wrap it into a function of your own -- see
https://github.com/yihui/knitr/blob/master/R/engine.R#L144.

R - Handling cut intervals of form [x,y] in tables when converting to LaTeX

I'm working on a document in R, with knitr to pdflatex and am using the extended version of toLatex from memisc.
When I'm producing a table with cut intervals however, the square brackets are not sanitised and the pdflatex job errors because of the existence of [.
I tried putting sanitize=TRUE in the knitr chunk code, but this only works for tikz.
Previously, I have used gsub and replaced the string in the R object itself which is rather inelegant. I'm hoping someone could point me in the direction of a nuance of memisc or knitr that I'm missing or another function/method that would easily handle latex special characters.
Example
library("memisc")
library("Hmisc")
example<-data.frame(cbind(x=1:100,y=1:100))
example$x<-cut2(example$x,m=20)
toLatex(example)
UPDATE
Searching SO I found a post about applying latexTranslate with apply function, but this requires characters so I would have to unclass from factor to character.
I found another SO post that identifies the knitr:::escape_latex function however, the chunk then outputs the stuff as markup instead of translating it (using results='asis') or produces an R style table inside a code block (using results='markup'). I tried configuring it as a hook function in my parent document and it had the effect of outputting all the document contents as markup. This is a brand new area for me so I probably implemented it incorrectly.
<<setup,include=FALSE>>=
hook_inline = knit_hooks$get('inline')
knit_hooks$set(inline = function(x) {
if (is.character(x)) x = knitr:::escape_latex(x)
hook_inline(x)
})
#
...
<<tab-example,echo=FALSE,cache=TRUE,results='asis',sanitize=TRUE,inline=TRUE>>=
library("Hmisc")
library("memisc")
example<-data.frame(cbind(x=1:100,y=1:100))
example$x<-cut2(example$x,m=20)
toLatex(example)
#
According to #yihui this is the wrong way to go
UPDATE 2
I have created a gsub wrapper which will escape percentages etc, however the [ symbol still pushes latex into maths mode and errors.
Courtesy of folks on the tex SE, a [ directly after a line break(\\) is considered an entry into math-mode. It is very simple to prevent this behaviour by adding {} into the output just before a [. My function looks like:
escapedLatex<-function (df = NULL)
{
require("memisc")
gsub(gsub(x = toLatex(df, show.xvar = TRUE), pattern = "%",
replacement = "\\%", fixed = TRUE), pattern = "[", replacement = "{}[",
fixed = TRUE)
}
I'd be very happy to see any alternative, more elegant solutions around and will leave it open for a few days.

Resources