R shiny bookmark button

Viewed 205

I try to change the text on the pop-up of the bookmarkbutton (other language), but i could not figure out how to do this? Does anyone have a clue:

library(shiny)
enableBookmarking(store = "url")

ui <- fluidPage(
  selectInput("test", label = "option", choices = c("opt A", "opt B"))
  , bookmarkButton(label = "")
)

server <- function(input, output) {
}

shinyApp(ui = ui, server = server)

Change the text in the red rounds

2 Answers

As said by @Tschösi these values are hard coded in the function, which leaves you with 2 options:

  1. You create an own copy of the function, where you replace the hard coded versions.
  2. You apply some Javascript to change the text dynamically.

Personally, I'd favor option 2.

Here's a working example of how to do that:

library(shiny)
js <- HTML(paste("$(function() {",
                 "$('body').on('shown.bs.modal', function() {",
                 "$('.modal-dialog .modal-header > .modal-title').text('Speicher Lesezeichen')", 
                 "$('.modal-dialog .modal-body > span:first').text('Dieser Link speichert den aktuellen Status der Applikation.')",
                 "$('#shiny-bookmark-copy-text').text('Drücken Sie Ctrl-C um den Link zu kopieren.')",
                 "$('.modal-dialog .modal-footer > button').text('Schließen')",
                 "})",
                 "})",
                sep = "\n"))
ui <- function(request) {
   fluidPage(
      tags$head(tags$script(js, type = "text/javascript")),
      tabsetPanel(id = "tabs",
                  tabPanel("One",
                           checkboxInput("chk1", "Checkbox 1"),
                           bookmarkButton(id = "bookmark1")
                  ),
                  tabPanel("Two",
                           checkboxInput("chk2", "Checkbox 2"),
                           bookmarkButton(id = "bookmark2")
                  )
      )
   )
}
server <- function(input, output, session) {
   # Need to exclude the buttons from themselves being bookmarked
   setBookmarkExclude(c("bookmark1", "bookmark2"))
   
   # Trigger bookmarking with either button
   observeEvent(input$bookmark1, {
      session$doBookmark()
   })
   observeEvent(input$bookmark2, {
      session$doBookmark()
   })
}
enableBookmarking(store = "url")
shinyApp(ui, server)

Modal with changed text

Using thothal suggestion, the issue is solved:

library(shiny)
enableBookmarking(store = "url")

ui <- fluidPage(
  useShinyjs()
  , tags$head(
    tags$script(src = "bookmark_vertaling.js")
  )
  , selectInput("test", label = "option", choices = c("opt A", "opt B"))
  , bookmarkButton(label = "", onclick = JS("bookmark_vertaling()"))
)

server <- function(input, output) {
}

shinyApp(ui = ui, server = server)

with this js-script named "bookmark_vertaling.js" in the www-directory:

function bookmark_vertaling() {
  $('body').on('shown.bs.modal', function() {
    $('.modal-dialog .modal-title').text('Rechtstreekse link naar deze pagina.');
    $('.modal-dialog .modal-body > span:first').empty();
    $('#shiny-bookmark-copy-text').text('Kopieer deze link (ctrl-c) om rechtstreeks naar deze pagina te linken.');
    $('.modal-dialog .modal-footer > button').text('Sluiten');
  });
}

This js-script is loaded in the dashboard using tags$script (make sure you have this script in the www-directory to make it available in the browser). And this js-function is called when I click on the bookmarkButton (onclick)

Related