我是 Shiny 的新手,如果这个问题被误导,请原谅我。
假设我有 2 个单独的输入input$n_1
并且input$n_2
调用几乎相同的代码。
library(shiny)
ui <- fluidPage(
fluidRow(
numericInput("n_1", label = "n1", value = 5000),
numericInput("n_2", label = "n2", value = 5000),
textOutput("result1"),
textOutput("result2")
)
)
server <- function(input,output,session)({
generate_result1 <- reactive({
# A whole lot of code to generate a result using input$n_1
x = input$n_1 * 5
})
generate_result2 <- reactive({
# A whole lot of identical code to generate a result using but using input$n_2.
# This example only shows one line of code but the real code has dozens of
# lines of code where the only difference from generate_result1 is that it uses
# input$n_2 instead of input$n_1.
x = input$n_2 * 5
})
output$result1 <- renderText(generate_result1())
output$result2 <- renderText(generate_result2())
})
shinyApp(ui = ui, server = server)
有没有办法避免当前在上面的示例中发现的代码generate_result1
重复generate_result2
?