Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 760081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:40:26+00:00 2026-05-14T15:40:26+00:00

I want to have a sweave document that will include a variable number of

  • 0

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….

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:40:27+00:00Added an answer on May 14, 2026 at 3:40 pm

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 380k
  • Answers 380k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Would a hashtable/hashmap use a lot of memory if it… May 14, 2026 at 9:49 pm
  • Editorial Team
    Editorial Team added an answer Update 2015/06 As jQuery has evolved since the question was… May 14, 2026 at 9:49 pm
  • Editorial Team
    Editorial Team added an answer makecert can be used, Client certificate http://msdn.microsoft.com/en-us/library/ff650751.aspx Server certificate http://msdn.microsoft.com/en-us/library/ms751408.aspx May 14, 2026 at 9:49 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.