【问题标题】:Using purrr::walk to save the files - invalid 'description' argument使用 purrr::walk 保存文件 - 无效的“描述”参数
【发布时间】:2022-01-21 15:57:30
【问题描述】:

我在使用循环保存文件时遇到问题。我首先写了一个没有循环的函数。但是,它总是因循环而失败。 谁能告诉我原因?

a<-c(1,2,3)
b<-c(4,5,6)
c<-c(7,8,9)

data<-data.frame(a,b,c)
path<-list('path1/', 'path2/')

test<-function(data,path){
  write.csv(data,file = paste0(path,'result.csv'))
}

purrr::walk(path, test(data=data, path=path))
# Warning in if (file == "") file <- stdout() else if (is.character(file)) { :
#   the condition has length > 1 and only the first element will be used
# Error in file(file, ifelse(append, "a", "w")) : 
#   invalid 'description' argument

【问题讨论】:

    标签: r purrr


    【解决方案1】:

    关于condition has length 的警告是因为您传递了长度为2 的path=path,而write.csv 的file= 参数期望长度为1。

    您认为您正在使用walk 来迭代值,但您需要使用函数或~ 准函数。

    这行得通:

    purrr::walk(path, ~ test(data=data, path=.))
    

    . 也可以是 .x,它是要遍历的第一个参数中每个值的占位符:path。 “标准”功能使用将是

    purrr::walk(path, function(P) test(data=data, path=P))
    

    (也可以)。

    【讨论】:

      猜你喜欢
      • 2017-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-12
      • 2017-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多