1

I need to include a js file in a single-file shiny app. I tried to work with a two file app (classic UI-Server) and it works, but when I try to add the Javascript file into a single-file app, using the same structure, the browser can't find the js file.

This is the structure that I use into a two-file application, and works properly:

├── server.R
├── ui.R
└── www
   └── main.js

And this is the tag that I use to add the js file in the ui.R code:

tags$head(tags$script(src="main.js"))
JFernandez
  • 265
  • 4
  • 16
  • 1
    Possible duplicate of [Include a javascript file in Shiny app](http://stackoverflow.com/questions/23599268/include-a-javascript-file-in-shiny-app) – Pork Chop May 05 '17 at 07:54
  • As I said, I already added a js file in a Shiny app using Server.R and UI.R. What I ask is to add a js file in a single-file Shiny app. – JFernandez May 05 '17 at 08:43
  • Well then you need to declare it just before the `shinyServer(function(input,output,session) { }` – Pork Chop May 05 '17 at 09:01
  • Just put the `main.js` in www folder and call `tags$head(tags$script(src="main.js"))` in the `ui` function – HubertL May 05 '17 at 22:30

1 Answers1

0

To add to the solution:

If you want to include a js file from server, use:

ui = fluidPage(

useShinyjs(),
tags$head(tags$script(src="https://tab.worldbank.org/javascripts/api/tableau-2.min.js")))

If you want also want to include customized local js functions, add additional line with your path + js file name, e.g.:

extendShinyjs(script = "C:/Users/Desktop/YourFile.js")

Note here: in each js function inside extendShinyjs, it should be prefixed using shinyjs. e.g.:

shinyjs.switchView = function(sheetname) {
 var workbook = viz.getWorkbook();
 workbook.activateSheetAsync(sheetname);
 }
Daisywang
  • 287
  • 3
  • 17