我正在尝试将R Shiny:无需使用刷新按钮自动刷新主面板适应一个新的最小工作示例:
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
的某种组合,但我的尝试至今未果。