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 = )
selectInput
is"distr"
shouldn'tcondition = "input.distr ...."
too, and notcondition = "input.distrib...
? – novicaoutput$prob
solves the issue for me. – novica