Вопрос

I'm trying to use knitr to inject R code and its output in pandoc/markdown documents. But I do not get knitr to inject the R output. I have tried decorating the R chunks with r and with{r}. Both doesn't work. Here my sample setup (with ```r):

First I show the command I issue, then I list the two files subsequently used by this command.

Here the command for the shell:

$ r CMD BATCH knitme.R

Content of knitme.R:

library("knitr")
pandoc("foo.md")

Content of foo.md:

# My knitr test

```r
1+1
```

Did this print *the result* as well?

Here a graph:

```r

plot(1:10)
```

And where is the graph?

After I ran the command I do get, as expected a new file foo.html. Here its content:

<h1 id="my-knitr-test">My knitr test</h1>
<pre class="sourceCode r"><code class="sourceCode r"><span class="dv">1+1</span></code></pre>
<p>Did this print <em>the result</em> as well?</p>
<p>Here a graph:</p>
<pre class="sourceCode r"><code class="sourceCode r">
<span class="kw">plot</span>(<span class="dv">1</span>:<span class="dv">10</span>)</code></pre>
<p>And where is the graph?</p>

This result shows that pandoc converted the input file foo.md, *but knitr did not inject the Output of the execeutes R code.

What do I miss? any help appreciated!

Это было полезно?

Решение

You should first call knit() on an R Markdown (*.Rmd) document, which produces a Markdown (*.md) document, and that is when you can run pandoc().

library(knitr)
knit('foo.Rmd')
pandoc('foo.md')

The R scripts in examples 084 and 088 as mentioned on the flaky website have illustrated how. Please also take a look at the Rmd documents to learn the syntax for R code chunks in R Markdown. If you still have 5 minutes, watch the video on the homepage, and I think all the confusion should be gone.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top