plotly / plotly/plotly.R

Shiny/crosstalk barchart highlight adds bards

Aperta
#2,139 3 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
R
Stelle
2.7k
Fork
641
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

Trying to make a linked bar chart that links with other charts using crosstalk and shiny.
Issue: When clicking the plot, it stacks on top instead of just highlighting
image

RStudio cloud project with app for reprex https://rstudio.cloud/project/4071604

Shiny Example:

library(shiny)
library(crosstalk)
#> 
#> Attaching package: 'crosstalk'
#> The following object is masked from 'package:shiny':
#> 
#>     getDefaultReactiveDomain
library(plotly)
#> Loading required package: ggplot2
#> 
#> Attaching package: 'plotly'
#> The following object is masked from 'package:ggplot2':
#> 
#>     last_plot
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> The following object is masked from 'package:graphics':
#> 
#>     layout
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

# fake names
df = tibble::tribble(
  ~provider, ~n_total_visit,      ~Type, ~n_visit,
  "Dr. Robert Lyons",           315L,  "No Show",     142L,
  "Dr. Robert Lyons",           315L, "Occurred",     175L,
  "Dr. Alexander Augustine",           336L,  "No Show",     122L,
  "Dr. Alexander Augustine",           336L, "Occurred",     217L,
  "Dr. Alisia Valenzuela",           308L,  "No Show",     102L,
  "Dr. Alisia Valenzuela",           308L, "Occurred",     215L
)
df = df %>%
  mutate(utilization = n_visit / n_total_visit)
df = df %>%
  select(provider, utilization, Type) %>%
  mutate(Type = factor(Type, levels = c("Occurred", "No Show")))

ui <- fluidPage(
  titlePanel(title = ""),
  sidebarLayout(
    sidebarPanel(),
    mainPanel(
      plotlyOutput("utilization_by_provider", width = "100%"),
      plotlyOutput("gg", width = "100%")
    )
  )
)

server = function(input, output) {
  df = crosstalk::SharedData$new(df, key = ~ provider)

  output$utilization_by_provider = renderPlotly({
    fig = df %>%
      plot_ly(
        x = ~ provider,
        y = ~ utilization,
        color = ~ Type,
        type = "bar"
      ) %>%
      layout(
        barmode = 'stack'
      ) %>%
      highlight()

    fig
  })

  output$gg = renderPlotly({
    fig = df %>%
      ggplot(aes(x = provider, y= utilization, fill = Type)) +
      geom_bar(stat = "identity", position = "stack")
    fig = ggplotly(fig) %>%
      highlight()

    fig
  })

}

shinyApp(ui = ui, server = server)
#> 
#> Listening on http://127.0.0.1:4992
#> Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels

#> Warning in RColorBrewer::brewer.pal(N, "Set2"): minimal value for n is 3, returning requested palette with 3 different levels
#> Setting the `off` event (i.e., 'plotly_doubleclick') to match the `on` event (i.e., 'plotly_click'). You can change this default via the `highlight()` function.
#> Setting the `off` event (i.e., 'plotly_doubleclick') to match the `on` event (i.e., 'plotly_click'). You can change this default via the `highlight()` function.
#> Setting the `off` event (i.e., 'plotly_doubleclick') to match the `on` event (i.e., 'plotly_click'). You can change this default via the `highlight()` function.
#> Setting the `off` event (i.e., 'plotly_doubleclick') to match the `on` event (i.e., 'plotly_click'). You can change this default via the `highlight()` function.

