Using reactive expression in if expression in brilliant

I am writing a brilliant application in which the result should depend on the value of the variable, which is calculated in a reactive expression in brilliant form. Instead of playing the actual application, I believe I recreated my problem with the following simple application:

ui.R file: library(shiny) shinyUI(pageWithSidebar( headerPanel("Illustrating problem"), sidebarPanel( numericInput('id1', 'Numeric input, labeled id1', 0, min = 0, max = 100, step =5) ), mainPanel( h4('You entered'), verbatimTextOutput("oid1"), verbatimTextOutput("odic") ) )) server.R file library(shiny) shinyServer( function(input, output) { output$oid1 <- renderPrint({input$id1}) x<-reactive({5*input$id1}) if (x()>50) output$oid2<-renderPrint({"You entered a number greater than 10"}) else output$oid2<-renderPrint({"You entered a number less than or equal to 10"}) } ) 

If I run it like this, we get the error: Error .getReactiveEnvironment()$currentContext() : `

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

If I changed the if statement to: if (x>50) ..., then I get an error:

Error in x> 50: comparison (6) is possible only for atom types and lists

When I change the if: if (reactive({(x>50)})) ... statement ... then I get the error message:

Error in if (reactive ({: the argument is not interpreted as logical

I would really appreciate any help

+5
source share
2 answers

A couple of problems. The first and biggest problem is that you don't seem to understand how reactivity works. The error says that "the operation is not resolved without an active reactive context", and that the problem is that you are accessing x() inside the main object of the server function, but not within the reactive context. Any render* function is a reactive context, for example. So, to solve this problem, you just need to move the if statement inside renderPrint .

Another small problem is that your output identifiers do not match ( verbatimTextOutput("odic") vs output$oid2 ).

If you do not understand reactivity, I strongly recommend that you take half an hour to understand it better. This tutorial has a section on reactivity that might be useful, or you can re-read the official brilliant RStudio tutorial.

Here is the code for your fixed application (I removed a bunch of useless interface elements)

 library(shiny) ui <- fluidPage( numericInput('id1', 'Numeric input, labeled id1', 0, min = 0, max = 100, step=5), verbatimTextOutput("oid1"), verbatimTextOutput("oid2") ) server <- function(input, output, session) { output$oid1 <- renderPrint({input$id1}) x<-reactive({5*input$id1}) output$oid2<-renderPrint({ if (x()>50) { "You entered a number greater than 10" } else { "You entered a number less than or equal to 10" } }) } shinyApp(ui = ui, server = server) 
+8
source

Note that Shiny works on the basis of an event-driven model - like almost all graphically oriented user interfaces (and this is the vast majority today). This is not a new concept that has existed since the 80s at least, but it is more complicated than the programming that follows one flowchart - it gets used to it.

In any case, in the Shiny instructions, reactive and output and observe should be at the top level, I do not think there are exceptions to this.

You probably want something like this:

 library(shiny) u <- shinyUI(pageWithSidebar( headerPanel("Illustrating problem"), sidebarPanel( numericInput('id1', 'Numeric input, labeled id1', 0, min = 0, max = 100, step =5) ), mainPanel( h4('You entered'), verbatimTextOutput("oid1"), verbatimTextOutput("oid2") ) )) s <- shinyServer(function(input, output) { output$oid1 <- renderPrint({input$id1}) x<-reactive({5*input$id1}) output$oid2<-renderPrint({ if (x()>50){ "You entered a number greater than 10" } else { "You entered a number less than or equal to 10" } } ) } ) shinyApp(ui = u, server = s) 

Yielding:

enter image description here

+3
source

Source: https://habr.com/ru/post/1240733/


All Articles