Eka
Eka

Reputation: 15000

How to use conditional statement with shiny

I want to pass some text variables to conditional statement in server.R but I am receving this error.

Listening on http://127.0.0.1:4781
Warning: Error in ==: comparison (1) is possible only for atomic and list types
Stack trace (innermost first):
    40: server [/home/..../server.r#10]
     1: shiny::runApp
Error in com == 1 : 
  comparison (1) is possible only for atomic and list types

This is my ui.R and server.R

server.R

# server.R

shinyServer(function(input, output) {

 com=renderText(
    ifelse(input$txt1 <= 250,1,0)
  )  


 if(com==1)
 { 
   output$text1=renderText({paste("Hello", input$txt2)})  }
   #.....
   #...Other output like output$plot,output$summary

}
)

ui.R

# ui.R

shinyUI(fluidPage(
  titlePanel("Test"),

  sidebarLayout(
    sidebarPanel(
      helpText("Test UI"),

      textInput("txt1","Enter the Roll Number",""),
      textInput("txt2","Enter the Name","John"),
#       selectInput("var", 
#                   label = "Choose a variable to display",
#                   choices = c("Percent White", "Percent Black",
#                               "Percent Hispanic", "Percent Asian"),
#                   selected = "Percent White"),
#       dateInput("date_start", h4("Start Date"), value = "2005-01-01" ,startview = "year"),
      sliderInput("range", 
                  label = "Range of interest:",
                  min = 0, max = 100, value = c(0, 100))
     ),

    mainPanel(
      tabsetPanel(type="tab",tabPanel("Console", textOutput("text1")),tabPanel("Summary"))

    )
  )
))

The logic for above programme is if roll number is less than 250 then print hello with name

Upvotes: 1

Views: 3718

Answers (1)

Pork Chop
Pork Chop

Reputation: 29387

Put your input$txt1 into a reactive so it is easier to work with. Edit: if you want to control only 1 reactive you can look at only test() and do your conditional statements based on that variable

rm(list = ls())
library(shiny)

ui <- shinyUI(
  fluidPage(tags$style(type="text/css",".shiny-output-error { visibility: hidden; }",".shiny-output-error:before { visibility: hidden; }"),
    titlePanel("Test"),
    sidebarLayout(
      sidebarPanel(
        helpText("Test UI"),
        textInput("txt1","Enter the Roll Number",""),
        textInput("txt2","Enter the Name","John"),
        sliderInput("range", label = "Range of interest:",min = 0, max = 100, value = c(0, 100))
      ),
      mainPanel(
        tabsetPanel(type="tab",tabPanel("Console", textOutput("text1"), plotOutput("plot")),tabPanel("Summary"))
      )
    )
  )
)

server <- function(input, output, session) {

  test <- reactive({     
    my_number <- as.numeric(input$txt1)
    ifelse(my_number <= 250,1,0)
  }) 

  output$text1 <- renderText({
  if(test()== 1){
      paste("Hello",input$txt2)
    }})

  output$plot <- renderPlot({
    if(test()== 0){
      plot(mtcars$wt, mtcars$mpg)}  
  })
}
shinyApp(ui = ui, server = server)

Upvotes: 1

Related Questions