【发布时间】:2018-06-11 19:44:55
【问题描述】:
我正在尝试使 R Shiny: automatically refreshing a main panel without using a refresh button 适应一个新的最小工作示例:
ui <- fluidPage(
pageWithSidebar(
headerPanel("actionButton test"),
sidebarPanel(
numericInput("n", "N:", min = 0, max = 100, value = 50),
br(),
actionButton("goButton", "Go!"),
p("Click the button to update the value displayed in the main panel."),
actionButton("newButton", "New Button"),
actionButton("newButton2", "Another New Button")
),
mainPanel(
verbatimTextOutput("nText"),
textOutput("some_text_description"),
plotOutput("some_plot")
)
)
)
server <- function(input, output, session) {
# builds a reactive expression that only invalidates
# when the value of input$goButton becomes out of date
# (i.e., when the button is pressed)
ntext <- eventReactive(input$goButton, {
input$n
})
output$nText <- renderText({
ntext()
})
# Prep some text for output
output$some_text_description <- renderText({
if (input$newButton == 0) {return(NULL)}
else {
"Lorem ipsum dolorom."
}
})
# Prep some figure for output
# Simple Bar Plot
output$some_plot <- renderPlot({
if (input$newButton2 == 0) {return(NULL)}
else {
counts <- table(mtcars$gear)
barplot(counts, main="Car Distribution", xlab="Number of Gears")
}
})
}
shinyApp(ui = ui, server = server)
在上面的代码中,我有三个actionButton 命令,一个产生绘图,一个产生文本输出,一个产生一个数字(作为逐字文本输出)。当您单击每个按钮时,新的输出会出现在先前生成的输出旁边(来自您按下的最后一个按钮)。
无需实现手动清除所有内容的刷新按钮,我如何让每个actionButton 自动覆盖(即擦除)其他人的输出,而不会在主面板中将它们全部堆叠在一起。我的理解是我需要使用observeEvent、NULL 和reactiveValues 的某种组合,但到目前为止我的尝试都没有成功。
【问题讨论】: