1

I have a nice shiny app and would like to bookmark the state of the app and open a save file dialog that allows the user to save a URL shortcut to his/hers machine for later use. Example code below. Currently the app only shows the ulr link but does not create a url shortcut or save it anywhere. Seems simple enough but I haven't found any post regarding this directly and I haven't quite got it figured out myself.

    ui <- function(request) {
  fluidPage(
    textInput("txt", "Enter text"),
    checkboxInput("caps", "Capitalize"),
    verbatimTextOutput("out"),
    bookmarkButton()
  )
}
server <- function(input, output, session) {
  output$out <- renderText({
    if (input$caps)
      toupper(input$txt)
    else
      input$txt
  })
}

shinyApp(ui, server, enableBookmarking = "url")

I've found the following post useful but its not quite getting the result I'm looking for. link

CCurtis
  • 1,770
  • 3
  • 15
  • 25
  • 1
    I don't quite get your question. Your app is exactly doing what bookmarking in shiny promises: Generate a URL to save the state of the app (also have you seen https://shiny.rstudio.com/articles/bookmarking-state.html - its kind of an intro to bookmarking in shiny) – Thomas Dec 11 '20 at 11:11
  • @Thomas Yes the bookmark is working and provides a URL for the user save but once the user copies it they need to save it somewhere either creating a URL shortcut or saving it in a text/word file etc. I would like the app to open a save file dialog to save a URL shortcut for the user. Apologies for not stating this more clearly. – CCurtis Dec 11 '20 at 17:29
  • @Thomas yes I've seen that intro to bookmarking. – CCurtis Dec 11 '20 at 17:35

1 Answers1

1

I answer late but it could help the next visitors. You can use the shinyFiles package. The solution isn't aesthetic because you have to click first on the bookmarkButton before saving.

library(shiny)
library(shinyFiles)


ui <- function(request) {
  fluidPage(
    
    textInput("txt", "Enter text"),
    checkboxInput("caps", "Capitalize"),
    verbatimTextOutput("out"),
    bookmarkButton(id="bookmarkBtn"),
    shinySaveButton("save", "Save file", "Save file as ...", filetype=list(txt=".txt"))

  )
}

server <- function(input, output, session) {
  output$out <- renderText({
    if (input$caps)
      toupper(input$txt)
    else
      input$txt
  })
  
  myBookmarks <- reactiveValues(urlDF = NULL)  
  
  observeEvent(input$bookmarkBtn, {
    session$doBookmark()
  })
  
  onBookmarked(fun=function(url){
      myBookmarks$urlDF <- url
  })
  
  observeEvent(input$bookmarkBtn, {
    print(myBookmarks$urlDF)
  })
  
  observe({
    roots <- getVolumes()
    shinyFileSave(input, "save", roots=roots, session=session)
    fileinfo <- parseSavePath(roots, input$save)
    if (nrow(fileinfo) > 0) {
      write.table(myBookmarks$urlDF, as.character(fileinfo$datapath))
    }
  })
}

shinyApp(ui, server, enableBookmarking = "url")
  • Do you need to even use bookmarking in this case? Couldn't you just have a regular action button to trigger the saving process? I guess you use it to generate the url... I am trying this with the server option so I am saving the state of the app to a .rds file. If you have any ideas for reading it back in that would be great – see24 Nov 16 '21 at 15:47