如何使用带有光泽的条件语句

时间:2016-02-07 04:26:16

标签: r shiny

我想将一些文本变量传递给server.R中的条件语句,但我收到此错误。

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

这是我的ui.R和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"))

    )
  )
))

上述程序的逻辑是,如果卷号小于250,则使用名称打印 hello

1 个答案:

答案 0 :(得分:1)

input$txt1放入reactive,以便更轻松地使用。 修改:如果您只想控制1个被动反应,则只能查看test()并根据该变量执行条件语句

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)
相关问题