Stem-and-Leaf from R into LaTeX

假如想象 提交于 2019-12-04 14:23:24

I am assuming that you just want to export the result to latex, am I right?

You might want to try the following Sweave code, which passed the test:

\documentclass{article}

\usepackage{listings}

\begin{document}

\begin{lstlisting}
<<echo=F, results=tex>>=
y<- c(50, 26, 31, 57, 19, 24, 22, 23, 38, 13, 50, 13, 34, 23, 30, 49, 13, 15, 51)
stem(y)
@ 
\end{lstlisting}

\end{document}

Borrowing heavily from the solution provided by @DWin:

You can capture the output from a print statement with capture.output and translate it to Latex using latexTranslate in package HMisc:

library(Hmisc)
latexTranslate(capture.output(stem(y)))

[1] ""                                                         
[2] "  The decimal point is 1 digit(s) to the right of the $|$"
[3] ""                                                         
[4] "  1 $|$ 33359"                                            
[5] "  2 $|$ 23346"                                            
[6] "  3 $|$ 0148"                                             
[7] "  4 $|$ 9"                                                
[8] "  5 $|$ 0017"                                             
[9] ""         

The stem function returns NULL. It only works via a side-effect of printing to the console device.

You could also use sink, but I'm guessing that is not much closer to your goal

sink("stem.out")
stem(y)
sink()

When I run this through the Hmisc function latex I get:

latex(readLines("stem.out")
#--------file output follows----
% latex.default(readLines("stem.out")) 
%
\begin{table}[!tbp]
 \begin{center}
 \begin{tabular}{l}\hline\hline
\multicolumn{1}{c}{}\tabularnewline
\hline
\tabularnewline
  The decimal point is 1 digit(s) to the right of the |\tabularnewline
\tabularnewline
  1 | 33359\tabularnewline
  2 | 23346\tabularnewline
  3 | 0148\tabularnewline
  4 | 9\tabularnewline
  5 | 0017\tabularnewline
\tabularnewline
\hline
\end{tabular}

\end{center}

\end{table}
#---------  end of file ------
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!