如何使用 R 以编程方式将多个表包含到 Sweave 文档中

发布于 2024-08-30 19:12:39 字数 2161 浏览 11 评论 0原文

我想要一个 sweave 文档,其中包含可变数量的表。我认为下面的示例可以工作,但事实并非如此。我想循环列表 foo 并打印每个元素,因为它是自己的表。

% 
\documentclass[a4paper]{article}
\usepackage[OT1]{fontenc}
\usepackage{longtable}
\usepackage{geometry}
\usepackage{Sweave}
\geometry{left=1.25in, right=1.25in, top=1in, bottom=1in}
\listfiles
\begin{document}

<<label=start, echo=FALSE, include=FALSE>>=
startt<-proc.time()[3]
library(RODBC)
library(psych)
library(xtable)
library(plyr)
library(ggplot2)
options(width=80)

#Produce some example data, here I'm creating some dummy dataframes and putting them in a list
foo<-list()
foo[[1]]<-data.frame(GRP=c(rep("AA",10), rep("Aa",10), rep("aa",10)), X1=rnorm(30), X2=rnorm(30,5,2))
foo[[2]]<-data.frame(GRP=c(rep("BB",10), rep("bB",10), rep("BB",10)), X1=rnorm(30), X2=rnorm(30,5,2))
foo[[3]]<-data.frame(GRP=c(rep("CC",12), rep("cc",18)), X1=rnorm(30), X2=rnorm(30,5,2))
foo[[4]]<-data.frame(GRP=c(rep("DD",10), rep("Dd",10), rep("dd",10)), X1=rnorm(30), X2=rnorm(30,5,2))
@ 

\title{Docuemnt to test putting a variable number of tables into a sweave Document}
\author{"Paul Hurley"}
\maketitle

\section{Text}

This document was created on \today, with \Sexpr{print(version$version.string)} running
 on a \Sexpr{print(version$platform)} platform. It took approx \input{time} sec to process.

<<label=test, echo=FALSE, results=tex>>= 
cat("Foo")
@ 
that was a test, so is this
<<label=table1test, echo=FALSE, results=tex>>=
print(xtable(foo[[1]]))
@ 
\newpage

\subsection{Tables}

<<label=Tables, echo=FALSE, results=tex>>=
for(i in seq(foo)){
    cat("\n")
    cat(paste("Table_",i,sep=""))
    cat("\n")
    print(xtable(foo[[i]]))
    cat("\n")
    }
#cat("<<label=endofTables>>= ")
@ 


<<label=bye, include=FALSE, echo=FALSE>>= 
endt<-proc.time()[3]
elapsedtime<-as.numeric(endt-startt)
@ 
<<label=elapsed, include=FALSE, echo=FALSE>>=
fileConn<-file("time.tex", "wt") 
writeLines(as.character(elapsedtime), fileConn) 
close(fileConn) 
@ 

\end{document}

在这里,table1test块按预期工作,并根据foo[[1]]中的数据帧生成了一个表,但是循环仅生成Table(下划线)1....

I want to have a sweave document that will include a variable number of tables in. I thought the example below would work, but it doesn't. I want to loop over the list foo and print each element as it's own table.

% 
\documentclass[a4paper]{article}
\usepackage[OT1]{fontenc}
\usepackage{longtable}
\usepackage{geometry}
\usepackage{Sweave}
\geometry{left=1.25in, right=1.25in, top=1in, bottom=1in}
\listfiles
\begin{document}

<<label=start, echo=FALSE, include=FALSE>>=
startt<-proc.time()[3]
library(RODBC)
library(psych)
library(xtable)
library(plyr)
library(ggplot2)
options(width=80)

#Produce some example data, here I'm creating some dummy dataframes and putting them in a list
foo<-list()
foo[[1]]<-data.frame(GRP=c(rep("AA",10), rep("Aa",10), rep("aa",10)), X1=rnorm(30), X2=rnorm(30,5,2))
foo[[2]]<-data.frame(GRP=c(rep("BB",10), rep("bB",10), rep("BB",10)), X1=rnorm(30), X2=rnorm(30,5,2))
foo[[3]]<-data.frame(GRP=c(rep("CC",12), rep("cc",18)), X1=rnorm(30), X2=rnorm(30,5,2))
foo[[4]]<-data.frame(GRP=c(rep("DD",10), rep("Dd",10), rep("dd",10)), X1=rnorm(30), X2=rnorm(30,5,2))
@ 

\title{Docuemnt to test putting a variable number of tables into a sweave Document}
\author{"Paul Hurley"}
\maketitle

\section{Text}

This document was created on \today, with \Sexpr{print(version$version.string)} running
 on a \Sexpr{print(version$platform)} platform. It took approx \input{time} sec to process.

<<label=test, echo=FALSE, results=tex>>= 
cat("Foo")
@ 
that was a test, so is this
<<label=table1test, echo=FALSE, results=tex>>=
print(xtable(foo[[1]]))
@ 
\newpage

\subsection{Tables}

<<label=Tables, echo=FALSE, results=tex>>=
for(i in seq(foo)){
    cat("\n")
    cat(paste("Table_",i,sep=""))
    cat("\n")
    print(xtable(foo[[i]]))
    cat("\n")
    }
#cat("<<label=endofTables>>= ")
@ 


<<label=bye, include=FALSE, echo=FALSE>>= 
endt<-proc.time()[3]
elapsedtime<-as.numeric(endt-startt)
@ 
<<label=elapsed, include=FALSE, echo=FALSE>>=
fileConn<-file("time.tex", "wt") 
writeLines(as.character(elapsedtime), fileConn) 
close(fileConn) 
@ 

\end{document}

Here, the table1test chunk works as expected, and produced a table based on the dataframe in foo[[1]], however the loop only produces Table(underscore)1....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

來不及說愛妳 2024-09-06 19:12:39

这是由该语句中的下划线引起的:

cat(paste("Table_",i,sep=""))

如果将其更改为

cat(paste("Table ",i,sep=""))

Or

cat(paste("Table\\textunderscore",i,sep=""))

It running。您想要这些数字作为下标吗?

This is being caused by the underscore in this statement:

cat(paste("Table_",i,sep=""))

If you change it to

cat(paste("Table ",i,sep=""))

Or

cat(paste("Table\\textunderscore",i,sep=""))

It runs. Did you want those numbers as subscripts?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文