闪亮:renderText失败,并带有条件滑块

时间:2019-09-12 10:36:50

标签: shiny

我正在尝试学习Shiny,并且正在为一个问题而奋斗。

如果在条件面板内,则无法将由sliderInput确定的值呈现为文本。只是不明白如何解决这个问题。

最小示例

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)

如果滑块不是有条件的,它将起作用。反应性命令中缺少什么?我在教程中找到的outputOption命令,但没有任何区别。

我得到以下输出

  

您选择了二项式
  structure(function(...),{,if(length(outputArgs)!= 0 &&!hasExecuted $ get()){,warning(“未使用的参数:outputArgs。参数outputArgs仅是”,“在将Shiny代码片段嵌入到“,” R Markdown代码块中时使用(使用运行时:Shiny)。当运行“,”“完整的Shiny应用程序时,请直接在相应的输出函数”,“中设置输出参数UI代码。“),hasExecuted $ set(TRUE),},如果(is.null(formals(origRenderFunc))),origRenderFunc(),否则origRenderFunc(...),},class =” function“, outputFunc =函数(outputId,container = if(inline)span else div,,inline = FALSE),{,container(id = outputId,class =“ shiny-text-output”),},outputArgs = list(),hasExecuted =)

enter image description here

1 个答案:

答案 0 :(得分:1)

您在这里有两个反应性UI,一个用于滑块,另一个用于文本。但是只有一个人响应滑块condition。您希望两者都依赖于input$distr。一种方法是仅在input$distr != Poisson时呈现文本。

此外,我认为有人建议不要将render语句放在reactive语句之内或反之。因为render语句已经是反应性的。

因此,要仅在呈现滑块控件时将通过滑块选择的值显示为文本,可以执行以下操作:

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)
    }
})
}