【问题标题】:curl command behaves differently on Mac and Windowscurl 命令在 Mac 和 Windows 上的行为不同
【发布时间】:2020-10-29 10:34:47
【问题描述】:

我有一些可以在 Mac 上运行的 R 代码,但其他人试图在 Windows 上运行它,但它似乎不起作用。我一直使用的代码是:

for(i in 1:length(SNPs.needproxies)){
       print(i)
       system(paste0("curl -k -X GET 'https://ldlink.nci.nih.gov/LDlinkRest/ldproxy?var=",SNPs.needproxies[i],"&pop=MXL&r2_d=r2&token=",token,"' > ", dir,"out.",SNPs.needproxies[i],".txt"))
}

如果我尝试在 Windows 上运行它,我会收到错误

https 不支持或禁用'。

我们已经缩小了问题的范围,似乎应该将地址中的单引号替换为双引号。然而,由于这是在 paste0() 函数中,它认为这是另外一回事,所以我们想用反斜杠转义双引号,即, \"https://ldlink.nci.nih.gov/LDlinkRest/ldproxy?var=",SNPs.needproxies[i],"&pop=MXL&r2_d=r2&token=",token,"\"

这种方法的工作原理是它提供了我们想要的输出,但它应该将它打印到一个名为 dir/out.file.txt 的文件中,但它不执行此部分。

SNPs.needproxies 是:

SNPs.needproxies <- c("rs709692","rs9659182","rs13064990","rs11130017","rs9832922" ,"rs36120363","rs4727815","rs7994762","rs72772387")

我有一个网站令牌,但无法共享,但它存储在名为令牌的对象中。

【问题讨论】:

  • 可能不是paste0()的结果不同,而是系统命令shell的行为不同?
  • @jogo 谢谢,是的,我确实也这么认为,但也不确定如何解决这个问题。由于报价问题,我认为它可能是 paste0(),所以无论哪种方式都欢迎建议
  • 这行得通吗? u &lt;- sprintf("https://ldlink.nci.nih.gov/LDlinkRest/ldproxy?var=%s&amp;pop=MXL&amp;r2_d=r2&amp;token=%s", "rs709692", token); system2("curl", c("-k", "-X", "GET", u))
  • 与其使用curl,不如使用R的内置download.file函数?您似乎没有使用特定于 curl 的功能,除了 -k 之外,但这不是必需的(连接 is 安全 - 至少在访问 URI 时没有API 令牌;如果提供 API 令牌会改变这一点,我会感到非常惊讶)。

标签: r libcurl


【解决方案1】:

1。带有 curl -o 选项的 system()

在注意到 Windows 上的 curl 要求 https 地址用双引号而不是单引号括起来之后,我将完全避免剩余的管道问题,并在 curl 中使用 -o 选项来指定将结果写入的文件如果你坚持写出命令:

system(paste0("curl -k -X GET \"https://ldlink.nci.nih.gov/LDlinkRest/ldproxy?var=", SNPs.needproxies[i], "&pop=MXL&r2_d=r2&token=", token, "\" -o out.", SNPs.needproxies[i], ".txt"))

2。 curl_download()

或者,R 有一些基于 curl 的包来处理所有这些细节,例如 curl:

library(curl)
url <- sprintf("https://ldlink.nci.nih.gov/LDlinkRest/ldproxy?var=%s&pop=MXL&r2_d=r2&token=%s", SNPs.needproxies[i], token)
curl_download(url, sprintf("out.%s.txt", SNPs.needproxies[i]))

3。 file.download()

您也可以按照 Konrad 的建议在这种情况下使用 file.download()

url <- sprintf("https://ldlink.nci.nih.gov/LDlinkRest/ldproxy?var=%s&pop=MXL&r2_d=r2&token=%s", SNPs.needproxies[i], token)
download.file(url, sprintf("out.%s.txt", SNPs.needproxies[i]))

4。获取()

您也可以在httr 库中使用GET()

library(httr)
u <- "https://ldlink.nci.nih.gov/LDlinkRest/ldproxy"
q <- list(var = SNPs.needproxies[i],
          pop = "MXL",
          r2_d = "r2",
          token = token)
f <- sprintf("out.%s.txt", SNPs.needproxies[i])
GET(url = u, query = q, write_disk(f))

5。 LDproxy()

注意,似乎有一个 R 包专门用于连接到此 API here。在您的情况下,代码将是:

library(LDlinkR)
LDproxy(snp = SNPs.needproxies[i],
        pop = "MXL", 
        r2d = "r2", 
        token = token, 
        file = sprintf("out.%s.txt", SNPs.needproxies[i]))

【讨论】:

    【解决方案2】:

    paste0() 本身无关,但与curl 的Windows 版本无关,这要求将https 地址包含在double- 中而不是单引号。 查看here 了解此问题的完整分解。

    【讨论】:

      【解决方案3】:

      Windows 中的标准 curl 调用是 PowerShell 的 Invoke-RestMethod 的封装版本,这就是为什么执行系统调用会导致不同的结果,具体取决于您使用的是 Linux 还是 Windows。我建议在 R 中使用 httr 包,尽管它需要翻译调用。

      【讨论】:

        猜你喜欢
        • 2016-06-22
        • 1970-01-01
        • 1970-01-01
        • 2013-10-02
        • 2021-11-14
        • 2012-08-03
        • 2019-08-10
        • 2012-11-03
        • 1970-01-01
        相关资源
        最近更新 更多