【问题标题】:conditionalPanel doesn't update when changing the condition更改条件时条件面板不更新
【发布时间】:2017-08-30 15:46:52
【问题描述】:

我正在尝试构建一个闪亮的应用程序。在我的代码中,我使用如下条件面板,用户可以在不同的input.pkmodel 之间进行选择,并根据不同的参数集向用户显示

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 "the default for input.PKmodel == 1" 。

换句话说,如果我选择 input.PKmodel == 2,然后“使用用户界面”更改了 VCIN 或 CLIN 的值,这些值都不会改变。 对于 CLIN,它保持固定在 5 的值(从默认值 input.PKmodel == 1), 并且 VCIN 参数保持固定为 13 的值(来自 input.PKmodel == 2)。所以似乎参数获得了它们第一次出现的值,但它们没有得到更新。 你能帮忙解决这个问题吗?另外,请注意我正在使用 actionButton 和 eventReactive (我不确定这是否与问题有关)。 谢谢

【问题讨论】:

    标签: r shiny


    【解决方案1】:
    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 的两个输入,并且只能使用条件面板隐藏它们。条件面板不会从服务器后端移除 Input,它只会将其隐藏在 UI 中。

    【讨论】:

    • 非常感谢!!!我更改了代码以确保没有两个输入具有相同的 ID 并且它有效。
    【解决方案2】:

    感谢@Stefan Langenborg!!!!

    非常感谢斯特凡!!!我更改了代码以确保没有两个输入具有相同的 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-17
      • 2010-10-27
      相关资源
      最近更新 更多