我正在尝试在Shiny界面中创建一个进度条。
具体来说,我有一个闪亮的界面,用户可以在其中选择值。当他单击“关闭并运行”时,界面关闭,并调用了多个脚本。
问题在于,我的加载栏是在Shiny应用打开时执行的,而不是在他单击按钮时执行的。
我想让进度条在他单击按钮后启动,并一直发展到脚本结尾,然后关闭。
这是app.R代码:
require(shiny)
require(shinyWidgets)
require(shinyjs)
pb <- winProgressBar(title="Example progress bar", label="0% done", min=0, max=100, initial=0)
jscode <- "shinyjs.closeWindow = function() { window.close(); }"
if (interactive()) {
# Open this application in multiple browsers, then close the browsers
shinyApp(
#Ui part
ui <- pageWithSidebar(
headerPanel("Filters for Datorama"),
sidebarPanel(
dateInput(inputId = "startDate", label = "Start date : ", value = "2018-12-01", format = "yyyy/mm/dd"),
dateInput(inputId = "endDate", label = "End date : ", value = "2018-12-31", format = "yyyy/mm/dd"),
selectInput(inputId = "client", label = "Choose your client : ", c("ING" = "ING", "Pernod-Ricard" = "Pernod-Ricard")),
selectInput(inputId = "brand", label = "Choose your template : ", c("Carat" = "Carat", "Amplifi" = "Amplifi", "iProspect" = "iProspect", "Vizeum" = "Vizeum")),
selectInput(inputId = "medium", label = "Choose the medium type : ", c("Social Post" = "Social Post")),
textInput(inputId = "iplan", label = "Enter the plan ID of your campaign : "),
useShinyjs(),
extendShinyjs(text = jscode, functions = c("closeWindow")),
actionButton("close", "Close and run"),
for(i in 1:100) {
Sys.sleep(0.1) # slow down the code for illustration purposes
info <- sprintf("%d%% done", round((i/100)*100))
setWinProgressBar(pb, i/(100)*100, label=info)
}
),
mainPanel()
),
#Server part
server = function(input, output, session) {
observe({
startDate <<- input$startDate
endDate <<- input$endDate
client <<- input$client
brand <<- input$brand
medium <<- input$medium
iPlan <<- input$iplan
})
onStop(function()
source("C:/Users/RPeete01/Desktop/Automated reporting/Social/Scripts/DatoramaSocial.R"))
observeEvent(input$close, {
close(pb)
js$closeWindow()
stopApp()
})
}
)
}