How can I include javascript code into my shiny R dashboard

+7 votes
How can I include javascript code into my shiny R dashboard?
Dec 5, 2018 in Data Analytics by Ali
• 11,360 points
6,042 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,840 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, 2019 by anonymous
• 33,030 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
12,253 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 20, 2018 in Data Analytics by zombie
• 3,790 points
6,001 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 22, 2018 in Data Analytics by zombie
• 3,790 points
1,745 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, 2019 in Data Analytics by anonymous
• 33,030 points
1,952 views
+1 vote
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,840 points
2,341 views
+1 vote
1 answer

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

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

answered Nov 29, 2018 in Data Analytics by Maverick
• 10,840 points
3,153 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 30, 2018 in Data Analytics by Maverick
• 10,840 points
677 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP