我有一个反应式表达式,我想从最近两个其他反应式表达式中的任何一个中取出的值。我做了以下例子:
ui.r:
shinyUI(bootstrapPage(
column(4, wellPanel(
actionButton("button", "Button"),
checkboxGroupInput("check", "Check", choices = c("a", "b", "c"))
)),
column(8,
textOutput("test")
)
))
和server.r:
shinyServer(function(input, output) {
output$test <- renderText({
# Solution goes here
})
})
我希望输出显示button
的值(按钮被点击的次数)或 check
(字符向量显示哪个选中的框取决于最近更改了。
答案 0 :(得分:5)
您可以使用reactiveValues
来实现此目的,以跟踪按下按钮的当前状态:
library(shiny)
runApp(list(ui = shinyUI(bootstrapPage(
column(4, wellPanel(
actionButton("button", "Button"),
checkboxGroupInput("check", "Check", choices = c("a", "b", "c"))
)),
column(8,
textOutput("test")
)
))
, server = function(input, output, session){
myReactives <- reactiveValues(reactInd = 0)
observe({
input$button
myReactives$reactInd <- 1
})
observe({
input$check
myReactives$reactInd <- 2
})
output$test <- renderText({
if(myReactives$reactInd == 1){
return(input$button)
}
if(myReactives$reactInd == 2){
return(input$check)
}
})
}
)
)