【问题标题】:Action Button does not update sliderinput values操作按钮不更新滑块输入值
【发布时间】:2017-01-31 02:09:31
【问题描述】:

我想根据滑块的值更新 mainPanel 中表的值。下面是我的代码-

    library(shiny)

    ui <- shinyUI(fluidPage(
      sidebarPanel(
selectInput(inputId="Zone", label="Choose Forecasting Zone:",choices = list("North", "South")),
wellPanel( 
    conditionalPanel(
      condition = "input.Zone == 'North'",
      sliderInput("num1", "No of Sellers:",0,3500,value = c(2877,3277),step=1),
      sliderInput("num3", "Activation Percentage:",0,1,value = c(0.25,0.32),step=0.01),
      sliderInput("num5", "Case Size:",0,200000,value = c(60000,75000),step=1),
      sliderInput("num7", "Case Rate:",0,4,value = c(1.34,1.45),step=0.01)
    ),
    conditionalPanel(
      condition = "input.Zone == 'South'",
      sliderInput("num1", "No of Sellers:",0,3500,value = c(1008,1148),step=1),
      sliderInput("num3", "Activation Percentage:",0,1,value = c(0.26,0.32),step=0.01),
      sliderInput("num5", "Case Size:",0,200000,value = c(70000,80000),step=1),
      sliderInput("num7", "Case Rate:",0,4,value = c(1.15,1.22),step=0.01)
    ),
  actionButton("sumbit", "Submit")
  )
),
      mainPanel("",htmlOutput("ValueTable"))
      )
    )

    server <- function(input, output, session) {

      aab <- reactive({
input$sumbit
isolate({
  a1<-cbind(input$num1[1],input$num1[2])
  a2<-cbind(input$num3[1],input$num3[2])
  a3<-cbind(input$num5[1],input$num5[2])
  a4<-cbind(input$num7[1],input$num7[2])

  aa<-cbind(input$Zone,a1,a2,a3,a4)
  aa
})
      })
      output$ValueTable <- renderUI({
input$sumbit
isolate({
  aa<-aab()
  aa<-as.data.frame(aa)
  output$aa1 <- renderDataTable(aa,options = list(paging = FALSE,searching = FALSE,info=FALSE,autowidth=TRUE))
  dataTableOutput("aa1")
})
      })
    }

    shinyApp(ui, server)

因此,当我选择 Zone 作为 North 并按 Submit 时,它会显示正确的值。但是当我选择 Zone 作为 South 时,滑块会改变基础条件面板,但表中的值仍然是 North。

我该如何解决这个问题?

谢谢

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    如果我按下提交按钮,表格中的值就会更新,无论我在北方还是南方。您的 aab()submit 按钮上而不是在滑块上是反应性的。 当我不按提交时,加载应用程序并在 Zone North 中移动滑块也不会更新数据。

    您隔离了其他控件,因此在更改滑块时不会激活反应部分。

    此外,您为两个条件面板使用了相同的名称(num1 num3 num5num7)。您应该重命名 South 输入,并有条件地在 aab() 中创建结果。

    【讨论】:

    • 我无法更改条件面板中的名称,因为它正在我的应用程序的其他部分使用,基于所选区域。
    • 问题已解决。我使用observeEvent 来更改相应区域选择的滑块值。
    • 您能否发布上述示例的解决方案代码作为答案?
    【解决方案2】:

    这里是解决方案-

        library(shiny)
    
        ui <- shinyUI(fluidPage(
          sidebarPanel(
    selectInput(inputId="Zone", label="Choose Forecasting Zone:",choices = list("North", "South")),
    sliderInput("num1", "No of Sellers:",0,3500,value = c(2877,3277),step=1),
    sliderInput("num3", "Activation Percentage:",0,1,value = c(0.25,0.32),step=0.01),
    sliderInput("num5", "Case Size:",0,200000,value = c(60000,75000),step=1),
    sliderInput("num7", "Case Rate:",0,4,value = c(1.34,1.45),step=0.01),
    actionButton("sumbit", "Submit")
          ),
          mainPanel("",htmlOutput("ValueTable"))
        )
        )
    
        server <- function(input, output, session) {
    
          observeEvent(input$Zone,{
    if (input$Zone=="North"){
      updateSliderInput(session, "num1", value = c(2877,3277))
      updateSliderInput(session, "num3", value = c(0.25,0.32))
      updateSliderInput(session, "num5", value = c(60000,75000))
      updateSliderInput(session, "num7", value = c(1.34,1.45))
    }
    else  {}
          })
          observeEvent(input$Zone,{
    if (input$Zone=="South"){
      updateSliderInput(session, "num1", value = c(1008,1148))
      updateSliderInput(session, "num3", value = c(0.26,0.32))
      updateSliderInput(session, "num5", value = c(70000,80000))
      updateSliderInput(session, "num7", value = c(1.15,1.22))
    }
    else  {}
          })
    
          aab <- reactive({
    input$sumbit
    isolate({
      a1<-cbind(input$num1[1],input$num1[2])
      a2<-cbind(input$num3[1],input$num3[2])
      a3<-cbind(input$num5[1],input$num5[2])
      a4<-cbind(input$num7[1],input$num7[2])
    
      aa<-cbind(input$Zone,a1,a2,a3,a4)
      aa
    })
          })
          output$ValueTable <- renderUI({
    input$sumbit
    isolate({
      aa<-aab()
      aa<-as.data.frame(aa)
      output$aa1 <- renderDataTable(aa,options = list(paging = FALSE,searching = FALSE,info=FALSE,autowidth=TRUE))
      dataTableOutput("aa1")
            })
          })
        }
    
        shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-06
      • 2015-02-23
      • 1970-01-01
      • 2019-03-24
      • 1970-01-01
      • 2021-07-11
      • 2017-02-26
      • 1970-01-01
      相关资源
      最近更新 更多