【问题标题】:Wrong renderText output on use of conditionalPanel (shiny)使用 conditionalPanel 时出现错误的 renderText 输出(闪亮)
【发布时间】:2018-05-13 15:54:04
【问题描述】:

我在根据条件面板中的输入呈现输出时遇到问题。下面我编写了我的代码的修改版本,其中我删除了所有与问题无关的额外内容。

ui.R 是

library(shiny)

shinyUI(fluidPage(

titlePanel("Art and R"),

sidebarLayout(
    sidebarPanel(

        selectInput(
            "colspa", "Color space", choices = list("A" = "a", "B" = "b"), selected = 1
        ),  

        conditionalPanel(
            condition = "input.colspa == 'a'", selectInput(
                "colchoice", "Color choice", choices = list("ONE" = "one", "TWO" = "two", "THREE" = "three"), selected = 1
            )
        ),

        conditionalPanel(
            condition = "input.colspa == 'b'", selectInput(
                "colchoice", "Color choice", choices = list("FOUR" = "four", "FIVE" = "five", "SIX" = "six"), selected = 1
            )
        ),

        actionButton("btn", "Show!")
    ),

    mainPanel(
        textOutput("distPlot")
    )
)
))

而server.R是

library(shiny)

shinyServer(function(input, output) {
str2 <<- ""
str3 <<- ""
getResults1 <- observeEvent(input$btn, {
    str2 <<- (paste0(input$colspa))
})

getResults2 <- observeEvent(input$btn, {
    str3 <<- (paste0(input$colchoice))
})

calculate <- eventReactive(input$btn, {
    str1 <<- paste0(str2, str3)
    return(str1)
})

output$distPlot <- renderText({
    calculate()
})
})

如果我运行此应用程序,当colspa 为“a”时,我会得到正确的结果,但一旦我将 selectInput 中的colspa 更改为“b”,呈现的输出不是我想要的。下面是一个问题的例子。

【问题讨论】:

  • 而不是使用条件面板,您可以在服务器端使用updateSelectInput() 函数吗?我认为该应用程序很困惑,您有两个标记为“colchoice”的selectInputs,即使在任何时候只显示其中一个。
  • @JohnPaul 我之前尝试过,但似乎没有用。您能告诉我在上述情况下如何实现吗?

标签: r shiny reactive observers


【解决方案1】:

您不应该对两个不同的输出使用相同的 ID。失败的原因是“colchoice”仍然绑定到第一个 selectInput,因为它与第二个具有相同的 ID。以下是updateSelectInput 的工作示例。请注意,您需要在服务器中添加一个额外的 session 参数。

ui <- shinyUI(fluidPage(

  titlePanel("Art and R"),

  sidebarLayout(
    sidebarPanel(
      # first select input
      selectInput(
        "colspa", "Color space", choices = list("A" = "a", "B" = "b"), selected = "a"
      ),
      # second select input 
      selectInput(
          "colchoice", "Color choice", choices = list("ONE" = "one", "TWO" = "two", "THREE" = "three"), selected = "one"
      ),

      actionButton("btn", "Show!")
    ),

    mainPanel(
      textOutput("distPlot")
    )
  )
))

server <- shinyServer(function(input, output, session) {

  # change choices of input$colchoice depending on input$colspa
  observeEvent(input$colspa, {
    if(input$colspa == "a") myChoices <- c("ONE" = "one", "TWO" = "two", "THREE" = "three")
    else myChoices <- c("FOUR" = "four", "FIVE" = "five", "SIX" = "six")

    updateSelectInput(session, "colchoice", choices = myChoices)
  })


  # display selected choices upon click of input$btn

  calculate <- eventReactive(input$btn, {
    paste0(input$colspa, input$colchoice)
  })

  output$distPlot <- renderText({
    calculate()
  })
})
shinyApp(ui, server)

【讨论】:

  • 我以类似的方式解决了这个问题,但使用了 2 个按钮。你的更好。谢谢!
猜你喜欢
  • 2017-01-12
  • 2021-07-31
  • 2020-06-12
  • 1970-01-01
  • 1970-01-01
  • 2020-03-13
  • 2017-01-25
  • 2013-11-12
  • 2019-10-23
相关资源
最近更新 更多