问题
I have an app that plots the Old Faithul data with k-means clusters once the user clicks on a specific button ("run k-means"). Now, I want to add a button that removes the plot again ("remove plot"). Following Hide/show outputs Shiny R I tried this:
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)
Unfortunately, now the "run k-means" button does not work anymore. Hence, I also tried it via 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)
But now the plot is hidden "forever" and not shown again if "run k-means" is clicked - did I place the show()
function wrongly? Who can help me with either one of the two approaches (or both)?
来源:https://stackoverflow.com/questions/42795524/r-shiny-shinyjs-remove-plot-and-draw-it-again-if-button-is-clicked