Shiny中的反应变量用于以后的计算

kom*_*an_ 2 r shiny

这里总新手.

我有一个输出写入公式的结果,变量基于输入

output$text_calc <- renderText({
    paste("The result is =", input$num_W * input$num_l - input$slider_a... )
    })
Run Code Online (Sandbox Code Playgroud)

为了避免我的大脑爆炸长公式,我如何将输入定义为单个字母变量?我试过l <- input$num_l哪个给了我

"没有主动反应上下文就不允许操作.(你试图做一些只能在反应式表达式或观察者内部完成的事情.)"

放置reactive({})代码给出了

错误:无法将类型'闭包'强制类型为'character'类型的向量

Por*_*hop 6

尝试类似下面的例子.另外,请注意在reactive表达式中使用输入

例1

#rm(list = ls())
library(shiny)
ui <- shinyUI(fluidPage(
  mainPanel(
    numericInput("num_W", "Observations:", 10,min = 1, max = 100),
    numericInput("num_l", "Observations:", 10,min = 1, max = 100),
    sliderInput("slider_a","test",value = 1,min=1,max=20,step=1),
    textOutput("text_calc"))
))
server <- shinyServer(function(input, output,session){
  output$text_calc <- renderText({
    W <- input$num_W
    L <- input$num_l
    A <- input$slider_a
    paste("The result is =", W*L-A)
  })
})

shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)

例2,使用reactiveValues()

#rm(list = ls())
library(shiny)
ui <- shinyUI(fluidPage(
  mainPanel(
    numericInput("num_W", "Observations:", 10,min = 1, max = 100),
    numericInput("num_l", "Observations:", 10,min = 1, max = 100),
    sliderInput("slider_a","test",value = 1,min=1,max=20,step=1),
    textOutput("text_calc"))
))
server <- shinyServer(function(input, output,session){

  vals <- reactiveValues()
  observe({
    vals$W <- input$num_W
    vals$L <- input$num_l
    vals$A <- input$slider_a
  })

  output$text_calc <- renderText({
    paste("The result is =", vals$W*vals$L-vals$A)
  })
})

shinyApp(ui = ui, server = server)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述