oobianom / shinyStorePlus

An R package with in-browser storage for Shiny persistent, synchronized data from the inputs using IndexedDB. Transfer browser link parameters to Shiny input or output values.
https://shinystoreplus.obi.obianom.com
Other
26 stars 3 forks source link

Can't store values of dynamically generated inputs #8

Open ajlyons opened 3 months ago

ajlyons commented 3 months ago

Thanks for a fantastic package. I'm trying to use shinyStorePlus in an app where most of the ui (including inputs) is dynamically generated (because it has to be recreated whenever the user picks a different language). shinyStorePlus stores the inputs that are created in the usual manner in the ui, but not dynamically generated inputs.

Is there any way to get setupStorage to save the value of dynamically created inputs? I tried setting outputs=TRUE to no avail. Other ideas welcome.

library(shiny)
library(shinyStorePlus)

ui <- fluidPage(
    initStore(),
    selectInput("sel_color", "Color (hardcoded input):", choices = c("", "green", "blue", "red", "yellow", "cyan"), selected = ""),
    uiOutput("ui_moreinputs"),
    p("Refresh the page - the value of the hard coded input is retained, but the dynamic one is not")
)

server <- function(input, output, session) {
  observe({
    output$ui_moreinputs <- renderUI(
      selectInput("sel_month", "Month (dynamically generated):", choices = c("", month.name), selected = "")
    )
  })

  setupStorage(appId = "shinytest03", inputs = list("sel_month", "sel_color"), session = session)
}

shinyApp(ui = ui, server = server, options = list(launch.browser = TRUE))
oobianom commented 3 months ago

Hi Andy! Thanks for using the package. And great question, this has actual come up before but I wasn't sure how much people want the feature. I planned to work on it this year, but was a bit busy with my other packages. Nonetheless, now I will begin work on this and get back to you soonest.

My guess is that I should have some sort of solution by end of the weekend, but if not, do check back with me.

ajlyons commented 3 months ago

A million thanks! This will be a game changer.

oobianom commented 3 months ago

Hi Andy,

I have now worked on the feature and uploaded the draft to this repository. Take a look when you have time. Basically, update your package from this github repository [remotes::install_github("oobianom/shinyStorePlus")] and then test the code again. Let me know how it works out. Remember to thumbs up the project.

Use the code like below using the dyn.inputs

setupStorage(appId = "shinytest03", inputs = list("sel_color"), dyn.inputs = list("sel_month"), session = session)

or simply

setupStorage(appId = "shinytest03",  dyn.inputs = list("sel_month"), session = session)

additionally, i have created an example app in the shinyStorePlus examples folder https://github.com/oobianom/aagarw30_shinyapps_to-shinyStorePlus/tree/master/dynamic_inputs

ajlyons commented 3 months ago

Thanks Obi, that looks like a great solution. Unfortunately I'm not getting it to work.

I updated the package from GH (and verify that it's loading v1.2 in my app.R with a cat(...) call). I ran your updated app.R code, as well as example 3 from the setupStorage() help page, but the dynamic inputs are still not preserved when I refresh the page. For good measure I tried three browsers (Chrome, FireFox, and Edge), and ran the app in the RStudio Viewer pane, but the dynamic input does not get saved.

I also published a test app on ShinyApps.io to see if it would work from my Android phone. Unfortunately the dynamically generated inputs still get reset when the page is reloaded.

Below is the output from my sessionInfo(), in case the culprit is an OS thing. Thanks for trying!

> sessionInfo()
R version 4.4.0 (2024-04-24 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 22631)

Matrix products: default

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

time zone: America/Los_Angeles
tzcode source: internal

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

other attached packages:
[1] shinyStorePlus_1.2 shiny_1.9.1       

loaded via a namespace (and not attached):
 [1] miniUI_0.1.1.1    jsonlite_1.8.8    compiler_4.4.0    bsicons_0.1.2     promises_1.3.0    Rcpp_1.0.13      
 [7] stringr_1.5.1     jquerylib_0.1.4   later_1.3.2       fastmap_1.2.0     mime_0.12         R6_2.5.1         
[13] curl_5.2.1        htmlwidgets_1.6.4 profvis_0.3.8     openssl_2.2.0     bslib_0.8.0       rlang_1.1.3      
[19] cachem_1.1.0      stringi_1.8.4     httpuv_1.6.15     sass_0.4.9        fs_1.6.4          pkgload_1.4.0    
[25] memoise_2.0.1     cli_3.6.3         withr_3.0.1       magrittr_2.0.3    digest_0.6.35     rstudioapi_0.16.0
[31] xtable_1.8-4      askpass_1.2.0     remotes_2.5.0     devtools_2.4.5    lifecycle_1.0.4   vctrs_0.6.5      
[37] glue_1.7.0        urlchecker_1.0.1  sessioninfo_1.2.2 rsconnect_1.3.1   pkgbuild_1.4.4    purrr_1.0.2      
[43] tools_4.4.0       usethis_3.0.0     ellipsis_0.3.2    htmltools_0.5.8.1
oobianom commented 3 months ago

What? That's weird. I checked out the shiny app you published and its not saving either. It worked perfectly on my dev windows. Let me look into it to tomorrow. Between, did you clear your browser cache to make sure the javascript is loading properly?

ajlyons commented 3 months ago

Thanks for checking. I did clear the browser cache, but to completely eliminate that possibility I just ran it on another laptop with a fresh installation of R and got the same result. Let me know if there are any other tests or trials I can do.

oobianom commented 3 months ago

Hey, I just made a new update to the javascript. Reinstall and try it again this time. Let me know the results.

ajlyons commented 3 months ago

Thanks - that did it! Both the hard coded and dynamically generated input values are retained when I reload the page, and even when I completely shut down and reopen the browser. I also updated the test app and that is working now also. Thank you for your perseverance on this!

oobianom commented 3 months ago

That's fantastic, Andy! Thanks for being patient. I will do one last check and this will be published soon in CRAN as well.