0

我直接从网站 [这里][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)
4

2 回答 2

0

这是 mode="w" Vs mode="wb" 的问题吗?我以前有这个文件。没有ncdf4的经验。

不确定您是否可以通过 mode="wb" 来获取,但可以

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

工作/帮助


编辑:

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

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

于 2021-04-04T15:56:15.920 回答
0

好的,这是填充答案:

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)
于 2021-04-07T20:00:01.083 回答