Include a javascript file in Shiny app

前端 未结 5 1426
一生所求
一生所求 2020-11-29 02:02

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\')
<         


        
相关标签:
5条回答
  • 2020-11-29 02:25

    Another option not discussed yet is that you just delete the ui.R file entirely, and then code the entire thing as a custom HTML file. Details here https://shiny.rstudio.com/articles/html-ui.html

    In this article, the default HTML form elements are automatically used as inputs in server.R, but you can also build custom input (or output) elements for shiny with this guide https://shiny.rstudio.com/articles/building-inputs.html

    0 讨论(0)
  • 2020-11-29 02:33

    Another way is to use:

    includeScript("mapManipulator.js"),
    
    0 讨论(0)
  • 2020-11-29 02:40

    My preferred way is like this:

    ui.R:

        extendShinyjs(script = "app.js", functions = c("alerta")),
    

    app.js:

    shinyjs.alerta = function(text){
      alert(text);
    }
    

    server.R

      js$alerta("alerta alerta antifascista")
    

    You can also include code like this:

    ui.R after library imports:

    jsCode <- "shinyjs.alerta = function(text){alert(text);}"
    

    ui.R inside fluidPage:

    extendShinyjs(text = jsCode, functions = c("alerta")),
    

    the call from server.R would be the same

    0 讨论(0)
  • 2020-11-29 02:41
     └──shiny
        ├── server.R
        ├── ui.R
        └── www
            ├── stylesheet.css
            └── js
                 └── hoge.js
    

    ui.R

    Either one of them will work

    1. tags$head(HTML("<script type='text/javascript' src='js/hoge.js'></script>"))
    
    2. HTML('<head>
                  <link rel="stylesheet" type="text/css" href="stylesheet.css">
                  <script type="text/javascript" src="js/hoge.js"></script>
              </head>')
    
    0 讨论(0)
  • 2020-11-29 02:44

    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>
    
    0 讨论(0)
提交回复
热议问题