【问题标题】:Self-defined function doesn't output data to environment? [closed]自定义函数不向环境输出数据? [关闭]
【发布时间】: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 &lt;- 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") 这显示了环境中的数据。
  • 我忘了分配输出 - 粗心的错误。谢谢!

标签: r function readxl


【解决方案1】:

就像我在 cmets 中提到的,您忘记分配输出。

object <- 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")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多