我有一个应用程序,一旦用户点击一个特定的按钮(“运行k-means”),就会用旧的Faithul数据绘制k-means集群。现在,我想添加一个按钮,再次删除绘图(“删除绘图”)。关注Hide/show outputs Shiny R 我试过这个:
library(shiny)
ui <- fluidPage(
actionButton(inputId = "run_kmeans", label = "run k-means"),
actionButton(inputId = "remove_plot", label = "remove plot"),
conditionalPanel("output.show", plotOutput("plot"))
)
server <- function(input, output) {
v <- reactiveValues(show = TRUE)
clusters <- eventReactive(input$run_kmeans, {
kmeans(faithful, 2)
v$show <- TRUE
})
output$plot <- renderPlot({plot(faithful, col = clusters()$cluster)})
output$show <- reactive({
return(v$show)
})
observeEvent(input$remove_plot, {v$show <- 0})
}
shinyApp(ui, server)
不幸的是,现在“运行k-means”按钮不再起作用了。因此,我也通过shinyjs
尝试了它:
library(shiny)
library(shinyjs)
ui <- fluidPage(
useShinyjs(),
actionButton(inputId = "run_kmeans", label = "run kmeans"),
actionButton(inputId = "remove_plot", label = "remove plot"),
plotOutput("plot")
)
server <- function(input, output) {
clusters <- eventReactive(input$run_kmeans, {kmeans(faithful, 2)})
output$plot <- renderPlot({show(plot(faithful, col = clusters()$cluster))})
observeEvent(input$remove_plot, {hide("plot")})
}
shinyApp(ui, server)
但现在情节被“永远”隐藏,如果点击“运行k-means”,则不再显示 - 我是否错误地放置了show()
函数?谁可以用这两种方法中的任何一种(或两种方法)帮助我?