*注意,此问题仅在Windows上发生。
我有以下代码,这些代码在正常脚本或控制台上正常运行:
tdir <- tempdir()
stateurl <- "https://www2.census.gov/geo/tiger/GENZ2018/shp/cb_2018_us_state_500k.zip"
if(file.exists(paste(tdir,"/cb_2018_us_state_500k.shp",sep=""))==F){
download.file(stateurl, destfile = file.path(tdir, "States.zip"))
unzip(file.path(tdir,"States.zip"),exdir=tdir)}但是,当将相同的脚本放在块中,并试图在Rmarkdown中编织到HTML时,我会收到警告:“无法打开URL连接”。
我不知道为什么像下载文件这样简单的东西会在控制台中运行,而不是在RMarkdown中运行。
发布于 2022-02-26 10:43:40
在没有明显模式的情况下,我可以在所提供的代码中重现大约50%的错误(例如,重复运行同一会话中的“针织到HTML”将随机失败/工作)。
对我来说,如果我显式地将method = "libcurl"指定为download.file的参数(而不是默认的method = "auto",它在download.file上使用"wininet“),问题就解决了。
tdir <- tempdir()
stateurl <- "https://www2.census.gov/geo/tiger/GENZ2018/shp/cb_2018_us_state_500k.zip"
if(file.exists(paste(tdir,"/cb_2018_us_state_500k.shp",sep=""))==F){
download.file(stateurl, destfile = file.path(tdir, "States.zip"), method = "libcurl")
unzip(file.path(tdir,"States.zip"),exdir=tdir)}有了这个“与HTML相结合”,它就能持续地工作(至少对于我的10+测试来说是这样)。
https://stackoverflow.com/questions/71271210
复制相似问题