2

I would like to know if it is possible to extract the URL created by the Shiny Bookmark button? I know the Bookmark button is a special action button that can create a URL documenting all the input fields of a Shiny app. In addition to the default behavior, I want to save this URL to my Shiny app for further applications.

My ultimate goal is to see if I can feed in the Bookmark URL to the urlshorteneR package to create a shorten URL and report back to the users. When the Shiny app contains a lot of input fields, the Bookmark URL becomes lengthy. This is why I think it would be handy to be able to shorten the URL automatically.

Below is an example. The following script is an example from the enableBookmarking documentation. I uploaded it to the shinyapps.io and created this link (https://yuchenw.shinyapps.io/Bookmark_Example/).

# Basic example with state encoded in URL
ui <- function(request) {
  fluidPage(
    textInput("txt", "Text"),
    checkboxInput("chk", "Checkbox"),
    bookmarkButton()
  )
}
server <- function(input, output, session) { }
enableBookmarking("url")
shinyApp(ui, server)

Now if I want to save a state that the Checkbox is TRUE. I can click the Bookmark button and copy the URL (https://yuchenw.shinyapps.io/Bookmark_Example/?inputs&chk=true&txt=%22%22).

enter image description here

After that, I can use the urlshorteneR package to shorten the URL as follows.

library(urlshorteneR)

url <- "https://yuchenw.shinyapps.io/Bookmark_Example/?_inputs_&chk=true&txt=%22%22"

isgd_LinksShorten(longUrl = url, showRequestURL = TRUE)

This works well. It would be great if I can extract the Bookmark URL and automate this process. Thankr you in advance for any help or suggestions.

www
  • 38,575
  • 12
  • 48
  • 84
  • 2
    Are you aware of Saved-to-server bookmarking which results in short URLs? Try using `shinyApp(ui, server, enableBookmarking = "server")`. Please see [this](https://shiny.rstudio.com/articles/bookmarking-state.html) for further information. To extract the URL you can use `onBookmarked(function(url) {...})` [here](https://stackoverflow.com/questions/54426953/bookmarking-and-saving-the-bookmarks-in-r-shiny/54435930#54435930) is an example. – ismirsehregal Oct 15 '19 at 14:28

1 Answers1

2

Using your example this should do it:

library(shiny)
library(urlshorteneR)

# Basic example with state encoded in URL
ui <- function(request) {
  fluidPage(
    textInput("txt", "Text"),
    checkboxInput("chk", "Checkbox"),
    bookmarkButton(),
    htmlOutput("URLs")
  )
}

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

  onBookmarked(function(url) {
    URL(url)
  })

  output$URLs <- renderText({
    paste("Current URL:", URL(), br(), "Shortened URL:", isgd_LinksShorten(longUrl = URL(), showRequestURL = TRUE))})
}

enableBookmarking("url")
# enableBookmarking("server") # alternative

shinyApp(ui, server)

However, as mentioned in the comments I'd suggest using enableBookmarking = "server"

ismirsehregal
  • 30,045
  • 5
  • 31
  • 78
  • 2
    Thanks for your answer. It seems like `shinyapps.io` has not supported `enableBookmarking = "server"` yet. So the `onBookmarked` solution you provided will be useful if I want to publish on `shinyapps.io`. – www Oct 15 '19 at 20:14