shiny: refresh plot to add new points

爱⌒轻易说出口 提交于 2021-02-07 08:42:02

问题


I have a plot of waves where I need to identify each peak in the curve:

I would like to do the following:

Reactively add points to the plot where I click to mark the presence of each peak.

ui.R
plotOutput("plot1", click = "plot_click")

server.R
output$plot1 <- renderPlot({
  plot(x,y)
  points(x=input$plot_click$x,y=input$plot_click$y)
})

The problem here is that although the 'plot_click' mechanism identifies the x and y position of the points- the 'points()' command only causes points to appear momentarily and then disappear.

I also tried reactivePlot but this returned the error:

could not find function "func"

回答1:


Sorted it. Based on the help of a previous post: avoid double refresh of plot in shiny

library(shiny)
ui <- basicPage(
  actionButton("submit","submit"),
  plotOutput("plot1", click = "plot_click"),
  verbatimTextOutput("info"),
  tableOutput('table')
)

server <- function(input, output) {
  click_saved <- reactiveValues(singleclick = NULL)
  observeEvent(eventExpr = input$plot_click, handlerExpr = { click_saved$singleclick <- input$plot_click })
  rv=reactiveValues(m=data.frame(x=0,y=0))
  output$plot1 <- renderPlot({
    plot(x,y, type='l')
    points(rv$m$x[-1],rv$m$y[-1])
  })

  output$info <- renderText({
    paste0(unlist(click_saved$singleclick))
  })


  observeEvent(input$submit, {
    if (input$submit > 0) {
      rv$m <- rbind(rv$m,unlist(click_saved$singleclick))
    }
  })

  output$table <- renderTable({
    if (is.null(rv$m)) {return()}
    print(rv$m)
  }, 'include.rownames' = FALSE
  , 'include.colnames' = TRUE
  )

}

shinyApp(ui, server)


来源:https://stackoverflow.com/questions/45042155/shiny-refresh-plot-to-add-new-points

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