Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 函数用于将excel工作表导入列表并将工作表名称添加到列表中_R_Excel_Function_Readxl - Fatal编程技术网

R 函数用于将excel工作表导入列表并将工作表名称添加到列表中

R 函数用于将excel工作表导入列表并将工作表名称添加到列表中,r,excel,function,readxl,R,Excel,Function,Readxl,我编写了这个简短的函数,用于将excel文件中的所有工作表导入列表中的数据框,并向列表项添加名称: read_full_excel <- function(x){ # Imports the name of the sheets in the excel file and creates a list of data frames: sheet_names <- readxl::excel_sheets(x) # Create list and add each ex

我编写了这个简短的函数,用于将excel文件中的所有工作表导入列表中的数据框,并向列表项添加名称:

read_full_excel <- function(x){ 

# Imports the name of the sheets in the excel file and creates a list of data frames:  
  sheet_names <- readxl::excel_sheets(x) 

# Create list and add each excel sheet as a data frame by a loop:
  sheets <- list()
  for(i in 1:length(sheet_names)){
    sheets[[i]] <-  readxl::read_xlsx(x, sheet = sheet_names[i])
  }

# Add the sheet names to the list: 
  names(sheets) <- sheet_names

  return(sheets)

}

read\u full\u excel这是Windows的问题,而不是Linux的问题(我想MacOS也没有)。您需要提供excel文件的完整路径

# This is not working
> read_full_excel("TEST.xslx")
 Error: `path` does not exist: ‘TEST.xslx’ 

# This is OK
> read_full_excel("C:\\Users\\marcelo\\Documents\\TEST.xlsx")

# you can use your function interactively
read_full_excel(file.choose())

# or add this optionally in your function
read_full_excel <- function(x = NULL){ 

# Imports the name of the sheets in the excel file and creates a vector:  
  if (is.null(x) {
      x <- file.choose()
   } 
   sheet_names <- readxl::excel_sheets(x)

   # continue the function code as before ........ 

}

# usage
read_full_excel()
# or 
read_full_excel("C:\\my_full_path_to_excel_file") 

#这不起作用
>阅读完整的excel(“TEST.xslx”)
错误:`path`不存在:'TEST.xslx'
#这没关系
>阅读完整的excel(“C:\\Users\\marcelo\\Documents\\TEST.xlsx”)
#您可以交互地使用您的函数
读取\u full\u excel(file.choose())
#或者在函数中添加此选项

read_full_excel刚刚在excel工作表上试用过,您的功能对我有效。是一个类似的答案,基本上使用相同的方法,但是
lappy
而不是
for
循环。这很奇怪,它对我不起作用。你的链接似乎就是我想要的答案。我确实试图找出
lappy
方法来实现这一点,但我不知道如何
lappy
将名称列表添加到
read\u xlsx
的条件参数中。尽管如此,我仍然无法理解语法:
x在lappy或sapply中,函数(x)应用于向量表的每个元素,这相当于为(I In 1:length(sheets))
和apply
函数(sheets[I])
编写循环
。如果函数需要更多的参数,可以在
函数(X){}
之后添加。奇怪的是,它对我不起作用。我尝试了你的解决方案,因为一开始我正试图做类似的事情。但是,我仍然无法将工作表名称应用于列表。我看到您添加了一个变量
s_name
,我在任何地方都看不到它。我认为这是一个输入错误,所以当我重新编写函数时,我用
sheet\u name
替换了它。我还想知道你为什么用
sapply
而不是
lappy
?这有什么好处吗?s_name是sappy中匿名函数的一个变量,可以,Sheet_name通过s_name应用到匿名函数中。我使用sappy而不是lappy,因为sappy具有逻辑参数USE.NAMES,它用各自的表名命名列表中的每个元素。当然,您可以使用Lappy,然后运行
名称(工作表)我更新了以前的答案,在Windows中,您需要使用
sapply
提供excel fileNice解决方案的完整路径!
> zz<-read_full_excel("./Documentos/test.xlsx")
> names(zz)
[1] "Hoja1" "Hoja2" "Hoja3" "Hoja4"
read_full_excel <- function(x){ 

  # Imports the name of the sheets in the excel file and creates a vector:  
  sheet_names <- readxl::excel_sheets(x) 

  # Alternative equivalent function using sapply. Note the inverted order 
  # of arguments x ans sheet_names for the internal function
  # See sapply parameter USE.NAMES = TRUE
  sapply(
    sheet_names, 
    function(s_name, x) { readxl::read_xlsx(path = x, sheet = s_name) },
    x, 
    USE.NAMES = TRUE
    )
}