1

我想读取一个包含多张工作表的 excel 文件并为每张工作表指定 col_types,以下是我的代码:

data_file <- "./input/data.xlsx"

sheet_names <- c("Sales", "Customs", "Departments")

col_type_list <- list(
  Sales = c(rep("text", 3), rep("numeric", 2)),
  Customs = rep("text", 5),
  Departments = rep("text", 4)
)

DList <- purrr::map2(.x = sheet_names, .y = col_type_list,
  .f = read_xlsx(
    path = data_file,
    sheet = .x,
    col_types = .y
  )
)

执行上面的代码,得到如下错误信息:

Error in standardise_sheet(sheet, range, sheets_fun(path)) : 
  object '.x' not found

如何将正确的 .x 和 .y 放入代码中?

4

1 回答 1

1

你做对了,但是在调用 read_xlsx 之前你错过了波浪号。

这是添加了波浪号(〜)并且运行顺利:)

data_file <- "./input/data.xlsx"

sheet_names <- c("Sales", "Customs", "Departments")

col_type_list <- list(
  Sales = c(rep("text", 3), rep("numeric", 2)),
  Customs = rep("text", 5),
  Departments = rep("text", 4)
)

DList <- purrr::map2(.x = sheet_names, .y = col_type_list,
                     .f = ~read_xlsx(
                       path = data_file,
                       sheet = .x,
                       col_types = .y
                      )
)
于 2018-02-11T00:38:37.897 回答