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

Helpers to Grab JS Directly #37

Closed
jonthegeek opened this issue Nov 3, 2022 · 4 comments · Fixed by #48
Closed

Helpers to Grab JS Directly #37

jonthegeek opened this issue Nov 3, 2022 · 4 comments · Fixed by #48

Comments

@jonthegeek
Copy link
Contributor

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

jnolis commented Nov 3, 2022

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

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

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

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

jonthegeek added a commit that referenced this issue Jan 10, 2023
jonthegeek added a commit that referenced this issue Jan 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants