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

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

    here's my attempt. 1) as stated, you don't need to wrap input$a in reactive context and save as x. just use input$a 2) you don't need reactiveValues in this simple example. just save y as a reactive variable. then, in the renderText, access by calling the function ("y()")

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

提交回复
热议问题