0

I'm importing some raster files from a PostgreSQL connection into R in a loop. I want to assign my newly gained rasters automatically to a variable whose name is derived from the input variable like this: substring(crop, 12)

crop <- "efsa_capri_barley"
ras <- readGDAL(sprintf("PG:dbname='' host='' port='' user='' schema='' table='%s' mode=2", crop))
paste0(substring(crop, 12)) <- raster(ras, 1)

What function do I have to use that R recognizes the result of substring() as a character string and not as the function itself? I was thinking about paste() but it doesn't work. Probably this question has already been asked but I couldn't find a proper answer.

andschar
  • 3,504
  • 2
  • 27
  • 35
  • Duplicate of [R: How to convert string to variable name?](http://stackoverflow.com/questions/6034655/r-how-to-convert-string-to-variable-name). I found it by searching for "assign variable name in loop r" in Google. – Tchotchke Jun 27 '16 at 14:10
  • yes, you're right. I extended my question. – andschar Jun 28 '16 at 15:55
  • 1
    Generally on SO you don't ask more than one question at a time. Given that your second question is completely unrelated to the first, I'd delete that and make it a new question. And then if you think @joran answered your first question, I'd go ahead and accept that as correct. – Tchotchke Jun 28 '16 at 16:48
  • ok. sounds reasonable. I will ask a new question – andschar Jun 28 '16 at 17:39

2 Answers2

5

Based on your description, assign is technically correct, but recommending it is bad advice.

If you are pulling in multiple rasters in a loop, best practice in R is to initialize a list to hold all the resulting rasters and name each list element accordingly. You can do this one at a time:

# n is number of rasters
raster_list <- vector("list",n)

for (i in seq_len(n)){
  ...
  #crop[i] is the ith crop name
  raster_list[[substring(crop[i],12)]] <- raster(...)
}

You can also set the names of each element of the list all at once via setNames. But you should try to avoid using assign pretty much at all costs.

joran
  • 169,992
  • 32
  • 429
  • 468
  • Thanks for your helpful explanation and without your hint I would have ran into `assign` instantly. – andschar Jun 28 '16 at 15:35
  • Maybe it's worth posting a new question or to broaden mine: As each of my rasters (EU (still including the UK^^) wide data) has approx. 200MB and I only need the areal extent of Germany, I wonder if there's a possibility to clip the rasters to my extent when importing / reading them. Can sb think of one? – andschar Jun 28 '16 at 15:44
  • instead of n=number of rasters I used n=0, because the code doesn't fill into the list, it appends the rasters to the list. – andschar Jul 13 '16 at 11:24
0

If I understand your question correctly, you are looking for something like assign. For example you can try this:

assign(substring(crop, 12), raster(ras, 1))

To understand how assign works, you can check this code:

x <- 2
# x is now 2

var_to_assign <- "x"
assign(var_to_assign, 3)
# x is now set to 3
x
# 3

Does that give you what you want?

David
  • 9,216
  • 4
  • 45
  • 78