我有兴趣开发一个以下列方式行事的Shiny App:
actionButton
/ isolate
构造完成的。observeEvent
构造应更新警告消息,然后传递给renderText
。renderText
可以返回空字符串。actionButton
应将警告消息字符串值还原为空c("")
。 可以使用insertUI
/ renderUI
的替代方法。 app.R
library(shiny)
ui <- fluidPage(titlePanel("Reactive UI"),
sidebarLayout(
sidebarPanel(
# Reactive text should appear upon any change here
sliderInput(
"bins",
"Number of bins:",
min = 1,
max = 50,
value = 30
),
checkboxInput(
inputId = "chckBxLg",
label = "Log scale",
value = FALSE
)
,
actionButton("btnUpdate", "Update")
),
mainPanel(textOutput("msgTxt"),
plotOutput("distPlot"))
))
# Define server logic required to draw a histogram
server <- function(input, output) {
# Create placeholder object for msg
msgUpdateText <- c("")
# Insert update message text upon change to UI elements
observeEvent(eventExpr = {
input$bins
input$chckBxLg
},
handlerExpr = {
# This message should only show after interaction with the UI
isolate(msgUpdateText <<-
c("You have clicked something, update the chart"))
})
# Render text
output$msgTxt <- renderText(msgUpdateText)
output$distPlot <- renderPlot({
input$btnUpdate
isolate({
x <- faithful[, 2]
if (input$chckBxLg) {
x <- log(x)
}
bins <-
seq(min(x), max(x), length.out = input$bins + 1)
# Also delete the text message
msgUpdateText <<- c("")
})
hist(x,
breaks = bins,
col = 'darkgray',
border = 'white')
})
}
shinyApp(ui = ui, server = server)
消息:
您点击了某些内容,更新了图表
只应在用户与用户界面互动后显示 , {{后 消失 按下1}} ,而信息永久可见。
提供的解决方案应该可以跨多个UI元素进行扩展。提供的 not-working,示例尝试捕获对两个UI元素的更改:
actionButton
我正在努力使代码能够容纳大量元素
observeEvent(eventExpr = {
input$bins # Element 1
input$chckBxLg # Element 2
},
handlerExpr = {
# This message should only show after interaction with the UI
isolate(msgUpdateText <<-
c("You have clicked something, update the chart"))
})
答案 0 :(得分:2)
我想我已经设法达到了预期的效果。我已将必要的逻辑带入3 observeEvent
次调用。第一个观察到任何输入变化并将变量设置为TRUE
。第二个观察updatebutton并将变量设置为FALSE
。第三个观察输入和更新按钮并根据变量呈现警告消息(如果它打印出来TRUE
,否则它是空的)。
我发现的唯一问题是,它现在开始显示警告信息,但我还没有找到原因。
最终代码:
library(shiny)
ui <- fluidPage(titlePanel("Reactive UI"),
sidebarLayout(
sidebarPanel(
# Reactive text should appear upon any change here
sliderInput(
"bins",
"Number of bins:",
min = 1,
max = 50,
value = 30
),
checkboxInput(
inputId = "chckBxLg",
label = "Log scale",
value = FALSE
)
,
actionButton("btnUpdate", "Update")
),
mainPanel(uiOutput("msgui"),
plotOutput("distPlot"))
))
# Define server logic required to draw a histogram
server <- function(input, output) {
# Initialize the value to check if a change happened
Changebool <<- FALSE
observeEvent(eventExpr = { #When an input is changed
input$bins
input$chckBxLg
},
handlerExpr = { # Change the changebool to TRUE
Changebool <<- TRUE
}
)
observeEvent(eventExpr = { #When the update button is pressed
input$btnUpdate
},
handlerExpr = { # Change the changebool to FALSE
Changebool <<- FALSE
}
)
observeEvent({input$btnUpdate # If any of the inputs change or the update button is pressed
input$bins
input$chckBxLg},
{ # Recreate the message-ui
output$msgui <- renderUI({
if (Changebool) { # if a change has happened since last update
textOutput("msgTxt") #Output text
} else { #otherwise
#Output nothing
}
})
})
# Render text
output$msgTxt <- renderText("You have clicked something, update the chart")
output$distPlot <- renderPlot({
input$btnUpdate
isolate({
x <- faithful[, 2]
if (input$chckBxLg) {
x <- log(x)
}
bins <-
seq(min(x), max(x), length.out = input$bins + 1)
})
hist(x,
breaks = bins,
col = 'darkgray',
border = 'white')
})
}
shinyApp(ui = ui, server = server)
我必须承认我在这方面来回摆弄相当多,所以如果你发现代码中有任何奇怪的东西可以随意发表评论