I'm trying to fix a problem with browseURL, in that it doesn't work from a shiny server. I found a solution for shiny that extends browseURL so that it works, but I can't quite figure out how to apply that solution inside a quarto dashboard wrapper. I'll post the shiny code that works, and then post my failed attempt to incorporate that code into a quarto document.
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# https://shiny.posit.co/
#
library(shiny)
library(shinyjs)
# define js function for opening urls in new tab/window
js_code <- "
shinyjs.browseURL = function(url) {
window.open(url,'_blank');
}
"
URLs <- c("http://www.google.com", "http://www.stackoverflow.com")
ui <- fluidPage(
# set up shiny js to be able to call our browseURL function
useShinyjs(),
extendShinyjs(text = js_code, functions = 'browseURL'),
actionButton(
"click",
"Click here to open several browser tabs"
)
)
server <- function(input, output){
observeEvent(input$click, {
for (i in URLs){
js$browseURL(i)
Sys.sleep(1) #Short delay of 1 second
}
})
}
shinyApp(ui, server)
Here is my failed attempt (one of many) to translate this for quarto
title: "Reprex for opening browser tab"
author: "Alan Jackson"
format: dashboard
server: shiny
#| context: setup
library(tidyverse)
library(shiny)
library(shinyjs)
# define js function for opening urls in new tab/window
# https://stackoverflow.com/questions/41426016/shiny-open-multiple-browser-tabs
js_code <- "
shinyjs.browseURL = function(url) {
window.open(url,'_blank');
}
"
URLs <- c("http://www.google.com", "http://www.stackoverflow.com")
{.sidebar}
# set up shiny js to be able to call our browseURL function
shinyjs::useShinyjs()
shinyjs::extendShinyjs(text = js_code, functions = 'browseURL')
actionButton(
"click",
"Click here to open several browser tabs"
)
This is just some random text
#| context: server
observeEvent(input$click, {
for (i in URLs){
print(i)
js$browseURL(i)
Sys.sleep(1) #Short delay of 1 second
}
})