Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

url_search parsing a shinylive app #28

Open
thebioengineer opened this issue Oct 20, 2023 · 7 comments · May be fixed by posit-dev/shinylive#79
Open

url_search parsing a shinylive app #28

thebioengineer opened this issue Oct 20, 2023 · 7 comments · May be fixed by posit-dev/shinylive#79

Comments

@thebioengineer
Copy link

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
Copy link

shouldn't

session$clientData

be

parseQueryString(session$clientData$url_search)

@thebioengineer
Copy link
Author

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
Copy link

@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
Copy link
Collaborator

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
Copy link
Author

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

@schloerke
Copy link
Collaborator

George beat me by two weeks 🤣

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

@seanbirchall
Copy link

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))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants