shinyworks / cookies

Use Browser Cookies with 'shiny'
https://shinyworks.github.io/cookies/
Other
32 stars 4 forks source link

Helpers to Grab JS Directly #37

Closed jonthegeek closed 1 year ago

jonthegeek commented 1 year ago

Provide a helper function to add the JS to HTML more directly.

@jnolis: Can you give me a snippet of how you envision this working?

jnolis commented 1 year ago

I would provide two things (and maybe both exist so it's a matter of just documentation)

1. A function like cookie_js()

So that you could do something like:

ui <- bootstrapPage(
tags$head(cookie_js())
)

This is close to what you already have, except you get to specify where the javascript goes rather than having it handled for you on the outside. I think it's like, a little clear to be able to do it this way rather than wrap an entire UI in a function call, but that might just be my preference.

2. Just the actual HTML

Like somewhere in the docs say "you can also skip this entirely by manually adding the following to your HTML. This is most useful to use with functions like htmlTemplate() where you are loading HTML directly into shiny", and then provide:

<script type="text/javascript" src="js.cookie.js"></script>
<script type="text/javascript" src="cookie_input.js"></script>

Along with links to download the files.

As an aside, you really want to engineer it might want to make those two files downloadable from one of those global javascript repositories so people don't need to even place the files in the shinyapp themselves. Like for example here is bootstrap doing that: https://getbootstrap.com/docs/5.2/getting-started/introduction/#cdn-links

jonthegeek commented 1 year ago

Fwiw, cookie_dependency() should work in the first case. I'll make that clearer in the documentation. I THINK it will even work if you're otherwise providing the html "raw", but I haven't tried it yet.

I've seen examples of downloading the JS but I haven't wrapped my head around how to make that work cleanly in the package yet. I'll add an issue to sort that out, though!

jonthegeek commented 1 year ago

FYI these all work. View source to see that the libs are there.

I'll add documentation for these use cases! I'll probably still make the JS directly loadable but that's mostly what cookie_dependency() does (just using the htmltools::htmlDependency() style).

library(shiny)
library(cookies)
ui <- bootstrapPage(
  tags$head(cookie_dependency()) # Technically this can be anywhere, Shiny makes sure it goes to the head.
)
server <- function(input, output, server) {}
shinyApp(ui, server, options = list(launch.browser = TRUE))

You need a testing.html file (or to swap in your own) to test these.

library(shiny)
library(cookies)
ui <- shiny::tagList(
  cookie_dependency(),
  htmlTemplate(here::here("testing.html"))
)
server <- function(input, output, server) {}
shinyApp(ui, server, options = list(launch.browser = TRUE))
ui <- add_cookie_handlers(htmlTemplate(here::here("testing.html")))
server <- function(input, output, server) {}
shinyApp(ui, server, options = list(launch.browser = TRUE))
jonthegeek commented 1 year ago

I'm clarifying the help documentation to cover this as much as I plan to cover it.