如何为主面板输出重新编码 server.r 中闪亮的 ui.r 数值?

How to recode shiny ui.r numeric values in server.r for main panel outputs?

我正在构建一个闪亮的应用程序,其中有一系列数字输入,如下所示。用户可以为 10 个输入输入 1-9 的值。目标是生成一些图,将 1-9 值 server.r 重新编码为 1-3(即 1-3 = 1、4-6=2、7-9=3)。我尝试在 server.r 上使用各种重新编码函数,但都导致错误:'Attempted to assign a value to a read-only reactivevalues object'。下面是我的 ui.r 和 server.r 代码,我最终希望条形图反映重新编码的数字输入值。

**ui.R code**

library(shiny)
shinyUI(fluidPage(
titlePanel("Short Form Web App"),  

sidebarPanel(
  numericInput("i1", label = h5("Intellectual Item 1"), min=1,max=9,value=1),
  numericInput("i3", label = h5("Intellectual Item 3"), min=1,max=9,value=1),
  numericInput("i6", label = h5("Intellectual Item 6"), min=1,max=9,value=1),
  numericInput("i9", label = h5("Intellectual Item 9"), min=1,max=9,value=1),
  numericInput("i11",label = h5("Intellectual Item 11"), min=1,max=9,value=1),    
  numericInput("a5", label = h5("Academic Item 5"), min=1,max=9,value=1),
  numericInput("a6", label = h5("Academic Item 6"), min=1,max=9,value=1),
  numericInput("a7", label = h5("Academic Item 7"), min=1,max=9,value=1),
  numericInput("a8", label = h5("Academic Item 8"), min=1,max=9,value=1),
  numericInput("a9", label = h5("Academic Item 9"), min=1,max=9,value=1)
  ),

  mainPanel(
   plotOutput("distPlot1"))))

**server.R code**

library(shiny)

shinyServer(function(input, output) {   
 output$distPlot1 <- renderPlot({
 x<- cbind(input$i1, input$i3, input$i6, input$i9, input$i11)
 bins <- seq(min(x), max(x))
 barplot(height = x,names.arg=c("Item 1", "Item 3","Item 6","Item 9","Item 11"))    
 })
 })

我建议将 shinyServer 中的函数修改如下:

shinyServer(function(input, output) {
    output$distPlot1 <- renderPlot({
        x <- cbind(input$i1, input$i3, input$i6, input$i9, input$i11)
        x <- apply(x, 2, function(x) ifelse(x<4,1, ifelse(x>6,3,2)))
        bins <- seq(min(x), max(x))
        barplot(height = x, names.arg = c("Item 1", "Item 3", "Item 6",
            "Item 9", "Item 11"))
    })
})