In shiny how we can run script at the end of body tag? In the web programming world HTML loads from top to bottom. The head loads first, then the body, and then everything inside the body. If we put our JavaScript links in the head section, the entire JavaScript file will load before loading any of the HTML which could cause a few problems so I wanted to learn how we can put script at end of body tag.
library(shiny)
library(shinydashboard)
jscode <- "
window.close();
"
ui <- dashboardPage(
dashboardHeader(),
dashboardSidebar(),
dashboardBody(
actionButton("close", "Close app"),
tags$script(
"Shiny.addCustomMessageHandler('closeWindow', function(data) {
eval(data.message)
});"
)
)
)
server = function(input, output, session) {
observeEvent(input$close, {
session$sendCustomMessage(type = "closeWindow", list(message = jscode))
})
}
runApp(list(ui = ui, server = server), launch.browser =T)