R shiny, shinyjs, remove plot and draw it again if button is clicked

点点圈 提交于 2019-12-23 17:07:15

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!