How can I include javascript code into my shiny R dashboard?

+6 votes
How can I include javascript code into my shiny R dashboard?
Dec 5, 2018 in Data Analytics by Ali
• 10,440 points
495 views

4 answers to this question.

+3 votes

There are multiple ways doing this. One of the way is using inline script text. 

include a <script> tag directly in your Shiny UI, using tags$script() and HTML():

ui <- fluidPage(
  tags$script(HTML(
    "document.body.style.backgroundColor = 'skyblue';"
  ))
)
answered Dec 5, 2018 by Maverick
• 10,040 points
+2 votes

Adding to @Maverick's answer, I'd like to add, another option is creating a separate .js file and saving it in a www subdirectory (it must be directly under your app directory). This is only slightly less convenient than inlined script text, and removes most of the downsides.

www/myscript.js:

document.body.style.backgroundColor = "red";

app.R:

ui <- fluidPage(
  tags$script(src = "myscript.js")
)
answered Dec 5, 2018 by Kailash
+2 votes

You can also use htmlDependancy, like this:

mypackageDependencies <- function() {
  htmlDependency(name = "mypackage-assets", version = "0.1",
    package = "mypackage",
    src = "assets",
    script = "js/myscript.js",
    stylesheet = c("css/reset.css", "css/mystyles.css")
  )
}
answered Dec 5, 2018 by Haseeb
0 votes

To include JavaScript, use includeScript() or
1. Place the file in the www subdirectory
2. Link to it with

tags$head(tags$script(src = "<file name>"))
answered Aug 30 by anonymous
• 31,840 points

Related Questions In Data Analytics

0 votes
1 answer

How can I measuring running time of R Code ?

5 ways to measure running time of ...READ MORE

answered Apr 17, 2018 in Data Analytics by anonymous
332 views
0 votes
1 answer

How can I import multiple .csv files into R ?

## import_multiple_csv_files_to_R # Purpose: Import multiple csv files ...READ MORE

answered Apr 19, 2018 in Data Analytics by zombie
• 3,690 points
840 views
0 votes
1 answer

How can I Split code over multiple lines in an R script?

You can do this as follows: setwd(paste("~/a/very/long/path/here ...READ MORE

answered May 21, 2018 in Data Analytics by zombie
• 3,690 points
133 views
0 votes
1 answer

How to include css code in shiny r app?

To include a CSS file, use includeCSS(), ...READ MORE

answered Aug 30 in Data Analytics by anonymous
• 31,840 points
94 views
0 votes
1 answer

Error saying "could not find function "shinyUI"" in shiny R

Its a small spelling mistake that you've ...READ MORE

answered Nov 28, 2018 in Data Analytics by Maverick
• 10,040 points
474 views
0 votes
1 answer

Error saying "could not find function dashboardPage" in shiny R

Include this line in the code: Library(shinydashboard) READ MORE

answered Nov 28, 2018 in Data Analytics by Maverick
• 10,040 points
289 views
0 votes
1 answer
0 votes
1 answer

How do I add Icons to my messages on my dashboard which I’m creating using shiny r?

Try something like this: dashboardHeader(title="Edureka", dropdownMenu type = "message", messageItem(from = ...READ MORE

answered Nov 29, 2018 in Data Analytics by Maverick
• 10,040 points
62 views