是否可以将几个变量的结果分配给 Shiny 中的另一个变量?

Could the result of several variables be assigned to another variable in Shiny?

不久前才开始学习Shiny。我想将 y 分配给 (y1+y2+y3+y4)/4,并将此分配给 y在其他地方。

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

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

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

您可以根据输入创建自己的无功值

shinyServer(function(input, output,session) {

  y <- reactive((input$y1+input$y2+input$y3+input$y4)/4)

  output$y <- renderText(y())

})

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