Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embed HTML UI Shiny application in package

Tags:

r

shiny

I'm trying to include a shiny implementation in a package I'm making. I see that in shiny::runApp() you can specify the UI and server as a list rather than a directory location:

runApp(list(
  ui = bootstrapPage(
    numericInput('n', 'Number of obs', 100),
    plotOutput('plot')
  ),
  server = function(input, output) {
    output$plot <- renderPlot({ hist(runif(input$n)) })
  }
))

How do you do this when using a custom HTML UI? And where should the relevant files be placed in a package's directory? The layout of the shiny application would look something like this:

server.R
www/
    style.css
    scripts.js

I think the solution might involve includeHTML(), includeCSS() etc., but I can't figure out what the documentation is saying.

like image 433
Ciarán Tobin Avatar asked Nov 19 '13 17:11

Ciarán Tobin


People also ask

Can you embed a Shiny app on website?

Yes! A shiny app can be put in an iframe in another website from RStudio Connect. In order to put the app in an iframe, it is recommended to add a content URL, because you can move it to another piece of content should the need arise.

How do you add a Shiny code in HTML?

You cannot put raw HTML directly into a tag or UI object. Shiny will treat raw HTML as a character string, adding HTML as text to your UI document. To add raw HTML, use the HTML function. HTML takes a character string and returns it as HTML (a special class of object in Shiny).


2 Answers

You package hierarchy should look like this :

R
  launcher.R
man 
inst
   myshinyapp
        server.R
        www
           index.html      ## here your ui interface
           shared
                 js
                    yourscript.js
                 css
                    style.css

in launcher.R should add this function:

runUI <- function ()
    shiny::runApp(
        system.file('myshinyapp',                                                    
         package='my_package_name')) ## your package name here

You should export/import in NAMESPACE:

export(runUI)
import(shiny)

and in DESCRIPTION imports sections:

Imports:shiny
like image 173
agstudy Avatar answered Sep 30 '22 01:09

agstudy


Well, you can achieve this by putting all your HTML inside the HTML() function. Make sure you pass valid HTML for Shiny then you should be fine!

runApp(list(
  ui = bootstrapPage(
    HTML("<h1>Hello World</h1>")
  ),
  server = function(input, output) {
    output$plot <- renderPlot({ hist(runif(input$n)) })
  }
))
like image 28
xiaodai Avatar answered Sep 30 '22 02:09

xiaodai