我想在Shiny中为renderPlot()添加点 . 在Shiny之外的基本图形中,我可以使用定位器功能,我无法弄清楚如何在Shiny中完成它 .
类似于以下链接,但我想要一个按钮来启动绘图(并存储点击位置),另一个停止 . http://shiny.rstudio.com/gallery/dynamic-clustering.html
理想情况下,点击第一个按钮后,将绘制 Map 上的连续点击,而不必重绘整个 Map (我有一个大图像作为绘图的背景,需要一段时间刷新) . 单击停止记录单击位置的第二个按钮后,可以刷新整个绘图 .
以下是我尝试过的一些内容:
ui <- fluidPage(
titlePanel("Title"),
sidebarLayout(
sidebarPanel(width=3,
actionButton("startaddpoint", label = "Start"),
actionButton("stopaddpoint", label = "Stop"),
verbatimTextOutput("info")
),
mainPanel(
uiOutput("plot.ui")
)
)
)
server <- function(input, output, session) {
options(shiny.maxRequestSize=100*1024^2) # set maximum image size
xy_new <- reactiveValues(x= numeric(0), y = numeric(0), line=numeric(0)) # add new points
output$plot.ui <- renderUI({
plotOutput("distplot",
click = "plot_click",
dblclick = "plot_dblclick",
hover = "plot_hover",
brush = "plot_brush")
})
output$distplot <- renderPlot({
plot(0, 0, xlim=c(-2, 2), ylim=c(-2, 2), xlab="", ylab="")
# on Start, start plotting new clicks:
if(input$startaddpoint > 0) {
observe({
isolate({
xy_new$x <- c(xy_new$x, input$plot_click$x)
xy_new$y <- c(xy_new$y, input$plot_click$y)
# points(input$plot_click$x, input$plot_click$y)
})
})
}
points(xy_new$x, xy_new$y)
# on Stop, stop plotting new clicks:
# no idea here..
})
output$info <- renderText({
xy_str <- function(e) {
if(is.null(e)) return("NULL\n")
paste0("x=", round(e$x, 2), " y=", round(e$y, 2), "\n")
}
xy_range_str <- function(e) {
if(is.null(e)) return("NULL\n")
paste0("xmin=", round(e$xmin, 2), " xmax=", round(e$xmax, 2),
" ymin=", round(e$ymin, 2), " ymax=", round(e$ymax, 2),
" xrange=", round(e$xmax-e$xmin, 2), " yrange=", round(e$ymax-e$ymin,2),
" diag=",round(sqrt((e$xmax-e$xmin)^2+(e$ymax-e$ymin)^2)))
}
paste0(
"click: ", xy_str(input$plot_click),
"dblclick: ", xy_str(input$plot_dblclick),
"hover: ", xy_str(input$plot_hover),
"brush: ", xy_range_str(input$plot_brush)
)
})
}
# Run the application
shinyApp(ui = ui, server = server)
如果你看到了这样做的方法,请告诉我 . 干杯
1 回答
这里's a way to do this in a reactive construct. It'没有使用"start and stop"听力方法 . 相反,它需要一个始终监听新点击的观察者,并使用一个按钮来更新
plotOutput
的数据,从而使反应性 endpoints 无效并刷新绘图 .