How to import multiple excel tabs at the same time?

4

I'm trying to reproduce the code below, but to no avail. What should I put in the path argument? Is it the same in all of them? Some clearer example so I can understand? Any tips on how to import multiple excel sheets at the same time? Thanks!

# To load all sheets in a workbook:

path <- readxl_example("datasets.xls")
lapply(excel_sheets(path), read_excel, path = path)

###
In: https://cran.r-project.org/web/packages/readxl/readxl.pdf
    
asked by anonymous 12.12.2017 / 20:22

1 answer

4

The first thing to do is to install the package readxl , using the

install.packages("readxl")

This step only needs to be done once. With the readxl package installed, you need to load it into memory through the

library(readxl)

This step needs to be done every time R (or RStudio) is closed and then reopened.

The path argument is the path to the .xls file on your computer. For example, when I run path <- readxl_example("datasets.xls") on my PC, the result is

path
[1] "/Library/Frameworks/R.framework/Versions/3.4/Resources/library/readxl/
  extdata/datasets.xls"

Probably the result on your computer will be different, but realize that it will also have .xls , as this is the address of the readxl sample dataset file. To read all the sheets in an .xls file, run the

planilhas <- lapply(excel_sheets(path), read_excel, path = path)
length(planilhas)

The first line will read all the spreadsheets within an .xls file and save them within an object called planilhas . The second command will tell you how many spreadsheets were in this file. Now just run

planilhas[2]

For example, to see what data is present in worksheet 2 of the .xls file read.

Editing after this comment :

You can create a different date frame for each element of the planilhas list via the

for (j in 1:length(planilhas)){
  assign(paste("planilha", j, sep=""), planilhas[[j]])
}
    
13.12.2017 / 00:40