reddit / emacs

R Code Block in Org-Mode Doesn't Export the Resulting LaTeX Table to PDF

cddesjar · 2022-10-02 11:30
I am running the following R code in a code block in org-mode. #+begin_src R :session :results output latex :exports results library(ggplot2) library(xtable) x <- rnorm(1000) g0 <- ggplot(as.data.frame(x), aes(x)) + geom_histogram(); g0 hist_dat <- ggplot_build(g0)$data[[1]] hist_dat <- hist_dat |> select(xmin, xmax, count) print(xtable(hist_dat, digits = c(0, 1, 1, 0)), include.rownames = FALSE) #+end_src This creates the expected LaTeX code: \begin{table}[ht] \centering \begin{tabular}{rrr} \hline xmin & xmax & count \\ \hline -3.3 & -3.1 & 1 \\ -3.1 & -2.9 & 1 \\ -2.9 & -2.6 & 0 \\ -2.6 & -2.4 & 5 \\ -2.4 & -2.2 & 7 \\ -2.2 & -1.9 & 14 \\ -1.9 & -1.7 & 11 \\ -1.7 & -1.5 & 20 \\ -1.5 & -1.3 & 35 \\ -1.3 & -1.0 & 49 \\ -1.0 & -0.8 & 64 \\ -0.8 & -0.6 & 67 \\ -0.6 & -0.3 & 108 \\ -0.3 & -0.1 & 90 \\ -0.1 & 0.1 & 98 \\ 0.1 & 0.3 & 85 \\ 0.3 & 0.6 & 84 \\ 0.6 & 0.8 & 62 \\ 0.8 & 1.0 & 59 \\ 1.0 & 1.3 & 35 \\ 1.3 & 1.5 & 32 \\ 1.5 & 1.7 & 22 \\ 1.7 & 1.9 & 25 \\ 1.9 & 2.2 & 13 \\ 2.2 & 2.4 & 10 \\ 2.4 & 2.6 & 1 \\ 2.6 & 2.9 & 1 \\ 2.9 & 3.1 & 0 \\ 3.1 & 3.3 & 0 \\ 3.3 & 3.6 & 1 \\ \hline \end{tabular} \end{table} However, when I run org-export-dispatch and select export to LaTeX and As PDF file and open, I get nothing in the PDF (neither code nor the results). I wonder what I'm doing wrong.
scbagley · 2022-10-02 14:06
There are a couple of things going on. 1. You need `library(dplyr)` to get the `select` function defined. Maybe you do this somewhere else. 2. R is putting messages in the output, which include the `_` character, which is not in math mode, which breaks the latex formatting. Add the argument `geom_histogram(bins = 30)` to fix this. 3. Also, put `#+LATEX_HEADER: \usepackage{float}` at the top. How to debug this kind of problem: look in the `.log` file produced by latex. There is almost always useful information that can help narrow down the problem.
cddesjar · 2022-10-02 16:07
That doesn't solve the problem. The org code block is not writing the LaTeX code to the .tex file, so the resulting PDF shows nothing.
cddesjar · 2022-10-02 16:15
Okay, I figured out the problem it was something I had in my `.Rprofile` to fix tibbles with ESS.