gpt4 book ai didi

r - 在 Shiny 中选择最近更改的响应式(Reactive)表达式

转载 作者:行者123 更新时间:2023-12-04 11:14:22 25 4
gpt4 key购买 nike

我有一个响应式(Reactive)表达式,我想从最近更改的其他两个响应式(Reactive)表达式中的任何一个中获取其值。我做了以下例子:

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 (显示选中哪些框的字符向量)取决于最近更改的内容。

最佳答案

您可以使用 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)
}
})
}
)
)

enter image description here

关于r - 在 Shiny 中选择最近更改的响应式(Reactive)表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25672304/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com