Skip to content

event_data("plotly_click") reports wrong keys #795

@mdphan

Description

@mdphan

Hi,

I am trying to use event_data("plotly_click") to to get the key info from a scatter plot. However, the key I got was wrong. Below is the script that I used:

test.txt

library(plotly)
library(shiny)
library(dplyr)
library(tidyr)

# load data
set.seed(1)
mydata <- data.frame(id = paste0("item",1:10),
                     x = rbinom(10,size=100,prob=0.2),
                     s1 = rnorm(10,mean=0,sd=6),
                     s2 = rnorm(10,mean=0,sd=5),
                     s3 = rnorm(10,mean=0,sd=4),
                     s4 = rnorm(10,mean=0,sd=3))

# colour variable for plotly graph
mydata <- mydata %>% mutate_at(vars(starts_with("s")), 
              funs("col"= ifelse(. >= 2 & x >= 4, "Adv", ifelse(. <= -2 & x >=4, "Req", "NS"))))

# colour pallete
pal <- c("#0571B0", "grey90", "#CA0020")
pal <- setNames(pal, c("Adv", "NS", "Req"))

# shiny UI
ui <- fluidPage(
    selectInput("sample", "Sample:",
                list("Sample 1" = "s1", 
                     "Sample 2" = "s2",
                     "Sample 3" = "s3",
                     "Sample 4" = "s4")),
    mainPanel(
        plotlyOutput("plot")
    ),
    verbatimTextOutput("selection")
)

# shiny server
server <- function(input, output, session) {
 
    output$plot <- renderPlotly({
        plot_ly(mydata, x = ~x, y = as.formula(paste("~", input$sample)), type = "scatter", mode = "markers", colors = pal, text = ~id, color= as.formula(paste0("~",input$sample,"_col")), marker = list(size = 4), key = ~id, source = "plotkey") %>%
        layout(title = input$sample)
    })
    
    output$selection <- renderPrint({
        s <- event_data("plotly_click", source = "plotkey")
        if (length(s) == 0) {
            "Click on a dot:"
        } else {
            cat("Showing: \n\n")
            as.list(s)
        }
    })  
}

# call shiny 
shinyApp(ui, server, options = list(height = 500))

As you can see in the plotly graph, I map both the hover text and key to id (text = ~id, key = ~id) but only the hover text reports the correct id (item1), the key reports item8.

screen shot 2016-11-14 at 5 32 20 pm

Is this a real bug or am I doing something wrong?

sessionInfo()
## R version 3.3.1 (2016-06-21)
## Platform: x86_64-apple-darwin13.4.0 (64-bit)
## Running under: OS X 10.11.6 (El Capitan)
## 
## locale:
## [1] en_AU.UTF-8/en_AU.UTF-8/en_AU.UTF-8/C/en_AU.UTF-8/en_AU.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] tidyr_0.6.0   dplyr_0.5.0   plotly_4.5.6  ggplot2_2.2.0 shiny_0.14.2 
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.12.7       knitr_1.15        magrittr_1.5     
##  [4] munsell_0.4.3     viridisLite_0.1.3 colorspace_1.3-0 
##  [7] xtable_1.8-2      R6_2.2.0          httr_1.2.1       
## [10] stringr_1.1.0     plyr_1.8.4        tools_3.3.1      
## [13] grid_3.3.1        gtable_0.2.0      DBI_0.5-1        
## [16] htmltools_0.3.5   yaml_2.1.14       lazyeval_0.2.0   
## [19] assertthat_0.1    digest_0.6.10     tibble_1.2       
## [22] purrr_0.2.2       htmlwidgets_0.8   base64enc_0.1-3  
## [25] evaluate_0.10     mime_0.5          rmarkdown_1.1    
## [28] stringi_1.1.2     scales_0.4.1      jsonlite_1.1     
## [31] httpuv_1.3.3

Best regards,

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions