在闪亮的情况下,可以从服务器的逻辑中调用用javascript编写的客户端回调.说ui.R你有一些JavaScript,包括一个叫做的函数setText:
tags$script('
Shiny.addCustomMessageHandler("setText", function(text) {
document.getElementById("output").innerHTML = text;
})
')
Run Code Online (Sandbox Code Playgroud)
然后在你的server.R你可以打电话session$sendCustomMessage(type='foo', 'foo').
假设我有一个长时间运行的函数,它返回一些数据进行绘图.如果我这样做,R线程在运行此函数时很忙,因此在这段时间内无法处理其他请求.能够使用期货包运行此函数非常有用,因此它可以与代码异步运行,并且异步调用回调.但是,当我尝试这只是似乎没有工作.
对不起,如果这不是很清楚.作为一个简单的示例,以下内容应该有效,直到您取消注释尝试调用future的两行server.R.一旦取消注释这些行,就不会调用回调.显然它在这个例子的上下文中并没有实际用处,但我认为它在一般情况下非常有用.
ui.R:
library(shiny)
shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
sliderInput("max",
"Max random number:",
min = 1,
max = 50,
value = 30)
),
mainPanel(
verbatimTextOutput('output'),
plotOutput('plot')
)
),
tags$script('
Shiny.addCustomMessageHandler("setText", function(text) {
document.getElementById("output").innerHTML = text;
})
')
))
Run Code Online (Sandbox Code Playgroud)
server.R:
library(shiny)
library(future)
plan(multiprocess)
shinyServer(function(input, output, session) {
output$plot <- reactive({
max <- input$max
#f <- future({
session$sendCustomMessage(type='setText', 'Please wait') …Run Code Online (Sandbox Code Playgroud)