Reputation: 111
I am trying to build a shiny app. in my code, I use a conditional panel as the following, where the user choose between different input.pkmodel
, and based on that different set of parameters appear to the user
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)
I am facing this issue: When I select input.PKmodel == 2 , the CL parameter value doesn't change neither to 20 "the default for input.PKmodel == 2" nor to any user defined value. But instead it stays as CLIN=5 "the default for input.PKmodel == 1" .
In other words, If I select input.PKmodel == 2, then changed the value of either VCIN or CLIN "using the user interface", neither of these values do change. For CLIN it stays fixated to the value of 5 (from default value input.PKmodel == 1), and the VCIN parameter stays fixated to the value of 13 (from input.PKmodel == 2). So it seems that the parameters get the value of the first time they appear, but they dont get updated. Could you please help solve this issue? also , please notice that I am using actionButton and eventReactive ( I am not sure if this has a relation to the issue). Thanks
Upvotes: 0
Views: 1374
Reputation: 111
Thanks to @Stefan Langenborg!!!!
Thank you Stefan very much!!! I changed the code to make sure no two inputs have the same ID and it worked. This is the code I used:
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)
Upvotes: 0
Reputation: 243
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)
This version uses the reactiveValues object and observeEvent to update the internal values for CLIN and VCIN and works properly. If you want to be able to update the values for an internal variable like this I believe you have to use the reactiveValues object.
Also, I do not believe you can use two inputs with the same ID and only hide them with conditional panels. A conditional panel does not remove the Input from the backend of the server, it only hides it in the UI.
Upvotes: 1