@@ -242,6 +242,7 @@ plot_with_settings_ui <- function(id) {
242242# '
243243plot_with_settings_srv <- function (id ,
244244 plot_r ,
245+ gg2plotly = TRUE ,
245246 height = c(600 , 200 , 2000 ),
246247 width = NULL ,
247248 show_hide_signal = reactive(TRUE ),
@@ -296,7 +297,10 @@ plot_with_settings_srv <- function(id,
296297 }
297298
298299 plot_type <- reactive({
299- if (inherits(plot_r(), " ggplot" )) {
300+ req(plot_suppress(plot_r()))
301+ if (inherits(plot_r(), " ggplot" ) && gg2plotly ) {
302+ " ggplotly"
303+ } else if (inherits(plot_r(), " ggplot" )) {
300304 " gg"
301305 } else if (inherits(plot_r(), " trellis" )) {
302306 " trel"
@@ -373,17 +377,55 @@ plot_with_settings_srv <- function(id,
373377
374378 p_height <- reactive(`if`(! is.null(input $ height ), input $ height , height [1 ]))
375379 p_width <- reactive(`if`(! is.null(input $ width ), input $ width , default_slider_width()[1 ]))
376- output $ plot_main <- renderPlot(
377- apply_plot_modifications(
378- plot_obj = plot_suppress(plot_r()),
379- plot_type = plot_suppress(plot_type()),
380- dblclicking = dblclicking ,
381- ranges = ranges
382- ),
383- res = get_plot_dpi(),
384- height = p_height ,
385- width = p_width
386- )
380+
381+ observeEvent(plot_type(), ignoreNULL = TRUE , once = TRUE , {
382+ output $ plot_main <- if (identical(plot_type(), " ggplotly" )) {
383+ plotly :: renderPlotly({
384+ plotly :: event_register(
385+ plotly :: layout(
386+ plotly :: ggplotly(plot_r(), layerData = 1 ),
387+ dragmode = " select"
388+ ),
389+ " plotly_selected"
390+ )
391+ })
392+ } else {
393+ renderPlot(
394+ {
395+ apply_plot_modifications(
396+ plot_obj = plot_suppress(plot_r()),
397+ plot_type = plot_suppress(plot_type()),
398+ dblclicking = dblclicking ,
399+ ranges = ranges
400+ )
401+ },
402+ res = get_plot_dpi(),
403+ height = p_height ,
404+ width = p_width
405+ )
406+ }
407+ })
408+
409+
410+
411+ plotly_brush <- reactive({
412+ req(plot_suppress(plot_r()))
413+ # layer_data(plot_r(), 3)
414+ # ggplot_build(plot_r())$plot$data
415+ bbox <- plotly :: event_data(" plotly_selected" )
416+ if (is.null(bbox )) {
417+ return (NULL )
418+ }
419+ list (
420+ mapping = list (
421+ x = rlang :: as_label(plot_r()$ mapping $ x ),
422+ y = rlang :: as_label(plot_r()$ mapping $ y )
423+ ),
424+ xmin = min(bbox $ x ), xmax = max(bbox $ x ),
425+ ymin = min(bbox $ y ), ymax = max(bbox $ y ),
426+ direction = " xy"
427+ )
428+ })
387429
388430 output $ plot_modal <- renderPlot(
389431 apply_plot_modifications(
@@ -399,17 +441,21 @@ plot_with_settings_srv <- function(id,
399441
400442 output $ plot_out_main <- renderUI({
401443 req(plot_suppress(plot_r()))
402- tags $ div(
403- align = graph_align ,
404- plotOutput(
405- ns(" plot_main" ),
406- height = " 100%" ,
407- brush = `if`(brushing , brushOpts(ns(" plot_brush" ), resetOnNew = FALSE ), NULL ),
408- click = `if`(clicking , clickOpts(ns(" plot_click" )), NULL ),
409- dblclick = `if`(dblclicking , dblclickOpts(ns(" plot_dblclick" )), NULL ),
410- hover = `if`(hovering , hoverOpts(ns(" plot_hover" )), NULL )
444+ if (identical(plot_type(), " ggplotly" )) {
445+ plotly :: plotlyOutput(ns(" plot_main" ))
446+ } else {
447+ tags $ div(
448+ align = graph_align ,
449+ plotOutput(
450+ ns(" plot_main" ),
451+ height = " 100%" ,
452+ brush = `if`(brushing , brushOpts(ns(" plot_brush" ), resetOnNew = FALSE ), NULL ),
453+ click = `if`(clicking , clickOpts(ns(" plot_click" )), NULL ),
454+ dblclick = `if`(dblclicking , dblclickOpts(ns(" plot_dblclick" )), NULL ),
455+ hover = `if`(hovering , hoverOpts(ns(" plot_hover" )), NULL )
456+ )
411457 )
412- )
458+ }
413459 })
414460
415461 output $ width_warning <- renderUI({
@@ -500,25 +546,28 @@ plot_with_settings_srv <- function(id,
500546 return (
501547 list (
502548 brush = reactive({
503- # refresh brush data on the main plot size change
504- input $ height
505- input $ width
506- input $ plot_brush
549+ if (identical(plot_type(), " ggplotly" )) {
550+ plotly_brush()
551+ } else {
552+ input $ height
553+ input $ width
554+ input $ plot_brush
555+ }
507556 }),
508557 click = reactive({
509- # refresh click data on the main plot size change
558+ # # refresh click data on the main plot size change
510559 input $ height
511560 input $ width
512561 input $ plot_click
513562 }),
514563 dblclick = reactive({
515- # refresh double click data on the main plot size change
564+ # # refresh double click data on the main plot size change
516565 input $ height
517566 input $ width
518567 input $ plot_dblclick
519568 }),
520569 hover = reactive({
521- # refresh hover data on the main plot size change
570+ # # refresh hover data on the main plot size change
522571 input $ height
523572 input $ width
524573 input $ plot_hover
@@ -603,7 +652,7 @@ type_download_srv <- function(id, plot_reactive, plot_type, plot_w, default_w, p
603652# ' x = "AGE",
604653# ' y = "BMRKR1"
605654# ' ),
606- # ' xmin = 30, xmax = 40,
655+ # ' xmin = 30.1 , xmax = 40,
607656# ' ymin = 0.7, ymax = 10,
608657# ' direction = "xy"
609658# ' )
0 commit comments