【问题标题】:Observe double click conditional on side panel input in R Shiny观察 R Shiny 中侧面板输入的双击条件
【发布时间】:2018-03-16 17:07:20
【问题描述】:

我正在为一个项目开发一个闪亮的应用程序,其中 ggplot 是用户的主界面。根据来自侧边栏的输入,我希望该应用程序记录两个事件的坐标:单击(我已经工作)或双击(这是我卡住的地方)。本质上,我希望能够创建一种基于侧边栏条件记录起点和终点的方法。这是一个简短的例子:

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)

在此示例中,当用户选择绿色或蓝色时,我只想将单击记录为起点,并将NA 记录为终点。当他们选择粉红色时,我想将单击记录为起点,将双击记录为终点。任何帮助将不胜感激!

(由@blondeclover 在a question from earlier 创建的示例。)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    找到了解决办法!只需创建一个observeEvent() 以观察双击并使用新信息更新values$DT

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-04
      • 2013-11-03
      • 2016-08-14
      • 1970-01-01
      • 2014-11-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多