Shiny modules is what might help you.
See here: https://shiny.rstudio.com/articles/modules.html
As you can read in the article the desired functionality to pass in input is possible if you wrap the input in a reactive()
function. (See the end of the "Writing server functions" section of the article).
You would define the my_sum
function as follows:
(Note that you have to use the variables a
and b
as reactives a()
and b()
and wrap the result in a reactive()
function.)
my_sum <- function(input, output, session, a, b) {
reactive(as.numeric(a()) + as.numeric(b()))
}
And could use it as:
my_sum_reactive <- callModule(my_sum, "id", reactive(input$a), reactive(input$b))
which is then usable as:
my_sum_reactive()
Reproducible example:
library(shiny)
my_sum <- function(input, output, session, a, b) {
reactive(as.numeric(a()) + as.numeric(b()))
}
ui <- fluidPage({
fluidRow(
selectInput("a", "a", 1:3),
selectInput("b", "b", 1:3),
textOutput("txt")
)
})
server <- function(input, output, session) {
my_sum_reactive <- callModule(my_sum, "id", reactive(input$a), reactive(input$b))
output$txt <- renderText(paste0("The sum is: ", my_sum_reactive()))
}
shinyApp(ui, server)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…