2

What I really want to do is pass a numeric output condition for generating a conditional panel. I can't seem to render an output as numeric though.

My work around was to generate a reactive using the numeric condition of interest and pass that to the conditional panel as text. However, I'm struggling with this as well.

Below is an example:

the goal being to open a conditional panel whenever the user enters a number that when multiplied by 5 is greater than 100 YES - I know I could do this by setting input$number >20 This is a simplified example of what I'm working on. The issue is using numeric OUTPUT for a conditional panel

Thanks!

Sam

library(shiny)

shinyApp(
  ui = fluidPage(
    fluidRow(
            wellPanel(
              numericInput("number", label = "Choose a Number between -100 and 100", 
                           min = -100, max = 100, value = 0))),
    fluidRow(wellPanel(textOutput("text_out"))),
    fluidRow(conditionalPanel(
      condition = "output.big == '1' ", 
      fluidRow(h1("You've selected a number larger than 20!"))))),
  server = function(input, output, session){
    # I want to execute a function on a numeric input
    times_5<-reactive({ 
      input$number*5
      })   
    # I can't get the condition to work with a numeric query
    # So, I run the query on the server side and generate an if/else statement 
    # to pass a text variable
    over_100<-reactive({
      if(times_5()>100){
        return(1)
      } else{
        return(0)
      }
    })

    output$text_out<-renderText({
       paste("This is text out put of the number ", input$number, "multiplied by 5 to equal ", times_5(), over_100())
    })
    # I can't find a render function that generates numeric output
    # Even still this outputtext doesn't seem to work in the conditional statement.
    output$big<-renderText({
      over_100
    })
  }
)

1 Answer 1

3

At first it seems that Shiny needs to have the value displayed to use it in a condition, but this is not actually true:

outputOptions(output, 'big', suspendWhenHidden=FALSE)

You will tell Shiny to consider that value also when it's not visible.

Even if you add this line in your server function, you need another change: the value of big needs to be calculated, so this:

output$big<-renderText({
  over_100
})

has to become this:

output$big<-renderText(over_100())

Hope this can help you.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I keep forgetting the () when I call a reactive. The outputOptions was just what I needed.
outputOptions(output, 'big', suspendWhenHidden=FALSE) is such a biggie. Thanks @mucio .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.