[英]Observe double click conditional on side panel input in R Shiny
我正在为项目设计一个Shiny应用程序,其中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针对先前的问题创建。)
找到了解决方案! 只需创建一个observeEvent()
来观察双击并使用新信息更新values$DT
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.