In the example code below I create a function createBucket
that reads through a vector (dfVector
) and a list (dfList
) comprised of two sublist dataframes, "DFOne" and "DFTwo". The function creates another list of dummy dataframes for each dfList
sublist dataframe where it finds the element "Boy". This example code works as intended.
This is a simplification of the code I am working on. In the actual code, the equivalents of dfVector
and dfList
are reactive, expanding and contracting depending on Shiny inputs. There are other lists that the function reads, and there are other conditionals imposed as the vectors and lists are read through by the function. There are also calculations that feed from one sublist to another, instead of filling the sublist dataframes with zeroes as this example does for the sake of simplicity.
Given how much is going on with this function, is using lapply()
or another apply family function advisable? Speed is important, but the ultimate dataframe generated by this and related functions won't qualify for "big data" (120 rows by 100+ columns). How could I use lapply()
in the below? I could run speed tests with the for-loop versus lapply()
.
Code:
dfVector <- function(){c("DF One","DF Two")}
dfList <- list(DFOne = c("Boy","Cat","Dog"),DFTwo = c("Boy","Rat","Bat"))
createBucket <- function(nbr_rows) {
series <- gsub("\\s+", "", dfVector())
buckets <- list()
for (i in seq_along(series)) {
series_name <- series[i]
dfListOrder <- dfList[[series_name]]
if ("Boy" %in% dfListOrder) {
df_name <- paste0("bucket", gsub("\\s+", "", series_name))
bucket <- data.frame(
A = rep(0, nbr_rows),
B = rep(0, nbr_rows),
check.names = FALSE
)
buckets[[df_name]] <- bucket
}
}
if (length(buckets) > 0) {return(buckets)} else {return(NULL)}
}
result <- createBucket(10)
result