Extension:Shiny

The Shiny extension allows to embed Shiny apps in pages. The Shiny server uses itself the programming language R, which is a free software environment for statistical computing and graphics, on pages.

Requirements

 * R is a free software environment for statistical computing and graphics. It needs to be installed (probably available in your Linux distribution) and in the PATH: the command  should work on the command line.


 * The Shiny server lets you put shiny web applications online. It needs to be installed separately.

Download
You can obtain the extension from GitHub.

Installation
The following notes describe the installation process for Ubuntu 14.04.4 LTS with an Apache web server. For other linux systems or web servers the description needs to be adapted. For all steps it is assumed that you are logged in as root.

R
You may install a R version which comes with your linux distribution by

apt-get install r-recommended

However, the instructions for the Shiny server urge to use the latest R version which may not (yet) part of the repositories of your linux distribution.

deb https:///bin/linux/ubuntu trusty/ apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E084DAB9 apt-get update apt-get install r-recommended After that R should have been installed (or updated) from the choosen CRAN mirror.
 * Therefore you need to add a CRAN repository to the file /etc/apt/sources.list
 * Then run

Shiny server
R -e "install.packages('shiny', repos='https://cran.rstudio.com/')" apt-get install gdebi-core wget https://download3.rstudio.org/ubuntu-12.04/x86_64/shiny-server-1.4.2.786-amd64.deb gdebi shiny-server-1.4.2.786-amd64.deb cd /srv/shiny-server mkdir mediawiki chown www-data:www-data mediawiki
 * Install the shiny package in R
 * Install the gdebi package
 * Download the Shiny server (check if the version 1.4.2.786 still is okay)
 * Install the Shiny server to your system
 * To check if the shiny server works enter in your browser: http://:3838
 * Add a path and permissions to store the shiny apps

MediaWiki
cd /extensions apt-get install git git clone https://guthub.com/sigbertklinke/Shiny wfLoadExtension( 'Shiny' );
 * After the installation of you wiki go to the extensions</tt> directory
 * If necessary then install git</tt>
 * Clone the Shiny extension from github.com</tt>
 * Edit your <my.wiki>/LocalSettings.php</tt> and add at the end

Apache
The Apache webserver listens to port 80 and the Shiny server to port 3838. Usually I modify the Apache configuration file such that any URL with starts with http://<my.domain>/shiny</tt> is redirected to the Shiny server:

cd /etc/apache2 a2enmod proxy_http <VirtualHost *:80> ... some other config stuff ... <Proxy /shiny/> Allow from localhost </Proxy> ProxyPreserveHost On  ProxyPass /shiny/ http://localhost:3838/ ProxyPassReverse /shiny/ http://localhost:3838/ </VirtualHost> service apache2 restart
 * Go to the Apache configuration directory
 * Enable the proxy_http</tt> module
 * Edit the Apache configuration file /etc/apache2/sites-available/000.default.conf</tt> and add at the end
 * Restart the apache server
 * To check if this works properly enter in your browser: http://<my.domain>/shiny/</tt> (and when you enter http://<my.domain>:3838/</tt> you should see the same page)

Extension files

 * extension.json</tt>
 * stores the setup instructions.


 * Shiny_body.php</tt>
 * stores the execution code for the extension.

All other files in the <my.wiki>/extensions/Shiny</tt> could be deleted.

Configuration
The file extension.json</tt> contains three configuration variables which could be set:

"config": { "ShinyR":     "/usr/bin/R", "ShinyAppDir": "/srv/shiny-server/mediawiki", "ShinyUrl":   "http://localhost/shiny/mediawiki" },


 * ShinyR</tt>
 * the path and command to execute R. You may query it in your system for it by which R</tt>


 * ShinyAppDir</tt>
 * the path where the Shiny apps are stored. Note that the actually path where app.R</tt> is stored is <tt>/srv/shiny-server/mediawiki/<my.wiki.name>/<page.title>/<shiny.app.name</tt>


 * <tt>ShinyUrl</tt>
 * the URL which is called to access a Shiny app. Note that the actually URL for a shiny app is <tt>http://localhost/shiny/mediawiki/<my.wiki.name>/<page.title>/<shiny.app.name</tt>

