Reputation: 323
I'm new to Shiny, but am working on an app that requires a lot of back and forth between the UI and the shinyServer. To do this, I've familiarized myself with the dynamic renderUI function. However, I'm having some issues properly working with the inputs that are sent to the UI through the renderUI function.
I've coded up a quick toy example of my issue.
library(shiny)
ui <- fluidPage(
numericInput("cat.count", "How many cats do you have?", min = 0, value = 0),
checkboxInput('pet.check',"Do you other pets?", value = FALSE),
uiOutput("dog.input"),
uiOutput("dog.num")
)
server <- shinyServer(function(input,output){
output$dog.input <- renderUI({
if(input$pet.check){
return(checkboxInput("dog.check", "Do you have dogs?", value = FALSE))
}
})
output$dog.num <- renderUI({
if (exists("input$dog.check") & input&dog.check){
return(numericInput("dog.count", "How many dogs do you have?", min = 1,
value = 0))
}
})
})
shinyApp(ui = ui,server = server)
Once this toy app runs without errors, the "final product" would be a simple bar chart via geom_bar with ggplot2 graphics showing the total number of pets that the user owns.
I've researched the reactive() function a lot in addition to the req() and exists() functions, but so far, I've been running around in circles.
Thanks in advance!
Upvotes: 1
Views: 813
Reputation: 7695
If you type statements like
A(x) & B(x)
into R
, both A
and B
get evaluated even if A(x)
is wrong. This is where your error is coming from. Therefore it is better to use nested if
statements like in the answer of Pork Chop
Upvotes: 0
Reputation: 29417
Something like this work?
rm(list = ls())
library(shiny)
ui <- fluidPage(
numericInput("cat.count", "How many cats do you have?", min = 0, value = 0),
checkboxInput('pet.check',"Do you other pets?", value = FALSE),
uiOutput("dog.input"),
uiOutput("dog.num")
)
server <- shinyServer(function(input,output){
output$dog.input <- renderUI({
if(is.null(input$pet.check)){return()}
if(input$pet.check){
return(checkboxInput("dog.check", "Do you have dogs?", value = FALSE))
}
})
output$dog.num <- renderUI({
if(is.null(input$dog.check)){return()}
numericInput("dog.count", "How many dogs do you have?", min = 1, value = 0)
})
})
runApp(list(ui = ui, server = server))
Upvotes: 3