Home > Mobile >  Use mapply function instead of apply function in shiny code
Use mapply function instead of apply function in shiny code

Time:01-18

I would like to generate the same result I am getting, but instead of using the apply function I would like to use the mapply function in data_subset.

This question is very similar to this one: enter image description here

#Output using mapply function enter image description here

CodePudding user response:

The problem is that in your original apply code, you're using data(), which is the shiny-reactive full dataset; but in your attempt with mapply, you're using cur_data(), which is dplyr-speak for (in this case) Datas, a subset of the whole dataset.

If you replace your previous assignment

    All <- cbind(df2, coef = apply(df2, 1, function(x) {
      return_coef(data(), as.numeric(x[1]), as.Date(x[2]), x[3], var1, var2)
    }))

with

    All <- Datas %>%
      transmute(
        Id, date2, Category,
        coef = mapply(return_coef, list(data()), Id, as.Date(date2), Category, var1, var2)
      )
    All
#   Id      date2 Category      coef
# 3  1 2022-01-09      EFG -32.14286
# 4  1 2022-01-10      ABC  50.00000

it works as expected.

  •  Tags:  
  • Related