闪亮打开多个浏览器选项卡 [英] Shiny open multiple browser tabs
问题描述
在我的Shiny应用程序中,我想打开多个URL,而两次打开之间的延迟很短.这是一些示例代码,当我在RStudio中运行该应用程序时,效果很好.
库(发光)网址<-c("http://www.google.com," http://www.stackoverflow.com)ui<-fluidPage(actionButton(点击",单击此处打开几个浏览器选项卡"))服务器<-功能(输入,输出){watchEvent(input $ click,{用于(网址中的i){BrowseURL(i)Sys.sleep(1)#短暂延迟1秒}})}ShinyApp(用户界面,服务器)
但是,当我在Shinyapps.io上运行此应用程序时, browseURL()
不起作用(如解决方案
这是一个很老的问题,但是如果其他人在搜索时偶然发现,则会回答. >
正如您链接的参考文献中所述,我认为您需要使用一些JS来完成此任务.以下是使用 shinyjs
包定义光泽兼容的 browseURL
函数的示例.定义好函数后,将几行添加到 ui
中,然后在 server
中将其称为 js $ browseURL()
.
库(发光)图书馆(shinyjs)#定义用于在新标签页/窗口中打开网址的js函数js_code<-Shinyjs.browseURL = function(url){window.open(url,'_ blank');}"网址<-c("http://www.google.com","http://www.stackoverflow.com")ui<-fluidPage(#设置闪亮的js以便能够调用我们的browserURL函数useShinyjs(),extendShinyjs(text = js_code,functions ='browseURL'),actionButton(点击",单击此处打开几个浏览器选项卡"))服务器<-功能(输入,输出){watchEvent(input $ click,{用于(网址中的i){js $ browseURL(i)Sys.sleep(1)#短暂延迟1秒}})}ShinyApp(用户界面,服务器)
In my Shiny app I want to open several URL's with a short delay between opening. Here is some example code that works just fine when I run the app in my RStudio.
library(shiny)
URLs <- c("http://www.google.com", "http://www.stackoverflow.com")
ui <- fluidPage(
actionButton(
"click",
"Click here to open several browser tabs"
)
)
server <- function(input, output){
observeEvent(input$click, {
for (i in URLs){
browseURL(i)
Sys.sleep(1) #Short delay of 1 second
}
})
}
shinyApp(ui, server)
However, when I run this app on shinyapps.io, browseURL()
doesn't work (as mentioned here).
Does anyone know how to open multiple browser tabs with a short delay between opening them, so that it also works when the app is deployed on shinyapps.io? Would it be possible with R code or is JavaScript necessary?
This is a pretty old question, but answering in case others stumble upon while searching.
As mentioned in the reference you linked, I think you need to use some JS to accomplish this task. Below is an example of using the shinyjs
package to define a shiny compatible browseURL
function. Once we have the function defined we add a few lines to the ui
and then call it in the server
as js$browseURL()
.
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)
这篇关于闪亮打开多个浏览器选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!