0

My data exists as a tabbed spreadsheet, and I'm trying to write a script to import it.

library(readxl)
oput <- 0

tabnames <- excel_sheets("dataset.xlsx")

for(x in seq_along(tabnames)){
    assign(tabnames[x], read_excel("dataset.xlsx", sheet = tabnames[x], col_names = TRUE)
}

This works, giving me multiple datasheets in the environment:

  • tab1
  • tab2 ...

What I would like to do is have these outputs as items in a list:

>oput
$tab1
[1] data1
$tab2
[1] data2
...

But I can't get this working properly

assign(oput[[x]],  read_excel("dataset.xlsx", sheet = tabnames[x], col_names = TRUE)

and

assign(oput$x,  read_excel("dataset.xlsx", sheet = tabnames[x], col_names = TRUE)

both give:

Error in assign(oput[[x]], read_excel("dataset.xlsx",  : 
invalid first argument

It's obviously an error on my part in identifying the sheetname variable.

What's the correct way of doing this, please?

Trent
  • 55
  • 1
  • 8
  • did you try `lapply()`? – Bryan Goggin Jun 06 '16 at 21:21
  • a couple of issues with that: - 1) how would i pass the arguments from lapply() to the read_excel command? - ie `lapply(oput, read_excel("datasheet.xlsx", sheet = ???, col_names = TRUE))` and 2) i'm trying to dynamically generate the sheet; my plan is to lapply() transformations to the data once I've gotten them in there. – Trent Jun 06 '16 at 22:42

1 Answers1

0

Found previously on SO with some slightly different search terms. Apologies for the duplicate post.

How to read all worksheets in an Excel Workbook into an R list with data.frame elements using XLConnect?

Trent
  • 55
  • 1
  • 8