mmyoung77
mmyoung77

Reputation: 1417

Adding a second argument to an lapply function

So I have a function f(x1) as part of a Shiny app, that is deployed like so:

y <- lapply(input$A, FUN = f)

Through the course of bug fixing, I have realized I need to add a second argument, making it f(x1, x2), where x2 is a different input. But this code...

y <- lapply(input$A, FUN = f(..., input$B))

...gives me an error saying I am using ... incorrectly. Can someone tells me what the proper syntax should be?

Upvotes: 2

Views: 211

Answers (1)

akrun
akrun

Reputation: 887118

Instead of lapply, we can use Map/mapply for multiple arguments (assuming the length of 'input$A', 'input$B' are the same)

Map(f, input$A, input$B)

With lapply, an option is to loop over the sequence of input$A

lapply(seq_along(input$A), function(i) f(input$A[i], input$B[i]))

Or with purrr

library(purrr)
map2(input$A, input$B, f)

Upvotes: 2

Related Questions