简体   繁体   中英

R - Passing two variables to a function using lapply

I have a function looking like this:

Func_Daily <- function(DATAdir, DATE, RERUN){
  sum_dbefore <- Func_day_before(DATE)
  files <- Func_getFiles(DATE)
  if(length(files) > 0){
    data <- mclapply(files, function(x) Func_data_read(x), mc.cores = nCores)
    print(data)
  }
}

The main program passes the needed variables to Func_Daily() . Func_Daily() gets the files inside a folder from the function Func_getFiles() and passes that to the function Func_data_read() to read and alter each file inside that folder. The problem now is, that I have another function that calculates a value ( sum_dbefore ) and I will need that inside the Func_data_read() function:

Func_data_read <- function(file){
  if(length(file) > 0){
    data <- lf_getNetCDF(file)
    data$RR[1] <- data$RR[1]+sum_dbefore
    data$RR_acc <- cumsum(data$RR)
  }else{
    print(paste("no files on", DATE))
  }
  return(data)
}

Is there a way inside lapply to pass that value together with files to Func_data_read() to use both variables inside the function and get the value back to save it to data in Func_Daily ? I guess there is an easy way to do that using lapply, but so far I couldn't figure out how. If possible I don't want to rebuild the whole set up as it is connected to a few more functions inside the script. So using lapply to pass sum_dbefore and files and use them inside Func_data_read() would be my preferred way of doing it.

Yes, the following should do it assuming sum_before is the same for all files :

Func_Daily <- function(DATAdir, DATE, RERUN){
  sum_dbefore <- Func_day_before(DATE)
  files <- Func_getFiles(DATE)
  if(length(files) > 0){
    data <- mclapply(files, Func_data_read, sum_dbefore = sum_dbefore, mc.cores = nCores)
    print(data)
  }
}

with Fun_data_read defined before it

Func_data_read <- function(file, sum_dbefore) {
  if(length(file) > 0){
    data <- lf_getNetCDF(file)
    data$RR[1] <- data$RR[1]+sum_dbefore
    data$RR_acc <- cumsum(data$RR)
  } else {
    print(paste("no files on", DATE))
  }
  return(data)
}

In the above, we add an input argument for sum_dbefore to Fun_data_read(...) and we pass sum_dbefore to Fun_data_read() through mclapply as the "optional arguments to FUN". See the documentation for mclapply under USAGE :

mclapply(X, FUN, ..., mc.preschedule = TRUE, mc.set.seed = TRUE, mc.silent = FALSE, mc.cores = getOption("cores"), mc.cleanup = TRUE)

and Arguments

... optional arguments to FUN

Hope this helps.

Assuming that I've understood you correctly and sum_dbefore is a single constant for all files , then you shouldn't need to explicitly pass this to Func_data_read(x) within mclapply .

When sum_dbefore is encountered within Func_data_read(x) , R will search for this variable in the function's enclosing environment. If it isn't there, then it will search the enclosing environment of the enclosing environment, and so on. A good explanation can be found here .

For example,

library(parallel)
q <- 10
mclapply(1:3, function(x) sqrt(x + q), mc.cores = 1)

# [[1]]
# [1] 3.316625
# 
# [[2]]
# [1] 3.464102
# 
# [[3]]
# [1] 3.605551

Your code should work as it is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM