8

I want to execute R code blocks in org-mode and export the results to a PDF/html.

Here is an example:

Some text here...

#+begin_src R :results output latex
library(xtable)
x <- rnorm(100)
y <- x + rnorm(100)
xtable(summary(lm(y ~ x)))
#+end_src

I want to export the results (here, the output of xtable(summary(lm(y ~ x)))), but I can't figure out how to do it. When I export it to LaTeX or html, the text and code is exported nicely, but no table is produced in the output pdf / html file. This is the core problem I need help with. How do I get the results to export, and not just the code?

I have tried several experiments but not sure what settings I need to change.

  • If I execute the code inline (using C-c C-c), I am asked to allow for execution, and when allowed a beautiful LaTeX table is generated below the code as expected. Not sure why this is not happening when exporting to LaTeX or html.

  • I know that code is executing: If I modify the switches as below and then export to LaTeX/html, an output file is produced with appropriate name, but no results are integrated into resultant LaTeX pdf or html file. The syntax coloring is still present.

    #+begin_src R :results output latex :session *myR* :dir "." :file r-output.org
    library(xtable)
    x <- rnorm(100)
    y <- x + rnorm(100)
    xtable(summary(lm(y ~ x)))
    #+end_src
    
Dan
  • 32,584
  • 6
  • 98
  • 168
Shambho
  • 457
  • 3
  • 13
  • 1
    I've tried to clarify what I think it is that you're trying to ask. You can roll back the changes if I misunderstood your intent. – Dan Jan 31 '17 at 22:51
  • @Dan: Many thanks. This is certainly more readable. I'll try to keep these in mind in future. Much appreciated. – Shambho Jan 31 '17 at 22:54

1 Answers1

15

You need to use the :exports both header argument. By default, org only exports R code, not the results. From the org manual:

14.8.2.8 :exports

The :exports header argument is to specify if that part of the Org file is exported to, say, HTML or LaTeX formats. Note that :exports affects only ‘src’ code blocks and not inline code.

code The default. The body of code is included into the exported file.
Example: :exports code.

results The results of evaluation of the code is included in the exported file.
Example: :exports results.

both Both the code and results of evaluation are included in the exported file.
Example: :exports both.

none Neither the code nor the results of evaluation is included in the exported file. Whether the code is evaluated at all depends on other options.
Example: :exports none.

js.
  • 105
  • 4
Tyler
  • 21,719
  • 1
  • 52
  • 92
  • Thanks. This works for the text output but does not export images. – Shambho Jan 31 '17 at 23:49
  • 2
    You may need to add a `:results graphics` header argument for plots. See http://orgmode.org/worg/org-contrib/babel/languages/ob-doc-R.html#orgheadline4 – Tyler Feb 01 '17 at 00:17