location: Diff for "Shiny"

Institute of Mathematics - PublicMathWiki:

Differences between revisions 4 and 8 (spanning 4 versions)
Revision 4 as of 2023-04-22 14:29:11
Size: 5029
Editor: crose
Comment:
Revision 8 as of 2024-03-27 12:56:44
Size: 5767
Editor: crose
Comment:
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:
 A web application framework for R
|| Product || http://shiny.rstudio.com ||
|| Documentation || [[http://shiny.rstudio.com/tutorial/|Getting Started]], [[http://shiny.rstudio.com/gallery/|Gallery]], [[http://shiny.rstudio.com/articles/|Articles]], [[http://rmarkdown.rstudio.com/|Markdown]] ||
|| IMATH Shiny Server || https://shiny.math.uzh.ch ||
|| Log Files || http://shiny.math.uzh.ch:8080/ (accessible only via intranet=thinlinc) ||

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.
Line 7: Line 13:
= URLs = = Turn your R script into a webpage =
Line 9: Line 15:
|| Product || http://shiny.rstudio.com ||
|| Documentation || [[http://shiny.rstudio.com/tutorial/|Getting Started]], [[http://shiny.rstudio.com/gallery/|Gallery]], [[http://shiny.rstudio.com/articles/|Articles]], [[http://rmarkdown.rstudio.com/|Markdown]] ||
|| IMATH Shiny Server || https://shiny.math.uzh.ch ||
 * Ask for a personal [[Homepage]] if you do not have one
 * In `~/public_html` create one folder for each shiny app and copy your R scripts there.
 ''Grant access to your files (only necessary if you created new files) by executing'' `setwww` ''in a linux console.''
 * URL to your app: `http://shiny.math.uzh.ch/user/<NAME>/<FOLDER>/` where
  * '''NAME''' is typically your lastname, lowercase
  * '''FOLDER''' is the subfolder in `public_html` with the scripts
 Example: http://shiny.math.uzh.ch/user/doe/shinyapp1/
Line 13: Line 23:
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. = Show the shiny app as a part of another page =
Line 15: Line 25:
= Presentation = Use an iframe to embed your shiny app on another website as follows:
{{{
<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>
}}}
With the style part of the command, you can customize the size and look of the frame:
 * '''border''': 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 ''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, use ''float: left'' on one and ''float: right'' on the other.
Line 17: Line 37:
== Via `~/public_html` ==

 * Ask for an personal [[Homepage]].
 * 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.
 
 * URL: http://shiny.math.uzh.ch/user/<LASTNAME>/<FOLDER>/
  * Example: http://shiny.math.uzh.ch/user/doe/shinyapp1/

=== iframe: 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>
}}}

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.

== Via gitlab deployment ==
= Deployment with gitlab-runner =
Line 49: Line 46:
{{{  {{{
Line 59: Line 56:

# Paket fehlt (Beispiel von SampleSizeR)
}}}
 * Examine the log file for errors, for example a missing or an outdated package (an example from SampleSizeR)
 {{{
Line 65: Line 63:

# Überprüfen ob r-cran paket im ubuntu repo existiert
$ apt install r-cran-ipc

# Sonst Install über R
$ su - -c "R -e \"install.packages('ipc')\""

# Bei anderen Errors sollte der Code überprüft werden

# Am Ende vom Debugging sollte das persistent logging wieder deaktiviert werden, da sonst sehr viele logs entstehen
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
...
Line 76: Line 67:
  * the missing package exists in Ubuntu repo then it can be intalled directly
  {{{
$ 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
}}}
  Otherwise it must be installed from within R:
  {{{
$ sudo "R -e \"install.packages('vctrs')\""
}}}
  * confirm the current version
  {{{
$ R -q -s -e "packageVersion('vctrs')"
[1] ‘0.6.4’
}}}
 * In case of other errors please check the R code
'''Important''': persitent logs create huge log files - deactivate it when finished!

Shiny

Product

http://shiny.rstudio.com

Documentation

Getting Started, Gallery, Articles, Markdown

IMATH Shiny Server

https://shiny.math.uzh.ch

Log Files

http://shiny.math.uzh.ch:8080/ (accessible only via intranet=thinlinc)

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.

Turn your R script into a webpage

  • Ask for a personal Homepage if you do not have one

  • In ~/public_html create one folder for each shiny app and copy your R scripts there. Grant access to your files (only necessary if you created new files) by executing setwww in a linux console.

  • URL to your app: http://shiny.math.uzh.ch/user/<NAME>/<FOLDER>/ where

    • NAME is typically your lastname, lowercase

    • FOLDER is the subfolder in public_html with the scripts

    Example: http://shiny.math.uzh.ch/user/doe/shinyapp1/

Show the shiny app as a part of another page

Use an iframe to embed your shiny app on another website as follows:

<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> 

With the style part of the command, you can customize the size and look of the frame:

  • border: 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 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, use float: left on one and float: right on the other.

Deployment with gitlab-runner

  • Example deployment: https://git.math.uzh.ch/bbaer/publish-shiny example .gitlab-ci.yml file.

  • A commit to the GIT Repo will trigger the deployment described by .gitlab-ci.yml (via a gitlab-runner on alfred16, alfred20 or alfred22)

Debugging Apps

  • Ask IT Support for sudo rights on the shiny server.
  • Activate persistent logs:

    [root@shiny22]
    
    # Option einfügen unter zeile 'run_as shiny;'
    $ vim /etc/shiny-server/shiny-server.conf
    ...
    run_as shiny;
    
    preserve_logs true;
    ...
  • Examine the log file for errors, for example a missing or an outdated package (an example from SampleSizeR)
    $ 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
    ...
    • the missing package exists in Ubuntu repo then it can be intalled directly
      $ 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
      Otherwise it must be installed from within R:
      $ sudo "R -e \"install.packages('vctrs')\""
    • confirm the current version
      $ R -q -s -e "packageVersion('vctrs')"
      [1] ‘0.6.4’
  • In case of other errors please check the R code

Important: persitent logs create huge log files - deactivate it when finished!

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 2024-03-27 12:56:44 by crose)