更改条件时,conditionalPanel不会更新

时间:2017-08-30 15:46:53

标签: r shiny

我正在尝试构建一个闪亮的应用程序。在我的代码中,我使用条件面板作为以下内容,用户在不同的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“input.PKmodel == 1”的默认值。

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

2 个答案:

答案 0 :(得分: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的输入,只能用条件面板隐藏它们。条件面板不会从服务器的后端删除Input,它只会将其隐藏在UI中。

答案 1 :(得分:0)

感谢@Stefan Langenborg !!!!

非常感谢Stefan!我更改了代码以确保没有两个输入具有相同的ID并且它有效。这是我使用的代码:

<?php

class DirectoryUtils{      

  public function deleteAllFiles($dir){
    // code to delete all files in $dir;
  }

  public function renameDirectory($dir, $newName){
    // code to rename $dir name to $newName
  }
}