Created on 2022-05-19 by the reprex package (v2.0.1)

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.1.1 (2021-08-10)
#>  os       Ubuntu 20.04.3 LTS
#>  system   x86_64, linux-gnu
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       Etc/UTC
#>  date     2022-05-19
#>  pandoc   2.14.0.3 @ /usr/lib/rstudio-server/bin/pandoc/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package      * version    date (UTC) lib source
#>  assertthat     0.2.1      2019-03-21 [1] RSPM (R 4.1.0)
#>  bslib          0.3.1      2021-10-06 [1] RSPM (R 4.1.0)
#>  cachem         1.0.6      2021-08-19 [1] RSPM (R 4.1.0)
#>  callr          3.7.0      2021-04-20 [1] RSPM (R 4.1.0)
#>  cli            3.3.0      2022-04-25 [1] RSPM (R 4.1.0)
#>  colorspace     2.0-3      2022-02-21 [1] RSPM (R 4.1.0)
#>  crayon         1.5.1      2022-03-26 [1] RSPM (R 4.1.0)
#>  crosstalk    * 1.2.0      2021-11-04 [1] CRAN (R 4.1.1)
#>  curl           4.3.2      2021-06-23 [1] RSPM (R 4.1.0)
#>  data.table     1.14.2     2021-09-27 [1] RSPM (R 4.1.0)
#>  DBI            1.1.2      2021-12-20 [1] RSPM (R 4.1.0)
#>  digest         0.6.29     2021-12-01 [1] RSPM (R 4.1.0)
#>  dplyr        * 1.0.8      2022-02-08 [1] RSPM (R 4.1.0)
#>  ellipsis       0.3.2      2021-04-29 [1] RSPM (R 4.1.0)
#>  evaluate       0.15       2022-02-18 [1] RSPM (R 4.1.0)
#>  fansi          1.0.3      2022-03-24 [1] RSPM (R 4.1.0)
#>  farver         2.1.0      2021-02-28 [1] RSPM (R 4.1.0)
#>  fastmap        1.1.0      2021-01-25 [1] RSPM (R 4.1.0)
#>  fs             1.5.2      2021-12-08 [1] RSPM (R 4.1.0)
#>  generics       0.1.2      2022-01-31 [1] RSPM (R 4.1.0)
#>  ggplot2      * 3.3.5      2021-06-25 [1] RSPM (R 4.1.0)
#>  glue           1.6.2      2022-02-24 [1] RSPM (R 4.1.0)
#>  gtable         0.3.0      2019-03-25 [1] RSPM (R 4.1.0)
#>  highr          0.9        2021-04-16 [1] RSPM (R 4.1.0)
#>  htmltools      0.5.2      2021-08-25 [1] RSPM (R 4.1.0)
#>  htmlwidgets    1.5.4      2021-09-08 [1] RSPM (R 4.1.0)
#>  httpuv         1.6.5      2022-01-05 [1] RSPM (R 4.1.0)
#>  httr           1.4.2      2020-07-20 [1] RSPM (R 4.1.0)
#>  jquerylib      0.1.4      2021-04-26 [1] RSPM (R 4.1.0)
#>  jsonlite       1.8.0      2022-02-22 [1] RSPM (R 4.1.0)
#>  knitr          1.39       2022-04-26 [1] RSPM (R 4.1.0)
#>  labeling       0.4.2      2020-10-20 [1] RSPM (R 4.1.0)
#>  later          1.3.0      2021-08-18 [1] RSPM (R 4.1.0)
#>  lazyeval       0.2.2      2019-03-15 [1] CRAN (R 4.1.1)
#>  lifecycle      1.0.1      2021-09-24 [1] RSPM (R 4.1.0)
#>  magrittr       2.0.3      2022-03-30 [1] RSPM (R 4.1.0)
#>  mime           0.12       2021-09-28 [1] RSPM (R 4.1.0)
#>  munsell        0.5.0      2018-06-12 [1] RSPM (R 4.1.0)
#>  pillar         1.7.0      2022-02-01 [1] RSPM (R 4.1.0)
#>  pkgconfig      2.0.3      2019-09-22 [1] RSPM (R 4.1.0)
#>  plotly       * 4.10.0     2021-10-09 [1] CRAN (R 4.1.1)
#>  processx       3.5.3      2022-03-25 [1] RSPM (R 4.1.0)
#>  promises       1.2.0.1    2021-02-11 [1] RSPM (R 4.1.0)
#>  ps             1.7.0      2022-04-23 [1] RSPM (R 4.1.0)
#>  purrr          0.3.4      2020-04-17 [1] RSPM (R 4.1.0)
#>  R.cache        0.15.0     2021-04-30 [1] RSPM (R 4.1.0)
#>  R.methodsS3    1.8.1      2020-08-26 [1] RSPM (R 4.1.0)
#>  R.oo           1.24.0     2020-08-26 [1] RSPM (R 4.1.0)
#>  R.utils        2.11.0     2021-09-26 [1] RSPM (R 4.1.0)
#>  R6             2.5.1      2021-08-19 [1] RSPM (R 4.1.0)
#>  RColorBrewer   1.1-3      2022-04-03 [1] RSPM (R 4.1.0)
#>  Rcpp           1.0.8.3    2022-03-17 [1] RSPM (R 4.1.0)
#>  reprex         2.0.1      2021-08-05 [1] RSPM (R 4.1.0)
#>  rlang          1.0.2      2022-03-04 [1] RSPM (R 4.1.0)
#>  rmarkdown      2.14       2022-04-25 [1] RSPM (R 4.1.0)
#>  rstudioapi     0.13       2020-11-12 [1] RSPM (R 4.1.0)
#>  sass           0.4.1      2022-03-23 [1] RSPM (R 4.1.0)
#>  scales         1.2.0      2022-04-13 [1] RSPM (R 4.1.0)
#>  sessioninfo    1.2.2.9000 2022-04-08 [1] Github (r-lib/sessioninfo@27965c2)
#>  shiny        * 1.7.1      2021-10-02 [1] RSPM (R 4.1.0)
#>  stringi        1.7.6      2021-11-29 [1] RSPM (R 4.1.0)
#>  stringr        1.4.0      2019-02-10 [1] RSPM (R 4.1.0)
#>  styler         1.7.0      2022-03-13 [1] RSPM (R 4.1.0)
#>  tibble         3.1.6      2021-11-07 [1] RSPM (R 4.1.0)
#>  tidyr          1.2.0      2022-02-01 [1] RSPM (R 4.1.0)
#>  tidyselect     1.1.2      2022-02-21 [1] RSPM (R 4.1.0)
#>  utf8           1.2.2      2021-07-24 [1] RSPM (R 4.1.0)
#>  vctrs          0.4.1      2022-04-13 [1] RSPM (R 4.1.0)
#>  viridisLite    0.4.0      2021-04-13 [1] RSPM (R 4.1.0)
#>  webshot        0.5.2      2019-11-22 [1] RSPM (R 4.1.0)
#>  withr          2.5.0      2022-03-03 [1] RSPM (R 4.1.0)
#>  xfun           0.30       2022-03-02 [1] RSPM (R 4.1.0)
#>  xml2           1.3.3      2021-11-30 [1] RSPM (R 4.1.0)
#>  xtable         1.8-4      2019-04-21 [1] RSPM (R 4.1.0)
#>  yaml           2.3.5      2022-02-21 [1] RSPM (R 4.1.0)
#> 
#>  [1] /usr/local/lib/R/site-library
#>  [2] /usr/local/lib/R/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

