【问题标题】:Could the result of several variables be assigned to another variable in Shiny?可以将几个变量的结果分配给 Shiny 中的另一个变量吗?
【发布时间】:2021-10-24 22:04:48
【问题描述】:

我不久前才开始学习 Shiny。我想把y赋值给(y1+y2+y3+y4)/4,在其他地方调用这个赋值y。 p>

起初我尝试了简单的定义 y=(input$y1+input$y2+input$y3+input$y4)/4,但它不起作用。我也尝试了很多其他方法,但都没有奏效。

因为我会在后面的计算中多次使用y(比如计算方差),但是每次使用(input$y1+input$y2+input$y3+input$y4)/4都会很臃肿(yi可能会超过20)

我的部分代码如下,

shinyUI(fluidPage(
numericInput("y1", "y1:", sample(1:200,1), min = 1, max = 200)),
numericInput("y2", "y2:", sample(1:200,1), min = 1, max = 200)),
numericInput("y3", "y3:", sample(1:200,1), min = 1, max = 200)),
numericInput("y4", "y4:", sample(1:200,1), min = 1, max = 200))
))
shinyServer(function(input, output,session) {
  
  output$y <- renderText({(input$y1+input$y2+input$y3+input$y4)/4})
})

不知道有没有合适的解决方案。

【问题讨论】:

    标签: r shiny shiny-server shinyapps shiny-reactivity


    【解决方案1】:

    您可以根据输入创建自己的反应式值

    shinyServer(function(input, output,session) {
    
      y <- reactive((input$y1+input$y2+input$y3+input$y4)/4)
    
      output$y <- renderText(y())
    
    })
    

    你创建了一个响应值,你调用它就像一个函数 y() 而不是一个变量 y 来获取它的当前值

    【讨论】:

    • 有效!非常感谢!我为此困扰了好几个小时。
    猜你喜欢
    • 2019-08-31
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    • 2021-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多