【问题标题】:Unable to read raster file using `raster::raster` and Rscript in Windows无法在 Windows 中使用 `raster::raster` 和 Rscript 读取光栅文件
【发布时间】:2016-02-19 21:31:31
【问题描述】:

我似乎在使用 Rscript 和直接调用 raster 包读取光栅时遇到了仅限 Windows 的问题。我有最新版本的 raster 软件包 (2.5-2)。在 Windows 2008 R2 中运行 R 3.2.1。

鉴于以下情况:

library(raster)
j <- raster(matrix(rnorm(100), ncol=10, nrow=10))
writeRaster(j, 'j.tif')

如果我开始一个新的 R 会话(或不),这在 R 中有效:

library(raster)
j <- raster('j.tif')

这样做(同样,在新会话中):

j <- raster::raster('j.tif')

如果我从命令行尝试并使用 Rscript,则可以:

C:\> Rscript -e "library(raster); raster('j.tif')

但这不起作用:

C:\> Rscript -e "raster::raster('j.tif')"
Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer",  :
  Cannot create a RasterLayer object from this file.
Calls: <Anonymous> -> <Anonymous> -> .local -> .rasterObjectFromFile
Execution halted

【问题讨论】:

  • 我无法在我的系统上重现这一点 - 两个 Rscript 调用对我来说都很好(R 3.2.3,64 位;Windows 8.1;光栅 2.5-2)
  • 我已经弄清楚这是因为Rscript 没有加载方法包并且raster:::.rasterFromGDAL(在这种情况下由raster::raster 调用)需要methods::checkAtAssignment .但是,不知道为什么它在其他地方有效。无法立即升级到 3.2.3,所以我会继续观察。

标签: r r-raster


【解决方案1】:

文件是否存在于c:/ 中?你能做到吗:

Rscript -e "file.exists('j.tif')"

我认为你对 Rscript 不加载方法包是正确的。但是raster 依赖它并加载它:

Rscript -e "library(raster); sessionInfo()"

#Loading required package: methods
#Loading required package: sp
#R Under development (unstable) (2016-01-09 r69890)
#Platform: x86_64-w64-mingw32/x64 (64-bit)
#Running under: Windows 7 x64 (build 7601) Service Pack 1

#attached base packages:
#[1] methods   stats     graphics  grDevices utils     datasets  base

【讨论】:

    猜你喜欢
    • 2023-01-12
    • 2015-07-06
    • 1970-01-01
    • 2021-11-28
    • 2015-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-15
    相关资源
    最近更新 更多