2

I'm trying to export a table from an R code block and have read Results of Evaluation (The Org Manual) which states that tables can be output with header options of :exports results :results value table.

To get the table in the correct format I'm using the R package toOrg function from orgutils.

Unfortunately it doesn't appear to work with this simple example...

#+STARTUP: align
#+TITLE: Test
#+AUTHOR: Me
#+PROPERTY: header-args:R :session *org-R*
#+PROPERTY: header-args:R :cache yes
#+PROPERTY: header-args:R :results graphics
#+PROPERTY: header-args:R :width 1024
#+PROPERTY: header-args:R :height 768
#+PROPERTY: header-args:R :tangle yes


#+begin_src R :session *org-R* :eval yes :exports none :results output silent
  library(orgutils)
  library(tidyverse)
#+end_src


#+begin_src R :session *org-R* :eval yes :exports both :results value table
  employee <- c('John Doe','Peter Gynn','Jolie Hope')
  salary <- c(21000, 23400, 26800)
  startdate <- as.Date(c('2010-11-1','2008-3-25','2007-3-14'))
  employ.data <- data.frame(employee, salary, startdate)
  employ.data %>%
    toOrg()
#+end_src

Compiling to HTML I only get the code, no table is shown in the Org buffer itself (figures are shown inline) and no table in the resulting HTML.

If I execute just the R code within an R session it prints...

> employee <- c('John Doe','Peter Gynn','Jolie Hope')
> salary <- c(21000, 23400, 26800)
> startdate <- as.Date(c('2010-11-1','2008-3-25','2007-3-14'))
> employ.data <- data.frame(employee, salary, startdate)
>  employ.data %>%
    toOrg()
| employee   | salary | startdate  |
|------------+--------+------------|
| John Doe   |  21000 | 2010-11-01 |
| Peter Gynn |  23400 | 2008-03-25 |
| Jolie Hope |  26800 | 2007-03-14 |

Really can't see what I'm doing wrong here and any pointers would be gratefully received.

  • 1
    I think you just need to actually evaluate the code block so that the result is inserted into the org file. If you then export to HTML, the table should be included. – loris Sep 30 '20 at 7:01
  • Thanks @loris for your time, @Philopolis assessment got me over the line, I was over complicating things trying to use the orgutils package. – slackline Sep 30 '20 at 7:48
  • 1
    With :exports both (or just :exports results) , the exprorter will do the evaluation of the code block during export, so it should not need to be done beforehand. OTOH, evaluating the code block is a good test that everything works before you try the export (where problems in the code evaluation might be hidden). IOW, code evaluation is not necessary with the right :exports header, but it is a good debugging technique nevertheless. – NickD Sep 30 '20 at 11:50
2

First, two remarks: your example is not perfectly reproducible (you do not import the convenient R library for the %>% operator, and there is a typo in the header of your second src block). I don't know whether this explains everything, but this might explain a part of your problem.

Not sure of why you need an external R package for such a simple example. Using the standard org mode approach, this just works:

#+begin_src R :session *org-R* :exports both :results value table :colnames yes
  employee <- c('John Doe','Peter Gynn','Jolie Hope')
  salary <- c(21000, 23400, 26800)
  startdate <- as.Date(c('2010-11-1','2008-3-25','2007-3-14'))
  employ.data <- data.frame(employee, salary, startdate)
  employ.data
#+end_src

Otherwise, if you really want to use this package, then the result-type should not be a value; it should be raw output instead. The following src block is perfectly equivalent to the previous one:

#+begin_src R :session *org-R* :exports both :results output raw :colnames yes
  employee <- c('John Doe','Peter Gynn','Jolie Hope')
  salary <- c(21000, 23400, 26800)
  startdate <- as.Date(c('2010-11-1','2008-3-25','2007-3-14'))
  employ.data <- data.frame(employee, salary, startdate)
  toOrg(employ.data)
#+end_src
  • Apologies for omitting to import one of the libraries (library(tidyverse)) added (already had an R session running with it loaded). You are right this is a simple example, I'm trying to do something more complex with R but created an example rather than posting my whole problem. Going with the standard org mode approach you've recommended works as does raw output, thank you for taking the time to help me out. – slackline Sep 30 '20 at 7:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.