posit-dev / r-shinylive

https://posit-dev.github.io/r-shinylive/
Other
151 stars 16 forks source link

url_search parsing a shinylive app #28

Open thebioengineer opened 10 months ago

thebioengineer commented 10 months ago

I have an app from my screencast that I tried to turn into a shinylive app that takes advantage of session$clientData$url_search to pull in the query params from a user. Once I used shinylive to convert it, the queries passed to the URL did get passed onto the shinylive shiny session.

Not sure how large of use-case this is, but I would imagine some folks might want bookmarking to function so flagging for your attention.


library(shiny)

ui <- fluidPage(
  textOutput("time"),
  textOutput("search_values")
)

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

  ## Update timeVal every second with current time
  timeVal <- reactivePoll(1000, session,
                          checkFunc = function() {
                            Sys.time()
                          },
                          # This function returns the content of log_file
                          valueFunc = function() {
                            Sys.time()
                          }
  )

  output$time <- renderText({
    format(timeVal(),"%m-%d-%Y %H:%M:%S")
  })

  search <- reactive({
      timeVal()
      session$clientData
  })

  output$search_values <- renderText({
    format(search())
  })

}

shinyApp(ui, server)
scrapeable commented 10 months ago

shouldn't

session$clientData

be

parseQueryString(session$clientData$url_search)
thebioengineer commented 10 months ago

yes, for the specific problem posed. So it probably would have been better to have the example include that.

Admittedly, I got distracted while constructing the example and saw that session$clientData does have a fair amount of information that is lost on deployment via shinylive that folks might want.

scrapeable commented 10 months ago

@thebioengineer no worries was just mentioning in case that was the issue. I'm equally curious about parsing query parameters. Glad you asked the question.

schloerke commented 9 months ago

Oooooooo! I think I got this one haha.

The issue comes from the app being run in an iframe. The query string / hash are not being forwarded.

Smaller app that shows all information in the JS console:

library(shiny)

ui <- fluidPage(verbatimTextOutput("search_values"))

server <- function(input, output, session) {
  observe({
    str(reactiveValuesToList(session$clientData))
  })

  output$search_values <- renderText({
    invalidateLater(1000)
    paste(capture.output({
      print(Sys.time())
      str(getQueryString())
    }), collapse = "\n")
  })
}

shinyApp(ui, server)

While at url http://127.0.0.1:1234/?barret=1#asdf I had JS console messages:

preload echo:List of 10
shinylive.js:34937 preload echo: $ output_search_values_hidden: logi FALSE
shinylive.js:34937 preload echo: $ pixelratio                 : num 1.5
shinylive.js:34937 preload echo: $ url_protocol               : chr "http:"
shinylive.js:34937 preload echo: $ url_hostname               : chr "127.0.0.1"
shinylive.js:34937 preload echo: $ url_port                   : chr "1234"
shinylive.js:34937 preload echo: $ url_pathname               : chr "/app_u02o4jrgxutrye5cu93x/"
shinylive.js:34937 preload echo: $ url_search                 : chr ""
shinylive.js:34937 preload echo: $ url_hash_initial           : chr ""
shinylive.js:34937 preload echo: $ url_hash                   : chr ""
shinylive.js:34937 preload echo: $ singletons                 : chr ""

If I updated the iframe's src attr to be /app_u02o4jrgxutrye5cu93x/?barret=1#asdf, I had the JS console message:

preload echo:List of 10
shinylive.js:34937 preload echo: $ output_search_values_hidden: logi FALSE
shinylive.js:34937 preload echo: $ pixelratio                 : num 1.5
shinylive.js:34937 preload echo: $ url_protocol               : chr "http:"
shinylive.js:34937 preload echo: $ url_hostname               : chr "127.0.0.1"
shinylive.js:34937 preload echo: $ url_port                   : chr "1234"
shinylive.js:34937 preload echo: $ url_pathname               : chr "/app_u02o4jrgxutrye5cu93x/"
shinylive.js:34937 preload echo: $ url_search                 : chr "?barret=1"
shinylive.js:34937 preload echo: $ url_hash_initial           : chr "#asdf"
shinylive.js:34937 preload echo: $ url_hash                   : chr "#asdf"
shinylive.js:34937 preload echo: $ singletons                 : chr ""

🎉

Will make a PR in shinylive

thebioengineer commented 9 months ago

Amazing! Many thanks for looking into this.. hopefully it was an interesting one.

schloerke commented 9 months ago

George beat me by two weeks 🤣

We will wait on posit-dev/shinylive#79 .

seanbirchall commented 6 months ago

So I imagine I should be able to pass this by just running either of the below?

options(passUrlParams = TRUE) shiny::shinyApp(ui = ui, server = server, options = list(passUrlParams = TRUE))