2017-08-30 33 views
0

我試圖構建一個閃亮的應用程序。在我的代碼,我使用條件面板以下,其中用戶不同input.pkmodel之間進行選擇,並根據不同的參數設置出現在用戶面前conditionalPanel在更改條件時不更新

library("shiny") 
ui <- fluidPage(theme = shinytheme("united"),   
actionButton("go", "RUN"), 

selectInput("PKmodel", 
     "Select the PK Model:", 
     choices = list(" 1 Compartment Model" = 1, 
         " 2 Compartment Model" = 2), 

     selected = 1), 
conditionalPanel(
condition = "input.PKmodel == 1", 
numericInput("CL", "CL:", 5, min = 0.000001, max = 10000,step = .01) 
), 
conditionalPanel(
condition = "input.PKmodel == 2", 
numericInput("CL", "CL:", 20, min = 0.000001, max = 10000,step = .01), 
numericInput("VC", "V:", 13, min = 0.0000001, max = 20,step = .01)) 
) 



server <- function(input, output) { 

sim.data <- eventReactive(input$go,{ 
selectPKmod <- input$PKmodel 
if (selectPKmod == 1) { 
    CLIN<-input$CL } 
if (selectPKmod == 2) { 
    CLIN<-input$CL 
    VCIN<- input$VC } 
}) 
} 
shinyApp(ui = ui, server = server) 

我面對這個問題: 當我選擇input.PKmodel == 2,CL參數值不會更改爲20「input.PKmodel == 2的默認值」,也不會更改爲任何用戶定義的值。但是,它保持CLIN = 5「input.PKmodel == 1的默認值」。

換句話說,如果我選擇input.PKmodel == 2,然後使用用戶界面更改了VCIN或CLIN的值,但這些值都不會改變。 對於CLIN,它保持固定爲5的值(來自默認值input.PKmodel == 1), 並且VCIN參數固定爲值13(來自input.PKmodel == 2)。所以看起來參數獲得了它們第一次出現的值,但是它們沒有被更新。 你能幫忙解決這個問題嗎?另外,請注意,我正在使用actionButton和eventReactive(我不確定這是否與問題有關係)。 感謝

回答

0
library("shiny") 
library("shinythemes") 
ui <- fluidPage(theme = shinytheme("united"),   
       actionButton("go", "RUN"), 
       selectInput("PKmodel", 
          "Select the PK Model:", 
          choices = list(" 1 Compartment Model" = 1, 
              " 2 Compartment Model" = 2), 

          selected = 1), 
       conditionalPanel(
        condition = "input.PKmodel == 1", 
        numericInput("CL1", "CL:", 5, min = 0.000001, max = 10000,step = .01) 
       ), 
       conditionalPanel(
        condition = "input.PKmodel == 2", 
        numericInput("CL2", "CL:", 20, min = 0.000001, max = 10000,step = .01), 
        numericInput("VC", "V:", 13, min = 0.0000001, max = 20,step = .01)) 
) 



server <- function(input, output) { 

    values <- reactiveValues() 
    values$CLIN <- NULL 
    values$VCIN <- NULL 


    observeEvent(input$go,{ 
    selectPKmod <- input$PKmodel 
    if (input$PKmodel == 1) { 
     values$CLIN<-input$CL1 } 
    else if (input$PKmodel == 2) { 
     values$CLIN<-input$CL2 
     values$VCIN<- input$VC } 
    }) 
} 
shinyApp(ui = ui, server = server) 

這個版本使用reactiveValues對象和observeEvent來更新CLIN和VCIN內部的價值觀和正常工作。如果你想能夠像這樣更新一個內部變量的值,我相信你必須使用reactiveValues對象。

此外,我不相信你可以使用兩個輸入具有相同的ID,只有用條件面板隱藏它們。條件面板不會將輸入從服務器的後端移除,它只會將其隱藏在UI中。

+0

非常感謝您!我改變了代碼,以確保沒有兩個輸入具有相同的ID,它的工作。 –

0

感謝@Stefan Langenborg !!!!

謝謝Stefan非常!我改變了代碼,以確保沒有兩個輸入具有相同的ID,它的工作。這是我使用的代碼:

library("shiny") 
ui <- fluidPage(theme = shinytheme("united"),   
actionButton("go", "RUN"), 

selectInput("PKmodel", 
     "Select the PK Model:", 
     choices = list(" 1 Compartment Model" = 1, 
         " 2 Compartment Model" = 2), 

     selected = 1), 
conditionalPanel(
condition = "input.PKmodel == 1", 
numericInput("CL1", "CL:", 5, min = 0.000001, max = 10000,step = .01) 
), 
conditionalPanel(
condition = "input.PKmodel == 2", 
numericInput("CL2", "CL:", 17, min = 0.000001, max = 10000,step = .01), 
numericInput("VC2", "V:", 4.12, min = 0.0000001, max = 20,step = .01)) 
) 


server <- function(input, output) { 

sim.data <- eventReactive(input$go,{ 
selectPKmod <- input$PKmodel 
if (selectPKmod == 1) { 
    CLIN<-input$CL1 } 
if (selectPKmod == 2) { 
    CLIN<-input$CL2 
    VCIN<- input$VC2 } 
}) 
} 
shinyApp(ui = ui, server = server)