location: Shiny

Institute of Mathematics - PublicMathWiki:

Shiny

  • A web application framework for R

URLs

The Shiny Server searches for .R scripts in the folder he is pointed to and translates the shiny R script to normal HTML any Browser can read.

Turn your R script into a webpage

  • You need an IMATH account with a public_html folder.

    • If you don't have this, contact support@math.uzh.ch.

  • In ~/public_html create one folder for each shiny app and copy your R scripts to it.

    • Grant access to your files (only necessary if you created new files): Execute  setwww  in a linux console.

  • NAME: <typically your lastname, lowercase>

    • Example: For user 'John Doe' it will doe.

  • FOLDER: <directory below ~/public_html>.

    • Example: /home/b/john/public_html/shinyapp1 >> FOLDER: shinyapp1

  • URL: http://shiny.math.uzh.ch/user/<NAME>/<FOLDER>/

Show the shiny app as a part of another page

  • Put the following code on the page:

<iframe src="http://shiny.math.uzh.ch/user/doe/shinyapp1/" style="border: 1px solid #AAA; width: 290px; height: 500px"> Your Browser doesn't support iframe </iframe> 
  • Note: the shiny server is only accessible via http. If your page is accessed via https some browser will complain about the protocol change.

iframe

An iframe simply displays another website in your website. With the style part of the command, you can customize the size of the frame.

  • border: set the thickness of the border, the border color aswell as the style it should be displayed in. (solid, dotted, stripped) You can also deactivate the border completely by writing border: none;
  • width: set a custom width to the frame. Normally very dependent on the app you load.
  • height: set a height. Usually you just want the frame to be high enough that no scrollbar appears.
  • float: You can let the frame float to either the left or right side of the text. If you want two iframes displayed side by side, add left to one and right to the other.

Samples

The samples displayed on http://shiny.math.uzh.ch page are the following

R Sample: Hello

The folder hello includes two files.

  • server.R
    library(shiny)
    
    # Define server logic required to draw a histogram
    shinyServer(function(input, output) {
    
      # Expression that generates a histogram. The expression is
      # wrapped in a call to renderPlot to indicate that:
      #
      #  1) It is "reactive" and therefore should be automatically
      #     re-executed when inputs change
      #  2) Its output type is a plot
    
      output$distPlot <- renderPlot({
        x    <- faithful[, 2]  # Old Faithful Geyser data
        bins <- seq(min(x), max(x), length.out = input$bins + 1)
    
        # draw the histogram with the specified number of bins
        hist(x, breaks = bins, col = 'darkgray', border = 'white')
      })
    
    })
  • ui.R
    library(shiny)
    
    # Define UI for application that plots random distributions
    shinyUI(pageWithSidebar(
    
      # Application title
      headerPanel("It's Alive!"),
    
      # Sidebar with a slider input for number of observations
      sidebarPanel(
        sliderInput("bins",
                      "Number of bins:",
                      min = 1,
                      max = 50,
                      value = 30)
      ),
    
      # Show a plot of the generated distribution
      mainPanel(
        plotOutput("distPlot", height=250)
      )
    ))

R Markdown Sample: rmd

The rmd Folder includes only one file:

  • index.Rmd
    ---
    title: "Shiny Doc"
    output: html_document
    runtime: shiny
    ---
    
    ```{r, echo=FALSE}
    shinyApp(
    
      ui = fluidPage(
        selectInput("region", "Region:",
                    choices = colnames(WorldPhones)),
        plotOutput("phonePlot", height=270)
      ),
    
      server = function(input, output) {
        output$phonePlot <- renderPlot({
          barplot(WorldPhones[,input$region]*1000,
                  ylab = "Number of Telephones", xlab = "Year")
        })
      },
    
      options = list(height = 345)
    )
    ```

PublicMathWiki: Shiny (last edited 2018-10-29 16:27:11 by bbaer)