dreamRs / apexcharter

:bar_chart: R Htmlwidget for ApexCharts.js
https://dreamrs.github.io/apexcharter
Other
138 stars 15 forks source link

facet not working on shinyapps #54

Closed MaelTheuliere closed 2 years ago

MaelTheuliere commented 2 years ago

Hello dreamRs, I face an issue with apex facet on shinyapps. Here is a reprex.


#  ------------------------------------------------------------------------
#
# Title : apexcharts facet example
#
#  ------------------------------------------------------------------------

library(shiny)
library(apexcharter)
data("mpg", package = "ggplot2")

ui <- fluidPage(
  fluidRow(
    column(
      width = 8, offset = 2,
      tags$h2("Apexchart facet example in Shiny", class = "text-center"),
      apexchartOutput("apex"),
    )
  )
)

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

  output$apex <- renderApexchart({
    apex(mpg, aes(displ, cty), type = "scatter") %>%
      ax_xaxis(labels = list(formatter = format_num(".0f"))) %>% 
      ax_labs(
        title = "Facet wrap example",
        subtitle = "mpg data from ggplot2",
        x = "engine displacement, in litres",
        y = "city miles per gallon"
      ) %>% 
      ax_facet_wrap(vars(drv), ncol = 2)  
    })

}

shinyApp(ui, server)

message : Error: 'options' must be a fully named list, or have no names (NULL)

config :

R version 4.0.3 (2020-10-10) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 18363)

Matrix products: default

locale: [1] LC_COLLATE=French_France.1252 [2] LC_CTYPE=French_France.1252
[3] LC_MONETARY=French_France.1252 [4] LC_NUMERIC=C
[5] LC_TIME=French_France.1252

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

other attached packages: [1] shiny_1.6.0 apexcharter_0.3.0

loaded via a namespace (and not attached): [1] Rcpp_1.0.6 jquerylib_0.1.4
[3] bslib_0.2.5.1 pillar_1.6.2
[5] compiler_4.0.3 later_1.2.0
[7] tools_4.0.3 digest_0.6.27
[9] jsonlite_1.7.2 lifecycle_1.0.0
[11] tibble_3.0.6 gtable_0.3.0
[13] pkgconfig_2.0.3 rlang_0.4.10
[15] DBI_1.1.1 cli_3.0.1
[17] rstudioapi_0.13 yaml_2.2.1
[19] fastmap_1.1.0 dplyr_1.0.6
[21] sass_0.4.0 generics_0.1.0
[23] vctrs_0.3.8 htmlwidgets_1.5.3 [25] grid_4.0.3 tidyselect_1.1.1 [27] glue_1.4.2 R6_2.5.0
[29] fansi_0.5.0 ggplot2_3.3.5
[31] purrr_0.3.4 magrittr_2.0.1
[33] scales_1.1.1 promises_1.2.0.1 [35] ellipsis_0.3.2 htmltools_0.5.1.1 [37] assertthat_0.2.1 mime_0.11
[39] colorspace_2.0-0 xtable_1.8-4
[41] httpuv_1.6.1 utf8_1.2.1
[43] munsell_0.5.0 cachem_1.0.5
[45] crayon_1.4.1

pvictor commented 2 years ago

Hello, For rendering facets in Shiny you have to use apexfacetOutput() / renderApexfacet(), that's because ax_facet_wrap() returns a list of htmlwidgets that classic renderApexchart() can't handle. Unfortunately, I cannot customize error message in renderApexchart() since it's generated by {htmlwidgets} package. But I can make the documentation clearer.

Your working example:

library(shiny)
library(apexcharter)
data("mpg", package = "ggplot2")

ui <- fluidPage(
  fluidRow(
    column(
      width = 8, offset = 2,
      tags$h2("Apexchart facet example in Shiny", class = "text-center"),
      apexfacetOutput("apex"),
    )
  )
)

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

  output$apex <- renderApexfacet({
    apex(mpg, aes(displ, cty), type = "scatter") %>%
      ax_xaxis(labels = list(formatter = format_num(".0f"))) %>% 
      ax_labs(
        title = "Facet wrap example",
        subtitle = "mpg data from ggplot2",
        x = "engine displacement, in litres",
        y = "city miles per gallon"
      ) %>% 
      ax_facet_wrap(vars(drv), ncol = 2)  
  })

}

shinyApp(ui, server)

Victor

MaelTheuliere commented 2 years ago

Hello Victor, thanks for your quick reply. I looked at the doc again, and i miss the sentence about this on the facet vignette. I was looking on the shiny vignette, sorry. In order of improving the doc, may be adding a part on render/output shiny function on the shiny vignette ? Do you want à PR ?

pvictor commented 2 years ago

Sure, it'll be great!