简体   繁体   中英

how to use conditionalpanel() in shiny r

I am trying to create a shiny app where it allows you to select an input of what operation calculate. if the user chooses "Addition" it will show the two numeric input boxes (so they can input two numbers), if the user chooses "square" it will show only one numeric input box to square.

With this, I use conditionalPanel and if the condition is satisfied, it fetches through uiOutput() the numericInput s that I want. and same thing for square.

Now when I run this app, the conditional panels do not appear. Where did I go wrong? Thanks for checking out my question.

ui <- fluidPage( theme = shinytheme("slate"),
                 titlePanel("Basic Calculator"),
                 sidebarPanel(
                          selectInput("ops","Select what Operation use",choices = c("ADDITION","SQUARE")),
                   
                          helpText("Please input the appropriate number depending on the operations"),
                          conditionalPanel("input.ops=='ADDITION'", uiOutput("var2")),
                          conditionalPanel("input.ops=='SQUARE'", uiOutput("var1"))
                         
                          ),#sidebar panel
                  
                 
                   
)#fluidpage

server <- function(input, output) {
  
  output$basicmath <- renderText( ifelse(input$ops=="ADDITION",input$a+input$b,
                                  ifelse(input$ops=="SUBTRACTION",input$a-input$b,
                                  ifelse(input$ops=="SQUARE",input$a*input$a,
                                  ifelse(input$ops=="SQUARE ROOT",sqrt(input$a),
                                  ifelse(input$ops=="MULTIPLICATION",input$a*input$b,"not a valid operation"))))),
   
  output$var2 <- renderUI({
                            helpText("this will show to input two numerics to be added")
    
                          }) ,
  
  output$var1 <- renderUI({
                           helpText("this will show to input one numeric to square")
    
  })                             
      
)}


shinyApp(ui = ui, server = server)

The key issue you were having is that you put the uiOutputs inside the calculation output that you anticipated. It is better to separate them, since the calculation output won't run until it has the necessary prerequisite values (your input values). In addition, because you hadn't specified an output location for basicmath , the app didn't know where to put anything inside that call to renderText() . Below is working code that gets the right UI elements to appear.

One other thing you were missing in your renderUI was the use of tagList() . This helps ensure that all of your elements are packaged together, not just the last one.

Note that the math part does not work, but it looks like that was just a placeholder. When you do start to use it, be sure to use unique ids for each input. You have several instances of input$a and input$b , which probably isn't a workable approach.

library(shiny)
library(shinythemes)

ui <- fluidPage( theme = shinytheme("slate"),
                 titlePanel("Basic Calculator"),
                 sidebarPanel(
                   selectInput("ops","Select what Operation use",choices = c("ADDITION","SQUARE")),
                   helpText("Please input the appropriate number depending on the operations"),
                   conditionalPanel("input.ops=='ADDITION'", uiOutput("var2")),
                   conditionalPanel("input.ops=='SQUARE'", uiOutput("var1"))
                 ),
                 mainPanel(
                   textOutput("basicmath")
                 )
)#fluidpage

server <- function(input, output) {

  output$var2 <- renderUI({
    tagList(
      helpText("this will show to input two numerics to be added"),
      splitLayout(
        numericInput("var2a", label = "Input one number", value = NULL),
        numericInput("var2b", label = "Input another number", value = NULL)
      )
    )
  })
  
  output$var1 <- renderUI({
    tagList(
      helpText("this will show to input one numeric to square"),
      numericInput("var1a", label = "Input a number", value = NULL)
    )
  })
  
  output$basicmath <- renderText( {ifelse(input$ops=="ADDITION",input$a+input$b,
                                         ifelse(input$ops=="SUBTRACTION",input$a-input$b,
                                                ifelse(input$ops=="SQUARE",input$a*input$a,
                                                       ifelse(input$ops=="SQUARE ROOT",sqrt(input$a),
                                                              ifelse(input$ops=="MULTIPLICATION",input$a*input$b,"not a valid operation")))))
  })
}

shinyApp(ui = ui, server = server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM