I am writing a shiny app where the output should depend on the value of a variable which is calculated in a reactive expression in shiny. Instead of reproducing the actual app I believe I have recreated my problem with the following simple app:
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 then I get the error:
Error in .getReactiveEnvironment()$currentContext()
:`
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.)
If I change the if statement to: if (x>50)
... then I get the error:
Error in x > 50 : comparison (6) is possible only for atomic and list types
When I change the if statement to: if (reactive({(x>50)}))
... then I get the error message:
Error in if (reactive({ : argument is not interpretable as logical
I'd greatly appreciate any help