user1463242 user1463242 - 2 months ago 14
R Question

Action Button does not update sliderinput values

I want to update the values of a table in mainPanel basis the value of sliders. Below is my code-

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)


So when I select Zone as North and press Submit, it shows me the right values. But when I select Zone as South, the sliders change basis conditional panels but the values in the table is still for North.

How can I fix this?

Thanks

Answer

Here is the solution-

    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)