【发布时间】:2018-08-09 09:01:19
【问题描述】:
我正在尝试创建一个小函数来帮助从在线托管的 excel 电子表格中读取数据。
read2014 <- function(urlhere, filename){
url <- urlhere
destfile <- filename
curl::curl_download(url, destfile)
filename <- read_excel(destfile, skip = 14)
}
当我尝试使用这些参数值调用函数时,什么都没有发生。
read2014(urlhere = "https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2013/04/Beds-Open-Overnight-Web_File-Q1-2014-15-Revised-Nov15-Final-21447.xlsx", filename = "X2014_Q1")
但是,当我在不使用函数的情况下调用正文时,它将电子表格作为数据框输入到环境中没有问题。
url <- "https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2013/04/Beds-Open-Overnight-Web_File-Q1-2014-15-Revised-Nov15-Final-21447.xlsx"
destfile <- "X2014_Q1.xlsx"
curl::curl_download(url, destfile)
X2014_Q1 <- read_excel(destfile, skip = 14)
我以前从未编写过函数,所以我不知道如何解决这个问题。
【问题讨论】:
-
在函数的最后使用
return(filename),这会将文件名返回给您的环境。 -
@burton030 我试过了,它显示在控制台中,但不显示在环境中。
-
test <- read2014(urlhere = "https://www.england.nhs.uk/statistics/wp-content/uploads/sites/2/2013/04/Beds-Open-Overnight-Web_File-Q1-2014-15-Revised-Nov15-Final-21447.xlsx", filename = "X2014_Q1")这显示了环境中的数据。 -
我忘了分配输出 - 粗心的错误。谢谢!