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

前端 未结 3 900
轻奢々
轻奢々 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:34

    The answer above from John Paul is certainly acceptable, but I thought you might like to see another way as a part of your learning process. I will let StackOverflow sort out which is more advisable.

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

提交回复
热议问题