我有一个反应式表达式,我想从最近两个其他反应式表达式中的任何一个中取得的值.我做了以下示例:
ui.r:
shinyUI(bootstrapPage(
column(4, wellPanel(
actionButton("button", "Button"),
checkboxGroupInput("check", "Check", choices = c("a", "b", "c"))
)),
column(8,
textOutput("test")
)
))
Run Code Online (Sandbox Code Playgroud)
和server.r:
shinyServer(function(input, output) {
output$test <- renderText({
# Solution goes here
})
})
Run Code Online (Sandbox Code Playgroud)
我希望输出显示任意一个的值button
(按钮被点击的次数)或 check
(一个字符向量显示哪些框被选中),具体取决于最近更改的内容.
您可以通过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)
}
})
}
)
)
Run Code Online (Sandbox Code Playgroud)