Just Plotly Example:

library(shiny)
library(crosstalk)
#> 
#> Attaching package: 'crosstalk'
#> The following object is masked from 'package:shiny':
#> 
#>     getDefaultReactiveDomain
library(plotly)
#> Loading required package: ggplot2
#> 
#> Attaching package: 'plotly'
#> The following object is masked from 'package:ggplot2':
#> 
#>     last_plot
#> The following object is masked from 'package:stats':
#> 
#>     filter
#> The following object is masked from 'package:graphics':
#> 
#>     layout
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

# fake names
df = tibble::tribble(
  ~provider, ~n_total_visit,      ~Type, ~n_visit,
  "Dr. Robert Lyons",           315L,  "No Show",     142L,
  "Dr. Robert Lyons",           315L, "Occurred",     175L,
  "Dr. Alexander Augustine",           336L,  "No Show",     122L,
  "Dr. Alexander Augustine",           336L, "Occurred",     217L,
  "Dr. Alisia Valenzuela",           308L,  "No Show",     102L,
  "Dr. Alisia Valenzuela",           308L, "Occurred",     215L
)
df = df %>%
  mutate(utilization = n_visit / n_total_visit)
df = df %>%
  select(provider, utilization, Type) %>%
  mutate(Type = factor(Type, levels = c("Occurred", "No Show")))

