dreamRs / esquisse

RStudio add-in to make plots interactively with ggplot2
https://dreamrs.github.io/esquisse
Other
1.77k stars 229 forks source link

filter_UI() compatibility with bookmarking state #73

Closed fdetsch closed 4 years ago

fdetsch commented 4 years ago

I modified the example for filter_UI() to work with shiny's bookmarking state:

library(shiny)
library(shinyWidgets)
library(ggplot2)
library(esquisse)

ui <- function(request) {
  fluidPage(
    tags$h2("Filter data.frame"),

    radioButtons(
      inputId = "dataset", 
      label = "Data:",
      choices = c(
        "iris", "mtcars", "economics", 
        "midwest", "mpg", "msleep", "diamonds",
        "faithfuld", "txhousing"
      ),
      inline = TRUE
    ),

    fluidRow(
      column(
        width = 3,
        filterDF_UI("filtering")
      ),
      column(
        width = 9,
        progressBar(
          id = "pbar", value = 100, 
          total = 100, display_pct = TRUE
        ),
        DT::dataTableOutput(outputId = "table"),
        tags$p("Code dplyr:"),
        verbatimTextOutput(outputId = "code_dplyr"),
        tags$p("Expression:"),
        verbatimTextOutput(outputId = "code"),
        tags$p("Filtered data:"),
        verbatimTextOutput(outputId = "res_str")
      )
    ), 

    bookmarkButton()
  )
}

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

  data <- reactive({
    get(input$dataset)
  })

  res_filter <- callModule(
    module = filterDF, 
    id = "filtering", 
    data_table = data,
    data_name = reactive(input$dataset)
  )

  observeEvent(res_filter$data_filtered(), {
    updateProgressBar(
      session = session, id = "pbar", 
      value = nrow(res_filter$data_filtered()), total = nrow(data())
    )
  })

  output$table <- DT::renderDT({
    res_filter$data_filtered()
  }, options = list(pageLength = 5))

  output$code_dplyr <- renderPrint({
    res_filter$code$dplyr
  })
  output$code <- renderPrint({
    res_filter$code$expr
  })

  output$res_str <- renderPrint({
    str(res_filter$data_filtered())
  })

}

shinyApp(ui, server, enableBookmarking = "url")

Now, when I run the app, choose a non-default dataset (eg. 'mtcars') and create a bookmark, I can easily reproduce the current state of the app in a new Chrome tab (ie. 'mtcars' is selected right away). However, this does not apply to changes made to any filterUI() slider. For example, if I set 'mpg' to 22 and subsequently create a bookmark, the slider is being reset to its full range when reopening the app in a new browser tab.

Is there an easy solution from the esquisse side when working with bookmarking state and trying to retain those slider settings?

Here's my

[master]> sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18362)

Matrix products: default

locale:
[1] LC_COLLATE=German_Germany.1252  LC_CTYPE=German_Germany.1252    LC_MONETARY=German_Germany.1252
[4] LC_NUMERIC=C                    LC_TIME=German_Germany.1252    

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

other attached packages:
[1] esquisse_0.2.2       ggplot2_3.2.1        shinyWidgets_0.4.9   shiny_1.3.2          shinydashboard_0.7.1

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.2         RColorBrewer_1.1-2 compiler_3.6.1     pillar_1.4.2       later_0.8.0       
 [6] hrbrthemes_0.6.0   tools_3.6.1        extrafont_0.17     digest_0.6.20      evaluate_0.14     
[11] jsonlite_1.6       tibble_2.1.3       gtable_0.3.0       pkgconfig_2.0.2    rlang_0.4.0       
[16] rstudioapi_0.10    crosstalk_1.0.0    yaml_2.2.0         xfun_0.9           Rttf2pt1_1.3.7    
[21] knitr_1.24         stringr_1.4.0      withr_2.1.2        dplyr_0.8.3        systemfonts_0.1.1 
[26] gdtools_0.2.0      htmlwidgets_1.3    DT_0.8             grid_3.6.1         prompt_1.0.0      
[31] tidyselect_0.2.5   glue_1.3.1         R6_2.4.0           rmarkdown_1.15     extrafontdb_1.0   
[36] purrr_0.3.2        magrittr_1.5       ggthemes_4.2.0     scales_1.0.0       clisymbols_1.2.0  
[41] promises_1.0.1     htmltools_0.3.6    assertthat_0.2.1   mime_0.7           xtable_1.8-4      
[46] colorspace_1.4-1   httpuv_1.5.2       miniUI_0.1.1.1     stringi_1.4.3      lazyeval_0.2.2    
[51] munsell_0.5.0      crayon_1.3.4
pvictor commented 4 years ago

Hello @fdetsch ,

Filters id were randomly generated so far, I make an update on GitHub to use variable names instead. Bookmarking should work now, tell me if encounter new issues with this change.

Victor

fdetsch commented 4 years ago

Works like a charm, thank you so much for the rapid processing!