knitr

Hook to time knitr chunks

天涯浪子 提交于 2019-12-30 18:05:11
问题 I would like to time knitr chunks and record how long it took to render them using comments in LaTeX output. I've tried the following hook: now = Sys.time() knit_hooks$set(timeit = function(before) { if (before) { now <<- Sys.time() } else { paste("%", sprintf("Chunk rendering time: %s seconds.\n", round(Sys.time() - now, digits = 3))) } }) And it does produce the correct comment with timing but the problem is that it's wrapped in kframe which results in ugly gaps in the LaTeX output: \begin

How to nest knit calls to fix duplicate chunk label errors?

自古美人都是妖i 提交于 2019-12-30 10:35:39
问题 I'm running into a duplicate label error when I call a function that uses knit inside a knit call. If I label the chunks the problem goes away. Is there a way to call some_function in a way that doesn't collide with the parent knit call? library(knitr) some_function <- function(){ knit(text =" ```{r } 1 ``` ") } cat(knit(text =" ```{r } some_function() ``` ```{r } some_function() ``` ")) Output: ```r some_function() ``` ``` ## Error: duplicate label 'unnamed-chunk-1' ``` 回答1: You can use knit

knitr and plotting neural networks

让人想犯罪 __ 提交于 2019-12-30 09:00:26
问题 I'm trying to plot some neural network outputs, but I'm not getting any result. Plotting normal stuff like plot(iris) works fine, but there's something about the neuralnetwork() object that doesn't seem to plot the same way. My file looks like: --- title: "stack" author: "stack" date: "today" output: pdf_document: default html_document: default --- ```{r} library(neuralnet) AND <- c(rep(0,3),1) binary.data <- data.frame(expand.grid(c(0,1), c(0,1)), AND) net <- neuralnet(AND~Var1+Var2, binary

I can't generate \label{fig:mwe-plot} with knitr

雨燕双飞 提交于 2019-12-30 08:17:10
问题 I am having trouble generating \label{} for plots when using knitr to go from a *.Rmd file to a *.md file via knitr ; and then converting to *.pdf via pandoc . An MWE for my *.Rmd is included below: ```{r Setup, include=FALSE, results="hide", warning=FALSE} opts_chunk$set(dev="cairo_pdf", fig.lp="fig:", echo=FALSE, results="hide", message=FALSE, warning=FALSE) ``` ```{r mwe-plot, fig.cap = "MWE plot."} library(ggplot2) ggplot(mtcars, aes(factor(cyl))) + geom_bar() ``` I knit: knit("mwe.Rmd")

Plot several pictures with different size in a “for” loop

送分小仙女□ 提交于 2019-12-30 07:10:08
问题 I am new to knitr and markdown and this is my first question asked. Maybe this question has a simple answer that I just can't find. I have a for-loop, which creates 3 ggplots. The loop runs 300 to 400 times depending on the data input. I want to define the size of these 3 pictures as: 1st picture: width = 7, height = 3 2nd picture: width = 7, height = 3 3nd picture: width = 7, height = 12 So far I am using the following code: ```{r calc, echo=FALSE, warning=FALSE, message=FALSE, results='asis

knitr - exclude chunks from `purl(…)`?

人走茶凉 提交于 2019-12-30 03:47:32
问题 When I purl/tangle a document to extract the R chunks into a script, is there any way to: exclude an arbitrary chunk (by name say)? if not, exclude a chunk if eval=F (or perhaps I can define a chunk hook/option include=F )? For example, suppose I have the following Rmd: ```{r setup, echo=F} library(MASS) ``` First, we perform the setup (assume for some reason I need to evaluate `setup` silently before I wish to display the chunk to the user, hence the repetition) ```{r setup, eval=F} ``` Here

knitr: can I cite an article in a figure caption using the fig.cap chunk option?

末鹿安然 提交于 2019-12-30 03:17:39
问题 I'd like to cite an article in my figure caption. I've tried using the Rmarkdown/pandoc [@citekey] and the latex \\citep{citekey} forms in the fig.cap chunk option without any luck. Here is a reproducible example: --- output: rmarkdown::tufte_handout references: - id: Nobody06 title: 'My Article' author: - family: Nobody given: Jr issued: year: 2006 --- Some text [@Nobody06]. ```{r figure, fig.cap="A figure [@Nobody06]"} library(ggplot2) qplot(1:10, rnorm(10)) ``` # References This produces

Highlighting bash code with knitr / rmarkdown

蓝咒 提交于 2019-12-30 03:13:49
问题 I'm trying to generate an HTML report using RStudio, R Markdown and knitr. In the report I would like to display some bash code. I do not want to run the code but I would like it to be highlighted. It has been mentioned in another question but the suggestion there doesn't work for me. Here's what I've tried so far: --- title: "bash highlighting?" output: html_document --- ```{r, engine = 'bash', eval = FALSE} for foo in (ls bar) do echo $foo done ``` ```{bash, eval = FALSE} for foo in (ls bar

Adding a line break to code blocks in R Markdown

送分小仙女□ 提交于 2019-12-30 03:01:24
问题 I am using the knitr package with R Markdown to create an HTML report. I am having some trouble keeping my code on separate lines when using '+'. For example, ```{r} ggplot2(mydata, aes(x, y)) + geom_point() ``` will return the following the the HTML document ggplot2(mydata, aes(x, y)) + geom_point() Normally this is fine, but the problem arises once I start adding additional lines, which I want to keep separate to make the code easier to follow. Running the following: ```{r} ggplot2(mydata,

knitr: Use an inline expression in fig.cap chunk option

一笑奈何 提交于 2019-12-30 02:11:06
问题 My question is about the knitr option fig.cap, when using LaTeX. Is it possible to include an \rinline or \Sexpr in the fig.cap character string? For example, I would like to have something like (I'm using an .Rtex file): \documentclass{article} \begin{document} %% begin.rcode fig.cap="x is \\rinline{x}" % x <- 5 % p <- seq(0,5) % q <- x*p % plot(p,q) %% end.rcode \end{document} I'd really like for that chunk to produce a plot in my .tex document, with a caption reading "x is 5". Instead, it