2

I am generating several tables in R and then exporting them to HTML(5). For instance:

#+BEGIN_SRC R :session *R-org* :results output raw :exports results
  df <- data.frame(x=0.110, y=0.132)
  rownames(df) <- "Rates"
  print(ascii(df, header=TRUE, colnames=colnames(df), rownames=rownames(df),
              digits=3, caption="Rates"),
        type="org")
#+END_SRC

This seems to work well enough, but it creates very ugly tables. How do I assign HTML attributes (as shown with the #+ATTR_HTML line in org manual) from the R block?

  • Where does the ascii() function come from? – Tyler Sep 30 '16 at 19:06
  • found it, the ascii package, as it turns out. – Tyler Sep 30 '16 at 19:08
  • It's a function from the R ascii package that prints raw org-mode formatted tables (among other formats). For other examples see this page. – Jason Morgan Sep 30 '16 at 19:09
1

You can handle this from the R side:

* Test this

** Original
#+BEGIN_SRC R :session *R-org* :results output raw :exports results
  library(ascii)
  df <- data.frame(x=0.110, y=0.132)
  rownames(df) <- "Rates"
  print(ascii(df, header=TRUE, colnames=colnames(df), rownames=rownames(df),
              digits=3, caption="Rates"),
        type="org")
#+END_SRC


** Formatted
#+BEGIN_SRC R :session *R-org* :results output raw :exports results
  library(ascii)
  df <- data.frame(x=0.110, y=0.132)
  rownames(df) <- "Rates"
  cat("#+ATTR_HTML: :border 2 :rules all :frame border\n")
  print(ascii(df, header=TRUE, colnames=colnames(df), rownames=rownames(df),
              digits=3, caption="Rates"),
        type="org")
#+END_SRC

That's a bit ugly. It would be neater if the author of the ascii provided a header argument that allowed you to explicitly add an org-mode header in the call to ascii. (they do provide a header argument, but it serves another purpose). That may be a reasonable feature request.

  • That is ugly, but it works. I will submit a feature request. Thanks! – Jason Morgan Sep 30 '16 at 19:31
  • I missed a \n in the cat() statement that is important! Edited to correct – Tyler Sep 30 '16 at 19:31
  • Alternatively, it would be easy to write your own wrapper function in R that would bundle the cat() statement and the ascii output for you. – Tyler Sep 30 '16 at 19:32
  • That is what I am thinking. Isolate the ugly stuff. – Jason Morgan Sep 30 '16 at 19:33
0

Simple HTML exporting R tables from Org-mode

Perhaps you can try :results output value , :colnames yes :rownames yes.

#+BEGIN_SRC R :session *R-org* :results output value :exports results :colnames yes :rownames yes
df <- data.frame(x=0.110, y=0.132)
rownames(df) <- "Rates"
df
#+END_SRC

#+RESULTS:
|       |    x |     y |
|-------+------+-------|
| Rates | 0.11 | 0.132 |


HTML Exporting with Captions and Labels

If you need captions or reference labels,

#+BEGIN_SRC R :session *R-org* :results output value :exports results :colnames yes :rownames yes
df <- data.frame(x=0.110, y=0.132)
rownames(df) <- "Rates"
df
#+END_SRC

#+CAPTION: Rates
#+NAME: TABLE-TMP-01
#+ATTR_HTML: :border 2 :rules all :frame border
#+RESULTS:
|       |    x |     y |
|-------+------+-------|
| Rates | 0.11 | 0.132 |


Now we have a reference for Table [[TABLE-TMP-01]].

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.