<?xml version="1.0" encoding="utf-8"?><!DOCTYPE article  PUBLIC '-//OASIS//DTD DocBook XML V4.4//EN'  'http://www.docbook.org/xml/4.4/docbookx.dtd'><article><articleinfo><title>Shiny</title><revhistory><revision><revnumber>8</revnumber><date>2024-03-27 12:56:44</date><authorinitials>crose</authorinitials></revision><revision><revnumber>7</revnumber><date>2023-11-28 13:21:47</date><authorinitials>kputyr</authorinitials></revision><revision><revnumber>6</revnumber><date>2023-05-16 06:12:00</date><authorinitials>crose</authorinitials></revision><revision><revnumber>5</revnumber><date>2023-04-22 14:30:57</date><authorinitials>crose</authorinitials></revision><revision><revnumber>4</revnumber><date>2023-04-22 14:29:11</date><authorinitials>crose</authorinitials></revision><revision><revnumber>3</revnumber><date>2023-04-22 14:28:58</date><authorinitials>crose</authorinitials></revision><revision><revnumber>2</revnumber><date>2018-10-29 16:27:11</date><authorinitials>bbaer</authorinitials></revision><revision><revnumber>1</revnumber><date>2018-10-29 12:22:50</date><authorinitials>bbaer</authorinitials></revision></revhistory></articleinfo><section><title>Shiny</title><informaltable><tgroup cols="2"><colspec colname="col_0"/><colspec colname="col_1"/><tbody><row rowsep="1"><entry colsep="1" rowsep="1"><para> Product </para></entry><entry colsep="1" rowsep="1"><para> <ulink url="http://shiny.rstudio.com"/> </para></entry></row><row rowsep="1"><entry colsep="1" rowsep="1"><para> Documentation </para></entry><entry colsep="1" rowsep="1"><para> <ulink url="http://shiny.rstudio.com/tutorial/">Getting Started</ulink>, <ulink url="http://shiny.rstudio.com/gallery/">Gallery</ulink>, <ulink url="http://shiny.rstudio.com/articles/">Articles</ulink>, <ulink url="http://rmarkdown.rstudio.com/">Markdown</ulink> </para></entry></row><row rowsep="1"><entry colsep="1" rowsep="1"><para> IMATH Shiny Server </para></entry><entry colsep="1" rowsep="1"><para> <ulink url="https://shiny.math.uzh.ch"/> </para></entry></row><row rowsep="1"><entry colsep="1" rowsep="1"><para> Log Files </para></entry><entry colsep="1" rowsep="1"><para> <ulink url="http://shiny.math.uzh.ch:8080/"/> (accessible only via intranet=thinlinc) </para></entry></row></tbody></tgroup></informaltable><para>The Shiny Server searches for R scripts in the folder it is pointed to and translates the shiny R script to normal HTML that any browser can read. </para></section><section><title>Turn your R script into a webpage</title><itemizedlist><listitem><para>Ask for a personal <ulink url="https://wiki.math.uzh.ch/public/Shiny/public/Homepage#">Homepage</ulink> if you do not have one </para></listitem><listitem><para>In <code>~/public_html</code> create one folder for each shiny app and copy your R scripts there. <emphasis>Grant access to your files (only necessary if you created new files) by executing</emphasis> <code>setwww</code> <emphasis>in a linux console.</emphasis>  </para></listitem><listitem><para>URL to your app: <code>http://shiny.math.uzh.ch/user/&lt;NAME&gt;/&lt;FOLDER&gt;/</code> where </para><itemizedlist><listitem><para><emphasis role="strong">NAME</emphasis> is typically your lastname, lowercase </para></listitem><listitem><para><emphasis role="strong">FOLDER</emphasis> is the subfolder in <code>public_html</code> with the scripts </para></listitem></itemizedlist><para>Example: <ulink url="http://shiny.math.uzh.ch/user/doe/shinyapp1/"/> </para></listitem></itemizedlist></section><section><title>Show the shiny app as a part of another page</title><para>Use an iframe to embed your shiny app on another website as follows: </para><screen><![CDATA[<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> ]]></screen><para>With the style part of the command, you can customize the size and look of the frame: </para><itemizedlist><listitem><para><emphasis role="strong">border</emphasis>: set the thickness of the border, the border color as well as the style it should be displayed in (solid, dotted, stripped). You can also deactivate the border completely by writing <emphasis>border: none;</emphasis> </para></listitem><listitem><para><emphasis role="strong">width</emphasis>: set a custom width to the frame. Normally very dependent on the app you load. </para></listitem><listitem><para><emphasis role="strong">height</emphasis>: set a height. Usually you just want the frame to be high enough that no scrollbar appears. </para></listitem><listitem><para><emphasis role="strong">float</emphasis>: 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, use <emphasis>float: left</emphasis> on one and <emphasis>float: right</emphasis> on the other. </para></listitem></itemizedlist></section><section><title>Deployment with gitlab-runner</title><itemizedlist><listitem><para>Example deployment: <ulink url="https://git.math.uzh.ch/bbaer/publish-shiny"/> example <code>.gitlab-ci.yml</code> file. </para></listitem><listitem><para>A commit to the GIT Repo will trigger the deployment described by <code>.gitlab-ci.yml</code> (via a gitlab-runner on alfred16, alfred20 or alfred22) </para></listitem></itemizedlist></section><section><title>Debugging Apps</title><itemizedlist><listitem><para>Ask IT Support for sudo rights on the shiny server. </para></listitem><listitem><para>Activate <code>persistent logs</code>: </para><screen><![CDATA[[root@shiny22]
]]><![CDATA[
# Option einfügen unter zeile 'run_as shiny;'
$ vim /etc/shiny-server/shiny-server.conf
...
run_as shiny;
]]><![CDATA[
preserve_logs true;
...]]></screen></listitem><listitem><para>Examine the log file for errors, for example a missing or an outdated package (an example from SampleSizeR) </para><screen><![CDATA[$ less /var/log/shiny-server-git/SampleSizeR-shiny-20230420-132522-42355.log
...
Error in library(ipc) : there is no package called ‘ipc’
...
Error : package or namespace load failed for ‘plotly’ in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]):
 namespace ‘vctrs’ 0.6.3 is already loaded, but >= 0.6.4 is required
