Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

shiny

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?

library(shiny)

ui<-fluidPage(

  numericInput(inputId="a", label=NULL, value=0),
  textOutput(outputId="out")
)

server <- function(input, output) {
  x <- reactive(input$a)
  if (x() < 4) 
    {y<-1}
  else
  {y<-0}

  output$out <- renderText({y})
}

shinyApp(ui = ui, server = server)

The error message:

Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)

like image 902
Lee88 Avatar asked Jun 08 '16 18:06

Lee88


People also ask

How do you use reactive in Shiny R?

A reactive expression is an R expression that uses widget input and returns a value. The reactive expression will update this value whenever the original widget changes. To create a reactive expression use the reactive function, which takes an R expression surrounded by braces (just like the render* functions).

What are reactive values in R Shiny?

Reactive values contain values (not surprisingly), which can be read by other reactive objects. The input object is a ReactiveValues object, which looks something like a list, and it contains many individual reactive values. The values in input are set by input from the web browser.

What does the renderPlot function do in Shiny?

renderPlot is an reactive function that can take input data from the ui. R script and feed it into the server. R script. It then actively updates the information within its function.

What is reactive programming Shiny?

Reactive programming is an elegant and powerful programming paradigm, but it can be disorienting at first because it's a very different paradigm to writing a script. The key idea of reactive programming is to specify a graph of dependencies so that when an input changes, all related outputs are automatically updated.


2 Answers

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)
like image 162
John Paul Avatar answered Sep 21 '22 12:09

John Paul


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)
like image 36
timelyportfolio Avatar answered Sep 17 '22 12:09

timelyportfolio