knitr::opts_chunk$set(echo = TRUE)
help_console <- function(topic, format=c("text", "html", "latex", "Rd"),
                         lines=NULL, before=NULL, after=NULL) {  
  format=match.arg(format)
  if (!is.character(topic)) topic <- deparse(substitute(topic))
  helpfile = utils:::.getHelpFile(help(topic))

  hs <- capture.output(switch(format, 
                              text=tools:::Rd2txt(helpfile),
                              html=tools:::Rd2HTML(helpfile),
                              latex=tools:::Rd2latex(helpfile),
                              Rd=tools:::prepare_Rd(helpfile)
  )
  )
  if(!is.null(lines)) hs <- hs[lines]
  hs <- c(before, hs, after)
  cat(hs, sep="\n")
  invisible(hs)
}

Installation from github

To install the package from GitHub use the devtools package:

devtools::install_github('sigbertklinke/shinyExample')

Notes:

  1. To use the Shiny app you need to have installed Ghostscript and ImageMagick before.
  2. The app may not work on Windows systems.

shinyExample

shinyExample allows you to run R-, Octave- of Stata programs which produce only console output or one plot. For teaching purposes the source coude, the console output and the plot can be viewed. The app supports some limited interactivity: you can rerun the program which is especially in the framework of inferential statistics useful to show the influence of drawing a new (random) sample.

Interactive app

Clicking on the opens a sidebar which allows further operations.

The Shiny app will run in R (via runShinyExample()), but of course is intended to be embedded via an iframe into web pages. With URL given you can determine the starting parameters of the app. The URL

https://shinyapps.wiwi.hu-berlin.de/examples?P=stat/Statistik/lottozahlen.R&V=P

forces the example app to open the given program (assumed it exists on the server) and to show the plot window. For MediaWiki exists the Iframe extension to embed the app.

Following query parameters exists

Static app

Since each run of a program creates a set of HTML pages you can also embed these HTML pages rather the app itself. In web page you should not embed more than four apps, otherwise the server will not be able to display empty results. If you once made a program which produced a good plot or output then can click on STATIC link to get the URL for this plot or output. If you embed that link into your webpage then you will this HTML page with INTERACTIVE link to the app.

Installing the app on a Shiny server

  1. You copy everything under inst/examples into a shiny apps directoy of the server, e.g. /srv/shiny-server/examples
  2. The subdirectories rds, run and tmp should be writetable for the Shiny server.
  3. Place all necessary data files your example programs might need into the subdirectory run
  4. The subdirectory R contains your example programs, maybe with a further subdirectory structure.

ShinyApp

The ShinyApp part of the library is intended to convert quickly programm snippets to an interactive shiny app. For complex apps it is better to use shiny directly. A quick overview about availabe commands

Skeleton

The basic skeleton for generating is the R program

library("shinyExample")
ShinyApp() %>%
WriteApp()

The ShinyApp starts coding an interactive app and WriteApp writes the app into a file app.R.

The ShinyApp command starts

ShinyApp examples

The histogram example

A base histogram

A typical code snippet which simply shows a histogram might be (stored in a file called hist0.R)

hist0.R

library("MASS")
x <- Boston$medv
hist(x)

The first step is to create a file, e.g. named hist0_app.R which contains the app environment. Do not forget to set your working directory to the directory where hist0.R is stored.

hist0_app.R

library("shinyExample")
ShinyApp() %>%
PlotOutput('plot', file='hist0.R') %>%
WriteApp()  

The output parameter tells Shiny that you have one output element named plot which contains a plot (plotOut). The code to generate the plot is contained in your file hist0.R.

Running this R program will generate and open in RStudio a file named app.R. Running the app with the Run app button will run your app

A histogram with bins

The standdard example in Shiny is a histogram where the number of bins can be modified for the data set. We need to create an input element which allows us to modify the number of breaks.

hist1.R

library("MASS")
x <- Boston$medv
hist(x, breaks=value(input$breaks))

We will name the input elements breaks. If you create an shiny app then you will become the current value of the input element by input$breaks. The function value ensures that you always get a valid value; during an initialisation of an Shiny app it happens that input$breaks is NULL.

hist1_app.R

library("shinyExample")
elem <- sliderIn('breaks', 'Number of bins', min=1, max=50, value=10)
makeShinyApp(input=elem,
             output=plotOut('plot', file='hist1.R'))

elem contains the slider with name breaks which is later used for input$breaks. To the call of makeShinyApp we add now this input elements.

Looking at the histogram you will notice that we have only 9 bins instead of 10 bins as the slider says. When playing with the number of bins it seems that sometimes the histogram does not react on your changes. This is a problem of hist which modifies your value of breaks to a nearby value. For getting exactly input$breaks bins you have to set class borders explicitly.

hist2.R

library("MASS")
x <- Boston$medv
b <- seq(min(x), max(x), length.out=value(input$breaks)+1)
hist(x, breaks=b)

hist2_app.R

library("shinyExample")
elem <- sliderIn('breaks', 'Number of bins', min=1, max=50, value=10)
makeShinyApp(input=elem,
             output=plotOut('plot', file='hist2.R'))

Now you really get 10 bins for your histogram

When running your app in RStudio you see the following output

gettext("Number of bins"); # 1

Currently you can ignore this, but if you want internationalise your app then you need this information.

Rug your histogram

The standard Shiny example allows you to add the observations to your histogram, which requires two input elements

hist3.R

library("MASS")
x <- Boston$medv
b <- seq(min(x), max(x), length.out=value(input$breaks)+1)
hist(x, breaks=b)
if(value(input$rug)) rug(x)

Create your input elements and combine them into a list

hist3_app.R

library("shinyExample")
elem1 <- sliderIn('breaks', 'Number of bins', min=1, max=50, value=10)
elem2 <- checkboxIn('rug', 'Show observations')
makeShinyApp(input=list(elem1, elem2),
             output=plotOut('plot', file='hist3.R'))

Derived input elements

The following additional input elements are derived from



sigbertklinke/shinyExample documentation built on May 26, 2019, 4:32 a.m.