2016-09-22 64 views
0

我想要在mainPanel中更新滑塊的值。以下是我的代碼 -動作按鈕不會更新sliderinput值

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作爲South時,滑塊會更改基本條件面板,但表格中的值仍然爲North。

我該如何解決這個問題?

感謝

回答

0

這裏是解決方案 -

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) 
0

如果我按下提交按鈕在表中的值更新,無論我是在北方還是南方。您的aab()submit按鈕上反應而不在滑塊上。 加載應用程序並在Zone North中移動滑塊也不會更新數據,因爲我沒有按提交。

您隔離了其他控件,因此在更改滑塊時不激活反應部分。

此外,您對兩個條件面板(num1 num3 num5num7)都使用了相同的名稱。您應該重命名South輸入,並有條件地創建結果aab()

+0

我,因爲它是在區域的選擇我的應用程序基礎上其他地方正在使用無法在有條件面板更改名稱。 – user1463242

+0

問題解決。我使用'observeEvent'來更改各個區域選擇的滑塊值。 – user1463242

+0

您能否將上述示例的解決方案代碼作爲答案發布? – Wietze314