将点击事件的反应值添加到现有数据框并更新绘图 [英] Add reactive values from click events to existing data frame and update plot

查看:79
本文介绍了将点击事件的反应值添加到现有数据框并更新绘图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是使用Shiny的新手,在将反应性值添加到现有数据帧时遇到一些问题.

I'm new to using Shiny and having some problems with adding reactive values to an existing data frame.

我想使用一些已有数据绘制带有回归线的散点图,并通过用户点击来更新它.我找到了一个有用的示例,但无法弄清楚如何在数据框中存储反应值以更新回归线.

I would like to plot a scatter plot with a regression line using some preexisting data and update it with user clicks. I found a helpful example, but I can't figure out how to store the reactive values in a data frame in order to update the regression line.

这是我尝试过的.谢谢.

Here is what I tried. Thank you.

    library(shiny)

ui <- basicPage(
  plotOutput("plot1", click = "plot_click"),
  verbatimTextOutput("info"),
  actionButton("updateplot", "Update Plot:"),
  actionButton("refreshlinie", "Rlinie"),
)

server <- function(input, output) {

  var1 <- c(3, 10, 15, 3, 4, 7, 1, 12, 8, 18, 20, 4, 4, 5, 10)
  var2 <- c(4, 10, 12, 17, 15, 20, 14, 3, 4, 15, 12, 5, 5, 6, 2)
  df <- cbind (var1, var2)

  val <- reactiveValues(clickx = NULL, clicky = NULL, data = NULL)
  observe({
    input$plot_click
    isolate({
      val$clickx = c(val$clickx, input$plot_click$x)
      val$clicky = c(val$clicky, input$plot_click$y)
      })

   # isolate({
      # val$data = rbind(df, c(val$clickx, val$clicky))   
    #})

  }) #adding clicks to list


  output$plot1 <- renderPlot({
    plot(val$data[,1], val$data[,2])
    input$updateplot
    isolate({
      points(val$clickx, val$clicky)
    })
      if(input$refreshlinie)
    abline(lm(val$data[,1]~ val$data[,2]))
  })

  output$info <- renderText({
    paste0("x = ", val$clickx, ", y = ",val$clicky, "\n")
  })

}

shinyApp(ui, server)

推荐答案

基本思想是使用预先存在的数据初始化反应性值.向后添加新添加的点到反应值.

The basic idea is to initialize the reactive values with the pre-existing data. Late add to the reactive values the new points added.

下面我将您代码的简化版本放入vla$data中的点,并正确地更新abline.

Bellow I put a simplified version of your code that update the points in the vla$data and correctly update the abline.

ui <- basicPage(
  plotOutput("plot1", click = "plot_click"),
  actionButton("updateplot", "Update Plot:"),
  actionButton("refreshlinie", "Rlinie"),
  verbatimTextOutput("info")
)

server <- function(input, output) {

  var1 <- c(3, 10, 15, 3, 4, 7, 1, 12, 8, 18, 20, 4, 4, 5, 10)
  var2 <- c(4, 10, 12, 17, 15, 20, 14, 3, 4, 15, 12, 5, 5, 6, 2)
  n <- length(var1)

  # initialize reactive values with existing data
  val <- reactiveValues( clickx = NULL, clicky = NULL, data = cbind (var1, var2))

  observe({
    input$plot_click
    isolate({
      # save new points added
      val$clickx = c(val$clickx, input$plot_click$x)
      val$clicky = c(val$clicky, input$plot_click$y)
      # add new points to data
      val$data <- rbind(val$data, cbind(input$plot_click$x, input$plot_click$y))
    })
  })


  output$plot1 <- renderPlot({
    input$updateplot
    plot(val$data[,1], val$data[,2])
    if(input$refreshlinie)
      # I changed the order of the variables to create the model.
      abline(lm(val$data[,2]~ val$data[,1]))
  })

  output$info <- renderText({
    input$plot_click
    paste0("x = ", val$clickx, ", y = ",val$clicky, "\n")
  })

}

shinyApp(ui, server)

这篇关于将点击事件的反应值添加到现有数据框并更新绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