How to parallelize the.call function

Asked

Viewed 120 times

3

I need to do a multiple montage rasters for a single raster. Both the function raster::merge how much raster::mosaic work well in this situation, but I need to make a do.call to call the function to mount multiples rasters who are on a list.

How to do this parallelized? How to do do do do.call faster?

require(raster)
r <- raster(ncol=100, nrow=100)
r1 <- crop(r, extent(-10, 11, -10, 11))
r1[] <- 1:ncell(r1)
r2 <- crop(r, extent(0, 20, 0, 20))
r2[] <- 1:ncell(r2)
r3 <- crop(r, extent(9, 30, 9, 30))
r3[] <- 1:ncell(r3)
rast.list <- list(r1, r2, r3)     
rast.list$fun <- mean
rast.mosaic <- do.call(mosaic,rast.list)
  • 2

    try to put a minimum reproducible example of what you are doing. The way your question is, it will be difficult to get a good answer

  • -1 until a reproducible example is provided.

  • @Flaviobarros , the example seems reproducible

1 answer

0

To parallelize a function, you can use the package purrr.

The function map2 serves to use multiple arguments in one function at once:

library(tidyverse)
a = c("Eu sou o Igor.", "Eu sou o João.")
b = c(" Eu gosto disso.", " Eu gosto daquilo.")

map2(a, b, ~stringr::str_c(.x, .y))
#[[1]]
#[1] "Eu sou o Igor. Eu gosto disso"

#[[2]]
#[1] "Eu sou o João. Eu gosto daquilo"

Look at what’s happening. The value a is being applied as the first argument in the function str_c. The value b is being applied as second argument.

You can use map2_chr to return a character array:

map2(a, b, ~stringr::str_c(.x, .y))
#[1] "Eu sou o Igor. Eu gosto disso"   "Eu sou o João. Eu gosto daquilo"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.