How to create IF statement with reactive values in R ( Shiny )

前端 未结 3 901
轻奢々
轻奢々 2021-01-18 02:47

Beginner to R and shiny here! Tried to make a minimal working example... I want to check a condition on a reactive input value. What am I doing wrong?

librar         


        
3条回答
  •  天涯浪人
    2021-01-18 03:35

    You just need to use reactive with your if so that shiny knows that y changes when x does.

    library(shiny)
    
    ui<-fluidPage(
    
      numericInput(inputId="a", label=NULL, value=0),
      textOutput(outputId="out")
    )
    
    server <- function(input, output) {
      x <- reactive(input$a)
      y <- reactive( if (x()<4) 1 else 0 )
    
      output$out <- renderText({ y() })
    }
    
    shinyApp(ui = ui, server = server)
    

提交回复
热议问题