将 R 数据框导入到 org-mode 表中

将 R 数据框导入到 org-mode 表中

我有一个 R 脚本,可以生成数据框,并将其导出到 CSV 文件。数据如下所示:

"","Variant","Xaxis","N","mean","sd","se"
"1","line1",10,5,111.11,9.33,3.11
"1","line1",20,5,112.11,9.13,3.14
"1","line1",30,5,113.11,9.43,3.10
"1","line2",10,5,101.11,8.33,2.11
"1","line2",20,5,100.11,8.13,2.12
"1","line2",30,5,108.11,8.03,2.10

我有一个正在进行的 Org-Mode 文档,我想在其中报告这些数据。我想添加一个如下所示的表格:

| Variant | X Axis | Y Axis | N | Mean | Standard Deviation | Standard Error |
|----------------------------------------------------------------------------|

未来几个月内数据会频繁变化。我希望 org-mode 能够自动读取 CSV 文件,以便即时构建我的 org-table。我希望避免每次都复制和粘贴结果。

是否有一些 org-mode 或 emacs+ESS 魔法可以用来用来自我的 CSV 文件的源数据填充我的空表?

答案1

正如 @zeroth 指出的那样,这不是 TeX 问题,而是 orgmode 问题。不过,解决方案非常简单

#+BEGIN_SRC R :colnames yes :exports results
  read.csv("foo.csv") 
#+END_SRC

答案2

我将逻辑放在了文档中,但我很确定您可以将其编码到宏中。请注意,y 轴列为空白,因为您的 R 脚本未包含它。已编辑以处理逗号分隔的数据,而不是空格分隔的数据。

Herbert 的回答如何使用 `\whiledo` 以编程方式制作表格行?对我的解决方案来说是不可或缺的。

\documentclass{article}
\usepackage{readarray}
\newcounter{index}
\usepackage{filecontents}
\begin{filecontents*}{Rscript.txt}
"","Variant","Xaxis","N","mean","sd","se"
"1","line1",10,5,111.11,9.33,3.11
"1","line1",20,5,112.11,9.13,3.14
"1","line1",30,5,113.11,9.43,3.10
"1","line2",10,5,101.11,8.33,2.11
"1","line2",20,5,100.11,8.13,2.12
"1","line2",30,5,108.11,8.03,2.10
\end{filecontents*}
\readarraysepchar{,}
  % Based on:
  % https://tex.stackexchange.com/questions/7590/
  % how-to-programmatically-make-tabular-rows-using-whiledo
  \makeatletter
  \newcounter{tabindex}
  \newtoks\@tabtoks
  \newcommand\addtabtoks[1]{%
    \@tabtoks\expandafter{\the\@tabtoks\stepcounter{tabindex}#1}}
  \newcommand*\resettabtoks{\@tabtoks{}}
  \newcommand*\synctabindex[1]{\setcounter{tabindex}{\value{#1}}}
  \newcommand*\printtabtoks{\the\@tabtoks}
  \makeatother
\begin{document}
\readdef{Rscript.txt}{\tmpa}
\readArrayij{\tmpa}{first}{\ncols}
%
\resettabtoks
\setcounter{index}{0}
\synctabindex{index}
\stepcounter{index} %WILL NEED THIS TO STEP OVER HEADER LINE
\addtabtoks{ Variant & X Axis & Y Axis & N & Mean & Standard Deviation &
Standard Error \\\hline}
\whiledo{\value{index} < \nrows}{%
  \addtocounter{index}{1}%
  \addtabtoks{%
    \arrayij{first}{\thetabindex}{2} &
    \arrayij{first}{\thetabindex}{3} &
                                     &
    \arrayij{first}{\thetabindex}{4} &
    \arrayij{first}{\thetabindex}{5} &
    \arrayij{first}{\thetabindex}{6} &
    \arrayij{first}{\thetabindex}{7}
    \ifthenelse{\equal{\thetabindex}{\nrows}}{}{\\}%
  }
}
\begin{tabular}{|c|c|c|c|c|c|c|}
  \hline
  \printtabtoks
  \\\hline
\end{tabular}
\end{document}

在此处输入图片描述

答案3

您可以使用xtableR 库并手动插入 LaTeX 文档中,或者更好的是,无需Sweave任何文本编辑器的帮助即可自动插入。

以下示例仅使用 TeXworks 生成一个文件,但配置为通过 R 进行编译而不是使用pdflatex

平均能量损失

这是代码:

% Test.Rnw 
% This this is not a tex file
%  run with Sweave (not with pdflatex) 
\documentclass{article}
\parindent0pt
\begin{document}

<<MakingDataFrame,echo=F>>=
Variant=c('line1','line1','line1','line2','line2','line2')
Xaxis=c(10.5,20.5,30.5,10.5,20.5,30.5)
N=c(5,5,5,5,5,5)
mean=c(111.11,112.11,113.11,101.11,100.11,108.11)
sd=c(9.33,9.13,9.43,8.33,8.13,8.03)
se=c(3.11,3.14,3.10,2.11,2.12,2.10)
df = data.frame(Variant,Xaxis, mean,sd,se) 
dfraw <- df
colnames(df)<- c('Variant','X axis','Mean','Standard Deviation','Standard Error') 

@

A  \LaTeX{}  table showing a simple  {\em R} data frame: 

<<BeautifulTable,echo=F,results=tex>>=
library(xtable)
xtable(df, caption='This a xtable showing a data frame')
@

This was generated with \texttt{xtable} of  {\em R} whitin a  \LaTeX{}  (noweb) document with this raw data: 

<<ShowinDataFrame,echo=F>>=
dfraw
@

Inserting this code in a  created  true \TeX document: 

<<BeautifulTableCode,echo=F>>=
xtable(df, caption='This a xtable showing a data frame')
@

\end{document}

注意:我从头开始制作数据框以避免示例中出现外部文件,但当然您可以通过read.csv在 R 块中导入现有文件来更改这一点。

相关内容