Usage
For a simple example put into a wiki page: <shiny output="plot" name="test"> hist(rnorm(314)) It shows a histogram of 314 standard normal distributed.

Some hints for development:
 * For developing an Shiny app you are urged to use RStudio. If the app runs under RStudio then it will also work with MediaWiki.
 * If you do not intend to write interactive apps then you should consider Extension:R.

<tt>shiny</tt> tag
The <tt>shiny</tt> tag can have the following attributes:


 * <tt>output="..."</tt>

! <tt>...</tt> || corresponds to
 * <tt>data</tt> || <tt>dataTableOutput</tt> and <tt>renderDataTable</tt>
 * <tt>image</tt> || <tt>imageOutput</tt> and <tt>renderImage</tt>
 * <tt>plot</tt> || <tt>plotOutput</tt> and <tt>renderPlot</tt>
 * <tt>print</tt> || <tt>verbatimTextOutput</tt> and <tt>renderPrint</tt>
 * <tt>table</tt> || <tt>tableOutput</tt> and <tt>renderTable</tt>
 * <tt>text</tt> || <tt>textOutput</tt> and <tt>renderText</tt>
 * <tt>verb</tt> || <tt>print</tt>
 * }
 * Note if <tt>output</tt> is omited then between the start and end tag is a fully fledged shiny app is expected.
 * <tt>table</tt> || <tt>tableOutput</tt> and <tt>renderTable</tt>
 * <tt>text</tt> || <tt>textOutput</tt> and <tt>renderText</tt>
 * <tt>verb</tt> || <tt>print</tt>
 * }
 * Note if <tt>output</tt> is omited then between the start and end tag is a fully fledged shiny app is expected.
 * }
 * Note if <tt>output</tt> is omited then between the start and end tag is a fully fledged shiny app is expected.


 * <tt>name="..."</tt>
 * the name of the Shiny app. If not set then a hash value from the R program is derived.
 * CAUTION: Omitting the name may lead to a lot of subdirectories for the shiny server, therefore choose a name!


 * <tt>width="..."</tt>
 * the width of the windows in pixel (default: 800)


 * <tt>height="..."</tt>
 * the height of the windows in pixel (default: 600)


 * <tt>seamless="..."</tt>
 * if the Shiny app is embedded seamlessly (<tt>seamless="seamless"</tt>, default) or not (any other text for seamless)


 * <tt>sandbox="..."</tt>
 * enables extra restrictions on the content that can appear in the inline frame. (default: <tt>allow-same-origin allow-scripts</tt>). The value of the attribute can either be an empty string (all the restrictions are applied), or a space-separated list of tokens that lift particular restrictions. Valid tokens are:

! Token || Allows
 * <tt>allow-forms</tt> || the embedded browsing context to submit forms
 * <tt>allow-forms</tt> || the embedded browsing context to submit forms


 * <tt>allow-popups</tt> || to open popups (like from window.open, target="_blank", showModalDialog)
 * <tt>allow-same-origin</tt> || the content to be treated as being from its normal origin
 * <tt>allow-scripts</tt> || the embedded browsing context to run scripts (but not create pop-up windows)
 * <tt>allow-top-navigation</tt> || the embedded browsing context to navigate (load) content to the top-level browsing context.
 * }
 * <tt>allow-top-navigation</tt> || the embedded browsing context to navigate (load) content to the top-level browsing context.
 * }
 * }

Errors

 * In the wiki page is only an <tt><iframe ...>... </tt> shown instead of the Shiny app.
 * In case that an error occurs the extension shows the resulting HTML code instead of embedding. Check for typos/errors in the options or in your R program.


 * In the Shiny app is only displayed <tt>ERROR: Key / already in use</tt>
 * Delete from your R code the <tt>runApp(...)</tt> line.

Security
Currently are no security measures implemented.

Web links

 * Ubuntu packages for R
 * Download and Installing Shiny Server Open Source