【问题标题】:Shiny: renderText fails with conditional slidebar闪亮:renderText 因条件滑动条而失败
【发布时间】:2020-01-14 05:24:09
【问题描述】:

我正在努力学习 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 代码的 sn-ps 嵌入到 ", , "R Markdown 代码块中(使用运行时:shiny)。运行 ", , "完整的 Shiny 应用程序时,请直接在 ", , " 对应的UI 代码的输出函数。"), 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() , 已执行 = )

【问题讨论】:

  • 如果你的selectInput"distr" 也不应该是condition = "input.distr ....",而不是condition = "input.distrib...
  • 是的,没错。但这是问题中的错字(对不起)。固定。
  • output$prob 中删除反应式标签为我解决了这个问题。
  • @novica 是的,确实如此。我真的对活动环境感到困惑!谢谢

标签: shiny


【解决方案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)
    }
})
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-08
    • 1970-01-01
    • 2015-09-12
    • 2017-01-08
    • 1970-01-01
    • 1970-01-01
    • 2015-04-14
    相关资源
    最近更新 更多