Shiny:基于鼠标xy输入交互式添加绘图,并报告它们并分别对其进行操作

Shiny: Interactively add plots based on mouse xy input, and report them and do operations on them separately

我有

ui <- fluidPage(
  
  # App title ----
  titlePanel("Interactive CLicks"),   
  br(),
  fluidRow(
    br(),
    verbatimTextOutput("info_hover"),
    plotOutput(outputId = "distPlot",hover = "plot_hover",click= "plot_click", dblclick = "plot_db_click",height = 500, width = 1600 ),
    br(),
    verbatimTextOutput("info_click"),
    verbatimTextOutput("info_db_click"),    
    verbatimTextOutput("double_to_single_click"))
    
)




## server.R
server <- function( input, output, session){
  
  #Click Points
   single.source_coords <- reactiveValues(xy=data.frame(x=c(1,1),  y=c(1,1)))
   double.source_coords <- reactiveValues(xy=data.frame(x=c(1,1),  y=c(1,1)))
   
   
   observeEvent(input$plot_click, {
     single.source_coords$xy[2,] <- c(input$plot_click$x, input$plot_click$y)
   })
   
   
   observeEvent(input$plot_click, {
     double.source_coords$xy[2,] <- c(input$plot_db_click$x, input$plot_db_click$y)
   })
   
  
  ## RenderPlot 
  output$distPlot <- renderPlot({ 
    plot(1, 1,
      xlim=c(0,10), ylim=c(0,10))

#Click points
    points( single.source_coords$xy[2,1], single.source_coords$xy[2,2], cex=3, pch=17) 
     points( double.source_coords$xy[2,1], double.source_coords$xy[2,2], cex=3, pch=17) 
    segments(x0 =single.source_coords$xy[2,1], y0 = single.source_coords$xy[2,2], x1 =  double.source_coords$xy[2,1], y1 = double.source_coords$xy[2,2], col = "darkred", lwd = 2, lty = 3)
    
    
  })        
  
  output$info_click <- renderText({
    paste0("Single Click Multiple", "\nX=", input$plot_click$x*2, "     Y= ",input$plot_click$y*2)
  })
  
  output$info_db_click <- renderText({
    paste0("Double Click Multiple", "\nX=", input$plot_db_click$x*2, "     Y= ",input$plot_db_click$y*2)
  })
  
  output$info_hover <- renderText({
    paste0("Hover Click Multiple", "\nX=", input$plot_hover$x*2, "     Y= ",input$plot_hover$y*2)
  })
  
  
  
  
}

### Run Application
shinyApp(ui, server)

所以我想点击操作添加一个点,双击添加另一个点,同时保持verbatimTextOutput()

中值的显示和对值的一些操作

我似乎无法让它工作。我收到 Warning: Error in <-: replacement has length zero

同样点击和双击各只放置一个点。如果我单击 (x1,y1) 并单击 (x1,y2),则该点应从位置 1 移动到位置 2。

我错过了什么?

我认为您在第二个 observeEvent 中有错字(还是 input$plot_click 而不是 input$plot_db_click)。这解释了你的错误。其次,对于您的 verbatimTextOutput 我认为您应该参考反应值。这对你有用吗,还是我没有达到你的目的?

library(shiny)

ui <- fluidPage(
  
  # App title ----
  titlePanel("Interactive CLicks"),   
  br(),
  fluidRow(
    br(),
    verbatimTextOutput("info_hover"),
    plotOutput(outputId = "distPlot",hover = "plot_hover",click= "plot_click", dblclick = "plot_db_click",height = 500, width = 1600 ),
    br(),
    verbatimTextOutput("info_click"),
    verbatimTextOutput("info_db_click"),    
    verbatimTextOutput("double_to_single_click"))
  
)




## server.R
server <- function( input, output, session){
  
  #Click Points
  single.source_coords <- reactiveValues(xy=data.frame(x=c(1,1),  y=c(1,1)))
  double.source_coords <- reactiveValues(xy=data.frame(x=c(1,1),  y=c(1,1)))
  
  
  observeEvent(input$plot_click, {
    single.source_coords$xy[2,] <- c(input$plot_click$x, input$plot_click$y)
  })
  
  
  observeEvent(input$plot_db_click, {
    double.source_coords$xy[2,] <- c(input$plot_db_click$x, input$plot_db_click$y)
  })
  
  
  ## RenderPlot 
  output$distPlot <- renderPlot({ 
    plot(1, 1,
         xlim=c(0,10), ylim=c(0,10))
    
    #Click points
    points( single.source_coords$xy[2,1], single.source_coords$xy[2,2], cex=3, pch=17)
    points( double.source_coords$xy[2,1], double.source_coords$xy[2,2], cex=3, pch=17)
    segments(x0 =single.source_coords$xy[2,1], y0 = single.source_coords$xy[2,2], x1 =  double.source_coords$xy[2,1], y1 = double.source_coords$xy[2,2], col = "darkred", lwd = 2, lty = 3)

    
  })        
  
  output$info_click <- renderText({
    paste0("Single Click Multiple", "\nX=", single.source_coords$xy[2,1], "     Y= ",single.source_coords$xy[2,2])
  })

  output$info_db_click <- renderText({
    paste0("Double Click Multiple", "\nX=", double.source_coords$xy[2,1], "     Y= ",double.source_coords$xy[2,2])
  })

  output$info_hover <- renderText({
    paste0("Hover Click Multiple", "\nX=", input$plot_hover$x*2, "     Y= ",input$plot_hover$y*2)
  })
  
  
  
  
}

### Run Application
shinyApp(ui, server)