Skip to content
Back to formatted view

Raw Message

Message-ID: <CO6PR22MB2433A9762FB9A549590D9ECEB6CDA@CO6PR22MB2433.namprd22.prod.outlook.com>
Date: 2023-10-10T00:30:36Z
From: Keshav, Krishna
Subject: [R-pkg-devel] Error: [writeRaster] cannot write file
In-Reply-To: <CADNULg_xAggbz+JGMh=UeK9EBizAf29AcwCyEMsqaoGJOtE55A@mail.gmail.com>

Hi,

This works. I think this is also an implementation of what Berry and Duncan suggested earlier. Thanks.

Best Regards,
Krishna Keshav

From: Iris Simmons <ikwsimmo at gmail.com>
Date: Monday, October 9, 2023 at 7:37 PM
To: Keshav, Krishna <kkeshav at ufl.edu>
Cc: r-package-devel at r-project.org <r-package-devel at r-project.org>
Subject: Re: [R-pkg-devel] Error: [writeRaster] cannot write file
[External Email]
You wrote

# create the plots directory if it does not exist

but then created a directory named outdir/plots/typ_Sys.time.tif

You need to rewrite the code like:

plotsdir <- file.path(outdir, "plots")
fp <- file.path(plotsdir,  paste(typ, "_",
    stringr::str_replace_all(Sys.time(), "[^a-zA-Z0-9]", ""),
                                ".tif", sep = ""))
# Create the "plots" directory if it doesn't exist
  if (!dir.exists(plotsdir)) {
    dir.create(plotsdir, recursive = TRUE)
  }

On Mon, Oct 9, 2023, 15:49 Keshav, Krishna <kkeshav at ufl.edu<mailto:kkeshav at ufl.edu>> wrote:
Hi,

I am developing an R package where I need to save Raster file with .tif extension to the tempdir(). I am using terra::writeRaster for the same. While it works through R CMD check in mac, it is failing in R hub builder.
Snippet ?V
.saverast <- function(typ, rast, outdir) {

  if (is.null(outdir) || length(outdir) == 0) {
    outdir <- tempdir()
  }

  # Save the plot as a raster file
  fp <- file.path(outdir, paste("plots", "/",
                                typ, "_",
                                stringr::str_replace_all(Sys.time(), "[^a-zA-Z0-9]", ""),
                                ".tif", sep = ""))
    # Create the "plots" directory if it doesn't exist
  if (!dir.exists(fp)) {
    dir.create(fp, recursive = TRUE)
  }

  terra::writeRaster(rast, overwrite = TRUE,
                     filename = fp,
                     gdal = c("COMPRESS=NONE"))
  message(paste("raster created", fp, sep = ": "), "\n")
}

Error ?V

  Error: [writeRaster] cannot write file
   12.         ?|?wgeohabnet:::.saverast(typ, rast, outdir)
   13.           ?u?wterra::writeRaster(rast, overwrite = TRUE, filename = fp, gdal = c("COMPRESS=NONE"))
   14.           ?|?wterra::writeRaster(rast, overwrite = TRUE, filename = fp, gdal = c("COMPRESS=NONE"))
   15.             ?|?wterra (local) .local(x, filename, ...)
   16.               ?|?wterra:::messages(x, "writeRaster")
   17.                 ?|?wterra:::error(f, x at pnt$getError())



Best Regards,
Krishna Keshav

        [[alternative HTML version deleted]]

______________________________________________
R-package-devel at r-project.org<mailto:R-package-devel at r-project.org> mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel

	[[alternative HTML version deleted]]