Transition from R Studio to Cloud issue

In the loop below, I am trying to create names of rasters associated with each month (e.g. 1-12) of files located in the project file directory in Cloud. There are 12 rasters in the directory.


for (m in 1:12){ #start month cycle
  if (m<10){ #conditional to build month to character
    mnchar<-paste("0",m,sep="") #add a 0 to months below October
  } else { #conditional for months larger than October
    mnchar<-toString(m) #months from October to December convert directly to character
  } #end of conditional to write month as character
 
  # Generates name of monthly rasters
crasnam<-paste("PRISM_ppt_30yr_normal_4kmM2_",mnchar,"_bil/PRISM_ppt_30yr_normal_4kmM2_",mnchar,"_bil.bil",sep="") 
  cras<-raster(crasnam) #open monthly raster
  pbgmn[,m]<-extract(cras,geocoords) #get monthly values by grid
  pavgmn[m,1]<-mean(pbgmn[,m]) #calculate average basin precipitation by month
  
} #end of month cycle

I am able to run the loop successfully in R Studio but not in Cloud. I suspect I am missing something obvious in the transition from Studio to Cloud. A link to the project is here:

https://rstudio.cloud/spaces/65621/project/1267157

I get the error:

Error in .local(.Object, ...) :
Error in .rasterObjectFromFile(x, band = band, objecttype = "RasterLayer", :
Cannot create a RasterLayer object from this file. (file does not exist)

thanks,
nico

Maybe the file structure is different when you moved to rstudio.cloud. Do things work better if you modify the paste command? (It looks like the prefix "PRISM_ppt_30yr..." is duplicated)

crasnam<-paste("PRISM_ppt_30yr_normal_4kmM2_",mnchar,"_bil.bil",sep="") 

I was thinking since the filenames are fairly complex could you put the rasters in a separate data directory and then use files.list() to get them into a vector and loop over that. Would simplify the code.

Thank you, this was successful. It looks like I retained part of the file path from my local machine to Cloud. I appreciate the support!
best wishes, nico

Agreed, I will work on simplifying it. thank you so much for the insight.
best wishes,
nico

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.