...]]></screen><itemizedlist><listitem><para>the missing package exists in Ubuntu repo then it can be intalled directly </para><screen><![CDATA[$ sudo apt list | grep r-cran-ipc
r-cran-vctrs/jammy,now 0.6.4-1cran1.2204.0 amd64
$ sudo apt install r-cran-vctrs]]></screen><para>Otherwise it must be installed from within R: </para><screen><![CDATA[$ sudo "R -e \"install.packages('vctrs')\""]]></screen></listitem><listitem><para>confirm the current version </para><screen><![CDATA[$ R -q -s -e "packageVersion('vctrs')"
[1] ‘0.6.4’]]></screen></listitem></itemizedlist></listitem><listitem><para>In case of other errors please check the R code </para></listitem></itemizedlist><para><emphasis role="strong">Important</emphasis>: persitent logs create huge log files - deactivate it when finished! </para></section><section><title>Samples</title><para>The samples displayed on <ulink url="http://shiny.math.uzh.ch"/> page are the following </para><section><title>R Sample: Hello</title><para>The folder hello includes two files. </para><itemizedlist><listitem><para>server.R </para><screen><![CDATA[library(shiny)
]]><![CDATA[
# Define server logic required to draw a histogram
shinyServer(function(input, output) {
]]><![CDATA[
  # 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
]]><![CDATA[
  output$distPlot <- renderPlot({
    x    <- faithful[, 2]  # Old Faithful Geyser data
    bins <- seq(min(x), max(x), length.out = input$bins + 1)
]]><![CDATA[
    # draw the histogram with the specified number of bins
    hist(x, breaks = bins, col = 'darkgray', border = 'white')
  })
]]><![CDATA[
})]]></screen></listitem><listitem><para>ui.R </para><screen><![CDATA[library(shiny)
]]><![CDATA[
# Define UI for application that plots random distributions
shinyUI(pageWithSidebar(
]]><![CDATA[
  # Application title
  headerPanel("It's Alive!"),
]]><![CDATA[
  # Sidebar with a slider input for number of observations
  sidebarPanel(
    sliderInput("bins",
                  "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30)
  ),
]]><![CDATA[
  # Show a plot of the generated distribution
  mainPanel(
    plotOutput("distPlot", height=250)
  )
))]]></screen></listitem></itemizedlist></section><section><title>R Markdown Sample: rmd</title><para>The rmd Folder includes only one file: </para><itemizedlist><listitem><para>index.Rmd </para><screen><![CDATA[---
title: "Shiny Doc"
output: html_document
runtime: shiny
---
]]><![CDATA[
```{r, echo=FALSE}
shinyApp(
]]><![CDATA[
  ui = fluidPage(
    selectInput("region", "Region:",
                choices = colnames(WorldPhones)),
    plotOutput("phonePlot", height=270)
  ),
]]><![CDATA[
  server = function(input, output) {
    output$phonePlot <- renderPlot({
      barplot(WorldPhones[,input$region]*1000,
              ylab = "Number of Telephones", xlab = "Year")
    })
  },
]]><![CDATA[
  options = list(height = 345)
)
```]]></screen></listitem></itemizedlist></section></section></article>