Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)

The below code is a part of a Shiny app and that app is having a function f(x1):

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

I know that I need to add a second argument, making it f(x1, x2), where x2 is a different input. But the below code is giving me an error that I am using ... incorrectly

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

Can someone guide me what is the proper syntax?

1 Answer

0 votes
by (107k points)

If you are having multiple arguments then, we can use Map/mapply:

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

If you wish to use lapply then, 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)

If you are a beginner and want to know more about R then do check out the R programming tutorial

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...