【问题标题】:Prompt 'Yes' every time to getFilings每次获取文件时都提示“是”
【发布时间】:2017-06-21 19:38:21
【问题描述】:

我将使用 EDGAR 包在 R 中为几家公司下载 2005 年 10-K。我有一个迷你循环来测试哪个工作正常:

for (CIK in c(789019, 777676, 849399)){
  getFilings(2005,CIK,'10-K')
}

但是,每次运行时,我都会收到是/否提示,我必须输入“是”:

Total number of filings to be downloaded=1. Do you want to download (yes/no)? yes
Total number of filings to be downloaded=1. Do you want to download (yes/no)? yes
Total number of filings to be downloaded=1. Do you want to download (yes/no)? yes

如何提示 R 在每次运行时回答“是”?谢谢你

【问题讨论】:

    标签: r for-loop edgar


    【解决方案1】:

    请记住在您的问题中包含一个可重复的最小示例,包括library(...) 和所有其他必要的命令:

    library(edgar)
    report <- getMasterIndex(2005)
    

    我们可以通过做一些代码手术来绕过提示。在这里,我们检索getFilings 的代码,并将要求提示的行替换为仅一条消息。然后我们将新函数(my_getFilings)写入一个临时文件,然后source该文件:

    x <- capture.output(dput(edgar::getFilings))
    x <- gsub("choice <- .*", "cat(paste(msg3, '\n')); choice <- 'yes'", x)
    x <- gsub("^function", "my_getFilings <- function", x)
    writeLines(x, con = tmp <- tempfile())
    source(tmp)
    

    一切正常:

    for (CIK in c(789019, 777676, 849399)){
      my_getFilings(2005, CIK, '10-K')
    }
    list.files(file.path(getwd(), "Edgar filings"))
    # [1] "777676_10-K_2005" "789019_10-K_2005" "849399_10-K_2005"
    

    【讨论】:

    • 不错。替换该行的另一种方法是body(getFilings)[[6]][[3]][[3]][[3]][[8]] &lt;- quote(choice &lt;- "yes")。疯狂,但它有效
    • 太棒了!谢谢你。下次我也会包含库代码。
    • @RichScriven,这很好用。您能否详细说明如何识别列表中的元素?我也希望能够将这种方法用于其他功能。 (或者如果包/功能发生变化以影响精确编号)
    • 例如,对于 1.0.9 版本,正确的行似乎是 body(getFilings)[[7][[3]][[3]][[3]][[8]]
    猜你喜欢
    • 1970-01-01
    • 2018-11-15
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    • 2012-11-01
    • 1970-01-01
    • 2011-08-23
    • 1970-01-01
    相关资源
    最近更新 更多