【发布时间】: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