简体   繁体   English

观察R Shiny中侧面板输入的条件双击

[英]Observe double click conditional on side panel input in R Shiny

I'm working on a Shiny app for a project where a ggplot is the main interface for the user. 我正在为项目设计一个Shiny应用程序,其中ggplot是用户的主要界面。 Depending on input from the sidebar, I'd like the app to record coordinates for two events: a single click (which I have working), or a double click (which is where I'm stuck). 根据侧边栏的输入,我希望该应用程序记录两个事件的坐标:单击(我正在工作)或双击(这是我被卡住的地方)。 Essentially, I'd like to be able to create a way to record a starting and ending point based on sidebar conditions. 本质上,我希望能够基于边栏条件创建一种记录起点和终点的方法。 Here's a brief example: 这是一个简单的示例:

library(shiny)
library(ggplot2)

ui = pageWithSidebar(
  headerPanel("Example"),
  sidebarPanel(
    radioButtons("color", "Pick Color", c("Pink", "Green", "Blue")),
    selectInput("shape", "Select Shape:", c("Circle", "Triangle"))
  ),
  mainPanel(
    fluidRow(column(width = 6,
                    h4("Click plot to add points"),
                    plotOutput("plot1", click = "plot_click"),
                    actionButton("rem_point", "Remove Last Point")),
             column(width = 6,
                    h4("Table of points on plot"),
                    tableOutput("table")))
  )
)

server = function(input, output){

  values = reactiveValues()
  values$DT = data.frame(x = numeric(),
                         y = numeric(),
                         color = factor(),
                         shape = factor())

  output$plot1 = renderPlot({
    ggplot(values$DT, aes(x = x, y = y)) +
      geom_point(aes(color = color,
                     shape = shape), size = 5) +
      lims(x = c(0, 100), y = c(0, 100)) +
      theme(legend.position = "bottom") +
      scale_color_discrete(drop = FALSE) +
      scale_shape_discrete(drop = FALSE)
  })

  observeEvent(input$plot_click, {
    add_row = data.frame(x = input$plot_click$x,
                         y = input$plot_click$y,
                         color = factor(input$color, levels = c("Pink", "Green", "Blue")),
                         shape = factor(input$shape, levels = c("Circle", "Triangle")))
    values$DT = rbind(values$DT, add_row)
  })

  observeEvent(input$rem_point, {
    rem_row = values$DT[-nrow(values$DT), ]
    values$DT = rem_row
  })

  output$table = renderTable({
    values$DT[, c('color', 'shape')]
  })
}

shinyApp(ui, server)

In this example, when the user selects Green or Blue, I'd like to only record the single click as the starting point and record NA for the end point. 在此示例中,当用户选择“绿色”或“蓝色”时,我只想将单击记录为起点,并为终点记录NA When they select Pink, I'd like to record the single click as the starting point and the double click as the ending point. 当他们选择“粉红色”时,我想将单击记录为起点,将双击记录为终点。 Any help would be greatly appreciated! 任何帮助将不胜感激!

(Example created by @blondeclover on a question from earlier .) (示例由@blondeclover针对先前的问题创建。)

Found a solution! 找到了解决方案! Just create an observeEvent() to observe a double click and update values$DT with the new information. 只需创建一个observeEvent()来观察双击并使用新信息更新values$DT

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM