[英]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.