Event_data在Shiny内的绘图子图中未更新

时间:2019-02-21 13:00:39

标签: r shiny plotly

我正试图从由两个具有链接轴的子图组成的绘图对象中提取event_data。但是,单击图中的点时,event_data似乎没有更新。当两个绘图具有未链接的y轴时,代码按预期功能运行,但是在yaxis中包含plot_ly()参数,在scaleanchor中包含layout()参数会导致发生此错误。

理想情况下,我想知道每个子图上单击的最后一点。在子图中无法使用source参数,它会丢失源名称之一。

示例代码:

library(plotly)
library(shiny)

ui <- fluidPage(
  plotlyOutput("plot"),
  verbatimTextOutput("click1")
)

server <- function(input, output, session) {

  output$plot <- renderPlotly({
    p1 <- plot_ly(mtcars, x = ~mpg, y = ~wt, yaxis='y')
    p2 <- plot_ly(mtcars, x = ~mpg, y = ~wt, yaxis='y2')
    p1 <- layout(p1, yaxis=list(title="% growth",
                                scaleanchor='y2', constraintoward='bottom'),
                 xaxis=list(domain=c(0, 1)))
    p2 <- layout(p2, yaxis=list(title="% loss", autorange='reversed',
                                scaleanchor='y', constraintoward='top'),
                 xaxis=list(domain=c(0, 1)))
    p <- subplot(p1, p2, nrows=2,
                 shareX=TRUE, titleX=TRUE,
                 shareY=FALSE, titleY=TRUE,
                 margin=0.02)
    p <- layout(p, xaxis = list(domain=c(0,1)), legend=list(orientation='h'))
    p
    #browser()
  })

  output$click1 <- renderPrint({
    d <- event_data("plotly_click")
    if (is.null(d)) "Click events appear here" else d
  })

}

shinyApp(ui, server)

此外,启动后,控制台将打印

Warning: Can only have one: source
Warning: Can only have one: config
Warning: Can only have one: highlight

我提供了sessionInfo供参考:

> sessionInfo()
R version 3.4.4 (2018-03-15)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

Matrix products: default

locale:
[1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252   
[3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] bindrcpp_0.2       corrplot_0.84      shinythemes_1.1.1  leaflet_1.1.0     
 [5] reshape_0.8.6      knitr_1.21         markdown_0.9       htmltools_0.3.6   
 [9] plotly_4.8.0       shinyWidgets_0.4.4 ggplot2_3.1.0      shiny_1.2.0       

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.18        pillar_1.3.0        compiler_3.4.4      later_0.7.5        
 [5] plyr_1.8.4          bindr_0.1           tools_3.4.4         digest_0.6.12      
 [9] viridisLite_0.3.0   jsonlite_1.5        tibble_1.4.2        gtable_0.2.0       
[13] pkgconfig_2.0.1     rlang_0.3.1         rstudioapi_0.9.0    crosstalk_1.0.0    
[17] yaml_2.2.0          xfun_0.4            withr_2.1.2         dplyr_0.7.4        
[21] httr_1.3.1          sourcetools_0.1.6   htmlwidgets_1.3     grid_3.4.4         
[25] glue_1.3.0          data.table_1.10.4-3 R6_2.2.0            tidyr_0.8.2        
[29] purrr_0.2.4         magrittr_1.5        scales_1.0.0        promises_1.0.1     
[33] rsconnect_0.8.5     assertthat_0.2.0    mime_0.5            xtable_1.8-2       
[37] colorspace_1.3-2    httpuv_1.4.5.1      labeling_0.3        lazyeval_0.2.0     
[41] munsell_0.5.0       crayon_1.3.4        Cairo_1.5-9     

0 个答案:

没有答案