R闪亮的操作按钮,用于开始和停止录制打印单击

R闪亮的操作按钮,用于开始和停止录制打印单击,r,shiny,R,Shiny,我想在Shiny中为renderPlot()添加点。在Shinny之外的基本图形中,我可以使用定位器功能,但我不知道如何在Shinny中实现它 与下面的链接类似,但我希望有一个按钮启动绘图(并存储单击位置),另一个按钮停止 理想情况下,单击第一个按钮后,可以在地图上连续单击,而无需重新绘制整个地图(我有一个大图像作为绘图的背景,刷新需要一段时间)。单击停止记录单击位置的第二个按钮后,可以刷新整个绘图 以下是我尝试过的一些方法: ui <- fluidPage( titlePanel(

我想在Shiny中为renderPlot()添加点。在Shinny之外的基本图形中,我可以使用定位器功能,但我不知道如何在Shinny中实现它

与下面的链接类似,但我希望有一个按钮启动绘图(并存储单击位置),另一个按钮停止

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

以下是我尝试过的一些方法:

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)

ui这里有一种在反应式构造中实现这一点的方法。它不是使用“开始和停止”的倾听方法。相反,它需要一个始终在侦听新单击的观察者,并使用按钮更新
绘图输出的数据,从而使反应端点无效并刷新绘图

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)
库(闪亮)

ui这里有一种在反应式构造中实现这一点的方法。它不是使用“开始和停止”的倾听方法。相反,它需要一个始终在侦听新单击的观察者,并使用按钮更新
绘图输出的数据,从而使反应端点无效并刷新绘图

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)
库(闪亮)

ui嗨,对你的评论回复太晚表示歉意;我不在。你的答案很好,但我特别想找到一种方法,只记录一些点击的情节。我希望能够选择存储哪些单击以进行打印。我会再考虑一下,你让我走上了正确的道路。CheersHi,对你的评论回复太晚表示歉意;我不在。你的答案很好,但我特别想找到一种方法,只记录一些点击的情节。我希望能够选择存储哪些单击以进行打印。我会再考虑一下,你让我走上了正确的道路。干杯