我一直在尝试将管道函数与 normal 函数连接,并将返回值设置为新列。我要尝试的是向函数发送参数,然后继续链接操作
这是我要与 purrr 一起运行的功能:
fileDownloader<- function(url, path, log_attr){
url <- as.character(url)
path <- as.character(path)
log_attr <- as.character(log_attr)
res <- 0
if (log_attr == "a"){
text<-paste("Function invoked at ", as.character(Sys.time()), sep=" ")
print(text)
write.csv(text, file = "log.csv")
}
if(!file.exists(path)){
res <- tryCatch({
archivo <- curl_download(url=url, destfile=path)
res <-1
}
,error=function(e){res <- -1}
)
}
res
}
我正在尝试传递此数据框
my_dataframe <- data.frame(
url=c("www.google.com", "yahoo.com", "bing.com", NA),
file_name=c("mi_file1.txt", "mi_file2.txt", "mi_file3.txt", NA),
attributes=c("a", "", "", NA),
another_col=c("xxx", "yyy", "zzz", "111")
)
这是实际的管道:
my_dataframe %>%
filter(!is.na(url)) %>%
# mutate(file_status = pmap(list(.$url, .$file_name, .$attributes), .f=fileDownloader) )
# prints 212/321/131
# mutate(file_status = map(.$url, path=.$file_name, log_attr=.$attributes, .f=fileDownloader))
# works only with the first element
我设法通过 {walk} 使它“正常工作”,但是它什么也没有返回。
有人知道我在做什么错吗?
谢谢
答案 0 :(得分:0)
为您完成这项工作:
my_dataframe %>%
filter(!is.na(url)) %>%
mutate(file_status = pmap_chr(list(.$url, .$file_name, .$attributes), .f = fileDownloader))