R Shiny动作按钮用于开始和停止录制情节点击

时间:2016-12-11 05:32:13

标签: r shiny

我想在Shiny中为renderPlot()添加点。在Shiny之外的基本图形中,我可以使用定位器功能,但我无法弄清楚如何在Shiny中完成它。

类似于以下链接,但我想要一个按钮来启动绘图(并存储点击位置),另一个停止。 http://shiny.rstudio.com/gallery/dynamic-clustering.html

理想情况下,点击第一个按钮后,将无需重绘整个地图即可绘制地图上的连续点击(我有一个大图像作为绘图的背景,需要一段时间才能刷新)。单击停止记录点击位置的第二个按钮后,可以刷新整个绘图。

以下是我尝试过的一些内容:

ui <- fluidPage(

  titlePanel("Title"),

  sidebarLayout(
    sidebarPanel(width=3,
                 actionButton("startaddpoint", label = "Start"),
                 actionButton("stopaddpoint", label = "Stop"),
                 verbatimTextOutput("info")
    ),

    mainPanel(
      uiOutput("plot.ui")
    )

  )

)


server <- function(input, output, session) {
  options(shiny.maxRequestSize=100*1024^2) # set maximum image size

  xy_new <- reactiveValues(x= numeric(0), y = numeric(0), line=numeric(0)) # add new points

  output$plot.ui <- renderUI({
    plotOutput("distplot",
               click = "plot_click",
               dblclick = "plot_dblclick",
               hover = "plot_hover",
               brush = "plot_brush")
  })

  output$distplot <- renderPlot({


    plot(0, 0, xlim=c(-2, 2), ylim=c(-2, 2), xlab="", ylab="")

    # on Start, start plotting new clicks:
    if(input$startaddpoint > 0) {
      observe({
        isolate({
          xy_new$x <- c(xy_new$x, input$plot_click$x)
          xy_new$y <- c(xy_new$y, input$plot_click$y)
          # points(input$plot_click$x, input$plot_click$y)
        })
      })
    }
    points(xy_new$x, xy_new$y)

    # on Stop, stop plotting new clicks:
    # no idea here..

  })

  output$info <- renderText({
    xy_str <- function(e) {
      if(is.null(e)) return("NULL\n")
      paste0("x=", round(e$x, 2), " y=", round(e$y, 2), "\n")
    }
    xy_range_str <- function(e) {
      if(is.null(e)) return("NULL\n")
      paste0("xmin=", round(e$xmin, 2), " xmax=", round(e$xmax, 2), 
             " ymin=", round(e$ymin, 2), " ymax=", round(e$ymax, 2),
             " xrange=", round(e$xmax-e$xmin, 2), " yrange=", round(e$ymax-e$ymin,2),
             " diag=",round(sqrt((e$xmax-e$xmin)^2+(e$ymax-e$ymin)^2)))
    }

    paste0(
      "click: ", xy_str(input$plot_click),
      "dblclick: ", xy_str(input$plot_dblclick),
      "hover: ", xy_str(input$plot_hover),
      "brush: ", xy_range_str(input$plot_brush)
    )
  })
}

# Run the application 
shinyApp(ui = ui, server = server)

如果你看到了这样做的方法,请告诉我。 干杯

1 个答案:

答案 0 :(得分:0)

这是一种在反应式构造中执行此操作的方法。它没有使用“开始和停止”的听力方法。相反,它需要一个总是在监听新点击的观察者,并使用一个按钮来更新plotOutput的数据,从而使反应性端点无效并刷新图。

library(shiny)

ui <- fluidPage(

  titlePanel("Title"),

  sidebarLayout(
    sidebarPanel(width=3,
                 actionButton("plotpoints", label = "Plot"),
                 #actionButton("stopaddpoint", label = "Stop"),
                 verbatimTextOutput("info")
    ),

    mainPanel(
      uiOutput("plot.ui")
    )
  )
)

server <- function(input, output, session) {
  options(shiny.maxRequestSize=100*1024^2) # set maximum image size

  xy_new <- reactiveValues(x= numeric(0), y = numeric(0), line=numeric(0)) # add new points

  output$plot.ui <- renderUI({
    plotOutput("distplot",
               click = "plot_click",
               dblclick = "plot_dblclick",
               hover = "plot_hover",
               brush = "plot_brush")
  })

  # Listen for clicks and store values
  observe({
    if (is.null(input$plot_click)){
      return()
    }

    isolate({
      xy_new$x <- c(xy_new$x, input$plot_click$x)
      xy_new$y <- c(xy_new$y, input$plot_click$y)
    })
  })

  # Get the click values on button click
  pointsforplot <- eventReactive(input$plotpoints, ignoreNULL = F, {

    tibble(x = xy_new$x, y = xy_new$y)

  })

  output$distplot <- renderPlot({

    # Will update on button click, refreshing the plot
    coord <- pointsforplot()

    plot(coord$x, coord$y, xlim=c(-2, 2), ylim=c(-2, 2), xlab="", ylab="")

  })

  output$info <- renderText({
    xy_str <- function(e) {
      if(is.null(e)) return("NULL\n")
      paste0("x=", round(e$x, 2), " y=", round(e$y, 2), "\n")
    }
    xy_range_str <- function(e) {
      if(is.null(e)) return("NULL\n")
      paste0("xmin=", round(e$xmin, 2), " xmax=", round(e$xmax, 2), 
             " ymin=", round(e$ymin, 2), " ymax=", round(e$ymax, 2),
             " xrange=", round(e$xmax-e$xmin, 2), " yrange=", round(e$ymax-e$ymin,2),
             " diag=",round(sqrt((e$xmax-e$xmin)^2+(e$ymax-e$ymin)^2)))
    }

    paste0(
      "click: ", xy_str(input$plot_click),
      "dblclick: ", xy_str(input$plot_dblclick),
      "hover: ", xy_str(input$plot_hover),
      "brush: ", xy_range_str(input$plot_brush)
    )
  })
}

# Run the application 
shinyApp(ui = ui, server = server)