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,450 points
164 views

3 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

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
92 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
364 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
85 views
0 votes
1 answer

How can I define Global Variables in R?

The variables declared inside a function are ...READ MORE

answered Apr 25, 2018 in Data Analytics by shams
• 3,580 points
965 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
311 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
169 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
47 views