Hi, I'm trying to read several Excel sheets from an Excel file into a list. I'm using read.xls from package 'gdata'. I would like to know how I can check the number of sheets before the loop (in the example below) so that I could adjust the loop counter? Any suggestions? DF.list <- list() for (i in 1:4) { DF.list[[i]] <- read.xls("sample_file.xls", sheet=i, stringsAsFactors = FALSE) } Thanks, -L
Try this: library(RDCOMClient) xl <- COMCreate("Excel.Application") xl$Workbooks()$Open("teste.xls")$Sheets()$Count() On Thu, Jul 2, 2009 at 11:22 AM, Lauri Nikkinen <lauri.nikkinen@iki.fi>wrote:> Hi, > > I''m trying to read several Excel sheets from an Excel file into a > list. I''m using > read.xls from package ''gdata''. I would like to know how I can > check the number of sheets before the loop (in the example below) so > that I could adjust the loop counter? Any suggestions? > > DF.list <- list() > for (i in 1:4) { > DF.list[[i]] <- read.xls("sample_file.xls", sheet=i, > stringsAsFactors = FALSE) > } > > Thanks, > -L > > ______________________________________________ > R-help@r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25'' 40" S 49° 16'' 22" O [[alternative HTML version deleted]]
If you are on Windows and have Excel on the same machine then the code here: http://tolstoy.newcastle.edu.au/R/e6/help/09/03/7736.html will return the number of worksheets as well as a vector of the worksheet names. It seems that the email has somehow caused some of the lines to wrap so you will have to correct that. There is also some info on various methods of reading excel spreadsheets here: http://wiki.r-project.org/rwiki/doku.php?id=tips:data-io:ms_windows On Thu, Jul 2, 2009 at 10:22 AM, Lauri Nikkinen<lauri.nikkinen at iki.fi> wrote:> Hi, > > I'm trying to read several Excel sheets from an Excel file into a > list. I'm using > read.xls from package 'gdata'. I would like to know how I can > check the number of sheets before the loop (in the example below) so > that I could adjust the loop counter? Any suggestions? > > DF.list <- list() > for (i in 1:4) { > ? ?DF.list[[i]] <- read.xls("sample_file.xls", sheet=i, > stringsAsFactors = FALSE) > ? ?} > > Thanks, > -L > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >