1

I have a main directory, and each folder within this has the same format of data. I'd like to go into each folder individually and preform the same function. Each folder has a slightly different name, but the files all follow the same naming protocol (ie. YYYYMMDD_XX_raster1.tif; YYYYMMDD_XX_raster2.geo.tif).

I've tried modifying a code that I already had, but in that instance the .tifs were all in the same folder.

library(raster)
library(sp)
library(rgdal)

#create sample rasters
r <- raster(nrow=10, ncol=10)
x <- setValues(r, sample(-180:180,ncell(r), replace(T))
y <- setValues(r, sample(-90:90,ncell(r), replace(T))



dq2<- function(in_dir_path, add_file_name, silent=F){
    #inputs
    #in_dir_path: path to directory
    #add_file_name: ie. finished
   list_files <- sort(list.files(path= in_dir_path, pattern =""))
   print(list_files)
   if(silent == F){
     message(paste"Nb files in in_dir", length(list_files))
  }
  for(i in 1: length(list_files)){
    if(silent == F){
     message(paste("Processing", i, "out of", length(list_files)))
  }

   #get rasters (this would normally grab from within the subfolder, but here refers to the rasters created above)
   x <- raster(paste(in_dir_path, "/$raster1.geo.tif", list_files[i], sep = "")) 
   y <- raster(paste(in_dir_path, "/$raster2.geo.tif", list_files[i], sep = ""))

   #Create filename to save
  split<- strsplit(list_files[i], split=".tif")[[1]][1]
    sprint (split)
  nameFile <- paster(in_dir_path, "/", split, "_", add_file_name, ".tif", sep = "")

  # calculation here
    tf <- function(x,y) {
      return(100 * x / y)
    }

    answer <- overlay(x,y, fun=tf)
    WriteRaster(answer, filename=nameFile, format="GTiff", overwrite=TRUE, options=c('TFW=YES'))

    if(silent == F){
    message(paste("process complete", nameFile))
    }

}

message("finished")

dq2("C:/Users", "fin", silent=F) 

When I try to run this, it looks like the code is waiting for more input.

  • *"it looks like the code is waiting for more input"* usually means a syntax error, like a missing parenthesis. I think your problem is the first line inside the function, `sort(` never has a `)`. – Gregor Thomas Sep 17 '19 at 19:39
  • Not sure if you copied your code correctly to here, seems like there are several none matching parantheses – yosukesabai Sep 17 '19 at 19:43
  • Also, your `WriteRaster(` is missing its `)`, and you have an extra `)` at the end of the first `message()` line. And your first `paste` is missing it's `(`. Not `}` at the end of the function. Possibly more - go through each call and make sure it ends where you think it does. Try setting up your input and running one line at a time to make sure each line works. – Gregor Thomas Sep 17 '19 at 19:43
  • Like, where is the end of this dq2 function...? – yosukesabai Sep 17 '19 at 19:44
  • ...`for` loops need parens, `for(i in 1:...)` not just `for i in 1:...` – Gregor Thomas Sep 17 '19 at 19:46
  • You can run each line of the function definition too, just hit Ctrl+Enter in RStudio and watch for errors. Voting to close the question as it seems like the issue is sloppy syntax. – Gregor Thomas Sep 17 '19 at 19:49
  • Yes, you're right that the code didn't copy well, but those were present in the original code. I went through and changed parentheses here where I could see. Seems like sloppy syntax is a valid reason to ask for help? Not sure where people are supposed to get advice otherwise! –  Sep 17 '19 at 19:52
  • Fix the syntax and if still give problems, revise the question with corrected code and the description of problem – yosukesabai Sep 17 '19 at 20:40
  • 1
    It is also a sloppy question.... You have made no effort to make it minimal reproducible example; that is your side of the bargain. For example remove the messages, show the code outside of a function.... – Robert Hijmans Sep 18 '19 at 00:52
  • bmn018, while "sloppy question" might seem a little harsh, this is your second question where we've asked for reproducibility and effort. Asking a question *well* is not necessarily intuitive, and SO can be at times harsh in how we suggest you ask it. There are a few places that suggest "how to ask a question well", please take a look at them and try to adapt their suggestions to your questions: https://stackoverflow.com/questions/5963269, https://stackoverflow.com/help/mcve, and https://stackoverflow.com/tags/r/info. (Doing so will typically get you good answers, faster.) – r2evans Sep 18 '19 at 14:28

0 Answers0