Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
487 views
in Technique[技术] by (71.8m points)

r - Include a javascript file in Shiny app

I need to include a js library into my Shiny app. Currently I use includeHTML to include the script directly into html codes. e.g.

includeHTML('URL.js')

The browser will show "Not Found" when I try to browser the js file if I use tags$script, e.g.

http://127.0.0.1:7106/URL.js

tags$script(src = 'URL.js')

Now I put URL.js in the same folder of ui.r and server.r.

Where I should store the URL.js file? Or are there other ways to include a js file?

Thanks for any suggestions.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

What you need to do is:

  1. create www folder in the same folder as server.R and ui.R
  2. put javascript file into www folder.
  3. put tags$head(tags$script(src="hoge.js")) in UI.

The folder looks like:

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

The ui.R is something like

library(shiny)
shinyUI(pageWithSidebar(
  headerPanel("New Application"),
  sidebarPanel(
    sliderInput("obs", 
                "Number of observations:", 
                min = 1, 
                max = 1000, 
                value = 500)
  ),
  mainPanel(
    plotOutput("distPlot"),
    tags$head(tags$script(src="hoge.js"))
  )
))

and server.R

library(shiny)
shinyServer(function(input, output) {
  output$distPlot <- renderPlot({
    dist <- rnorm(input$obs)
    hist(dist)
  })
})

Note that these are templates generated by Rstudio.

Now head of html looks like:

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  ... snip ...
  <script src="shared/slider/js/jquery.slider.min.js"></script>
  <script src="hoge.js"></script>
</head>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...