r-传递的文件名不是字符串!(RMarkdown)



我直接从网站[此处][1]访问ncdf文件到我的RMarkdown。当我尝试使用下面代码中的nc_open函数读取文件时,我会收到错误"传递了一个不是字符串的文件名!"知道我该怎么解决这个问题吗?ps:我甚至尝试用gzcon函数解压缩文件,但当我尝试读取数据时,结果是一样的。谢谢你的帮助!卡米

library(httr)
library(ncdf4)
nc<-GET("https://crudata.uea.ac.uk/cru/data/hrg/cru_ts_4.05/cruts.2103051243.v4.05/pre/cru_ts4.05.2011.2020.pre.dat.nc.gz")
cru_nc<-nc_open(nc)

好的,这里是填充答案:

library(httr)
library(ncdf4)
library(R.utils)
url <- "https://crudata.uea.ac.uk/cru/data/hrg/cru_ts_4.05/cruts.2103051243.v4.05/pre/cru_ts4.05.2011.2020.pre.dat.nc.gz"
filename <- "/tmp/file.nc.gz"
# Download the file and store it as a temp file
download.file(url, filename, mode = "wb")
# Unzip the temp file
gunzip(filename)
# The unzipped filename drops the .gz
unzip_filename <- "/tmp/file.nc"
# You can now open the unzipped file with its **filename** rather than the object
cru_nc<-nc_open(unzip_filename)

这是一种模式吗;w";Vs模式=";wb";问题我以前在文件中遇到过这种情况。没有ncdf4的经验。

不确定是否可以通过模式=";wb";但

file.download(yourUrl,mode="wb"(

工作/帮助


编辑:

啊。另一件事是,您将对象存储为对象(nc(,但nc_open想要打开一个文件。

我认为您需要在本地保存对象(除非nc_open可以直接获取URL(,然后打开它?可能是在解压缩之后。

最新更新