0
votes

I am trying to learn Shiny and I am banging my head on a problem.

I cannot render as text a value determined with a sliderInput if this is inside a conditional panel. Just can't understand how to fix this problem.

Minimal example

library(shiny)
ui <- fluidPage(
titlePanel("Plot my distribution"),

sidebarLayout(
sidebarPanel(
  helpText("Plot a distribution."),

  selectInput("distr", 
              label = "Choose a distribution",
              choices = c("Binomial", 
                          "Negative Binomial",
                          "Poisson"),
              selected = "Binomial"),
  conditionalPanel(
    condition = "input.distr != 'Poisson'",
    sliderInput("prob", 
                label = "Probability of success:",
                min = 0, max = 1, value = 0.5
    )
  )
),  
mainPanel(
  textOutput("selected_dist"),
  textOutput("prob")
)
)
)
server <- function(input, output) {

  output$selected_dist <- renderText({ 
    paste("You have selected", input$distr)
  })

  output$prob <- reactive({
    renderText({ 
      paste("You have chosen probability =", input$prob)
    })
  })
  outputOptions(output, "prob", suspendWhenHidden = FALSE)  
}
shinyApp(ui, server)

If the slider is not conditional it works. What's missing in the reactive command? The outputOption command I found in a tutorial, but doesn't mak any difference.

I get the following output

You have selected Binomial
structure(function (...) ,{, if (length(outputArgs) != 0 && !hasExecuted$get()) {, warning("Unused argument: outputArgs. The argument outputArgs is only ", , "meant to be used when embedding snippets of Shiny code in an ", , "R Markdown code chunk (using runtime: shiny). When running a ", , "full Shiny app, please set the output arguments directly in ", , "the corresponding output function of your UI code."), hasExecuted$set(TRUE), }, if (is.null(formals(origRenderFunc))) , origRenderFunc(), else origRenderFunc(...),}, class = "function", outputFunc = function (outputId, container = if (inline) span else div, , inline = FALSE) ,{, container(id = outputId, class = "shiny-text-output"),}, outputArgs = list(), hasExecuted = )

enter image description here

1
If your selectInput is "distr" shouldn't condition = "input.distr ...." too, and not condition = "input.distrib...?novica
Yes, right. But that was a typo in the question (sorry). fixed.Marco Stamazza
removing the reactive tag from output$prob solves the issue for me.novica
@novica yes, it does. I am really confused about the eactive environments! ThanksMarco Stamazza

1 Answers

1
votes

You have two reactive UIs here, one for the slider and another for the text. But only one responds to the condition, the slider. You want both to depend on input$distr. One way to do this is to render text only when input$distr != Poisson.

Also, I think there is a recommendation that render statements should not be inside reactive statements or the other way around. because a render statement is already reactive.

So, to show the value selected through the slider as text only when the slider widget is rendered, you can do:

server <- function(input, output) {

output$selected_dist <- renderText({ 
    paste("You have selected", input$distr)
})


output$prob <- renderText({
    if (!input$distr== "Poisson") {
        paste("You have chosen probability =", input$prob)
    }
})
}