df = crosstalk::SharedData$new(df, key = ~ provider)

fig = df %>%
  plot_ly(
    x = ~ provider,
    y = ~ utilization,
    color = ~ Type,
    type = "bar"
  ) %>%
  layout(
    barmode = 'stack'
  ) %>%
  highlight()

Created on 2022-05-19 by the reprex package (v2.0.1)

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.1.1 (2021-08-10)
#>  os       Ubuntu 20.04.3 LTS
#>  system   x86_64, linux-gnu
#>  ui       X11
#>  language (EN)
#>  collate  en_US.UTF-8
#>  ctype    en_US.UTF-8
#>  tz       Etc/UTC
#>  date     2022-05-19
#>  pandoc   2.14.0.3 @ /usr/lib/rstudio-server/bin/pandoc/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version    date (UTC) lib source
#>  assertthat    0.2.1      2019-03-21 [1] RSPM (R 4.1.0)
#>  cli           3.3.0      2022-04-25 [1] RSPM (R 4.1.0)
#>  colorspace    2.0-3      2022-02-21 [1] RSPM (R 4.1.0)
#>  crayon        1.5.1      2022-03-26 [1] RSPM (R 4.1.0)
#>  crosstalk   * 1.2.0      2021-11-04 [1] CRAN (R 4.1.1)
#>  data.table    1.14.2     2021-09-27 [1] RSPM (R 4.1.0)
#>  DBI           1.1.2      2021-12-20 [1] RSPM (R 4.1.0)
#>  digest        0.6.29     2021-12-01 [1] RSPM (R 4.1.0)
#>  dplyr       * 1.0.8      2022-02-08 [1] RSPM (R 4.1.0)
#>  ellipsis      0.3.2      2021-04-29 [1] RSPM (R 4.1.0)
#>  evaluate      0.15       2022-02-18 [1] RSPM (R 4.1.0)
#>  fansi         1.0.3      2022-03-24 [1] RSPM (R 4.1.0)
#>  fastmap       1.1.0      2021-01-25 [1] RSPM (R 4.1.0)
#>  fs            1.5.2      2021-12-08 [1] RSPM (R 4.1.0)
#>  generics      0.1.2      2022-01-31 [1] RSPM (R 4.1.0)
#>  ggplot2     * 3.3.5      2021-06-25 [1] RSPM (R 4.1.0)
#>  glue          1.6.2      2022-02-24 [1] RSPM (R 4.1.0)
#>  gtable        0.3.0      2019-03-25 [1] RSPM (R 4.1.0)
#>  highr         0.9        2021-04-16 [1] RSPM (R 4.1.0)
#>  htmltools     0.5.2      2021-08-25 [1] RSPM (R 4.1.0)
#>  htmlwidgets   1.5.4      2021-09-08 [1] RSPM (R 4.1.0)
#>  httpuv        1.6.5      2022-01-05 [1] RSPM (R 4.1.0)
#>  httr          1.4.2      2020-07-20 [1] RSPM (R 4.1.0)
#>  jsonlite      1.8.0      2022-02-22 [1] RSPM (R 4.1.0)
#>  knitr         1.39       2022-04-26 [1] RSPM (R 4.1.0)
#>  later         1.3.0      2021-08-18 [1] RSPM (R 4.1.0)
#>  lazyeval      0.2.2      2019-03-15 [1] CRAN (R 4.1.1)
#>  lifecycle     1.0.1      2021-09-24 [1] RSPM (R 4.1.0)
#>  magrittr      2.0.3      2022-03-30 [1] RSPM (R 4.1.0)
#>  mime          0.12       2021-09-28 [1] RSPM (R 4.1.0)
#>  munsell       0.5.0      2018-06-12 [1] RSPM (R 4.1.0)
#>  pillar        1.7.0      2022-02-01 [1] RSPM (R 4.1.0)
#>  pkgconfig     2.0.3      2019-09-22 [1] RSPM (R 4.1.0)
#>  plotly      * 4.10.0     2021-10-09 [1] CRAN (R 4.1.1)
#>  promises      1.2.0.1    2021-02-11 [1] RSPM (R 4.1.0)
#>  purrr         0.3.4      2020-04-17 [1] RSPM (R 4.1.0)
#>  R.cache       0.15.0     2021-04-30 [1] RSPM (R 4.1.0)
#>  R.methodsS3   1.8.1      2020-08-26 [1] RSPM (R 4.1.0)
#>  R.oo          1.24.0     2020-08-26 [1] RSPM (R 4.1.0)
#>  R.utils       2.11.0     2021-09-26 [1] RSPM (R 4.1.0)
#>  R6            2.5.1      2021-08-19 [1] RSPM (R 4.1.0)
#>  Rcpp          1.0.8.3    2022-03-17 [1] RSPM (R 4.1.0)
#>  reprex        2.0.1      2021-08-05 [1] RSPM (R 4.1.0)
#>  rlang         1.0.2      2022-03-04 [1] RSPM (R 4.1.0)
#>  rmarkdown     2.14       2022-04-25 [1] RSPM (R 4.1.0)
#>  rstudioapi    0.13       2020-11-12 [1] RSPM (R 4.1.0)
#>  scales        1.2.0      2022-04-13 [1] RSPM (R 4.1.0)
#>  sessioninfo   1.2.2.9000 2022-04-08 [1] Github (r-lib/sessioninfo@27965c2)
#>  shiny       * 1.7.1      2021-10-02 [1] RSPM (R 4.1.0)
#>  stringi       1.7.6      2021-11-29 [1] RSPM (R 4.1.0)
#>  stringr       1.4.0      2019-02-10 [1] RSPM (R 4.1.0)
#>  styler        1.7.0      2022-03-13 [1] RSPM (R 4.1.0)
#>  tibble        3.1.6      2021-11-07 [1] RSPM (R 4.1.0)
#>  tidyr         1.2.0      2022-02-01 [1] RSPM (R 4.1.0)
#>  tidyselect    1.1.2      2022-02-21 [1] RSPM (R 4.1.0)
#>  utf8          1.2.2      2021-07-24 [1] RSPM (R 4.1.0)
#>  vctrs         0.4.1      2022-04-13 [1] RSPM (R 4.1.0)
#>  viridisLite   0.4.0      2021-04-13 [1] RSPM (R 4.1.0)
#>  withr         2.5.0      2022-03-03 [1] RSPM (R 4.1.0)
#>  xfun          0.30       2022-03-02 [1] RSPM (R 4.1.0)
#>  xtable        1.8-4      2019-04-21 [1] RSPM (R 4.1.0)
#>  yaml          2.3.5      2022-02-21 [1] RSPM (R 4.1.0)
#> 
#>  [1] /usr/local/lib/R/site-library
#>  [2] /usr/local/lib/R/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia eseguendo gli esempi forniti di Shiny e Plotly standalone, concentrandoti sul grafico a barre plot_ly con barmode = 'stack' e highlight(). Confronta il grafico selezionato con il comportamento atteso di highlight mostrato negli screenshot; il lavoro è completato quando facendo clic su una barra questa viene evidenziata senza aggiungere o impilare barre aggiuntive, in entrambi gli esempi.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
plotly, r
Ambito
data-visualization
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.