Introduction

Dans cet exercice, nous décvouvrons progressivement les différents éléments pour construire une interface R Shiny, en partant du plus simple et en élaborant progressivement.

A chaque étape, on ajoute un élément d’interactivité.

Les solutions sont fournies dans des dossiers séparés;

  • app1/app.R
  • app2/app.R
  • app3/app.R

Pour les exécuter, il suffit d’ouvrir le fichier app[#]/app.R dans RStudio et de cliquer le biouton Run App qui apparaît dans le coin supérieur droit de l’éditeur de code.

Attention, le nom du script R app.R est important pour faciliter le déploiement de l’application sur un serveur Shiny.


2. Bin numbers

Change the application app1 to allow the user to enter the numbers of bins when plotting the histogram

Solution: app2/app.R


3. Server response

Change the application app1 to add some text as a response from server.

Solution: app3/app.R


4. Mean and standard deviation

Change app1 so that

  • The user can enter different values for the mean and standard deviation.
  • The mean and standard deviation are shown in the mainPanel.
  • The user can enter a title for the histogram.

Solution: app4/app.R


5. Alternative displays

Change app1 so that the user can choose to do a histogram or a boxplot

Solution: app5/app.R


6. Do it the way you want it

Just about anything can be made to look the way you want it.

  • change the size of the sidebar: (,width=3)
  • Change the aspect ratio of the graph:

Solution: app6/app.R


7. Panel appears and disappears:

An ugly feature of our app: the input field Number of bins only makes sense for the histogram, not for the boxplot, so it should not appear when we do a boxplot.

Hint: conditionalPanel()

Solution: app7/app.R


8. Predefined data sets

Let us read files on the server files. For this purpose, first we need to save the data sets in the same folder as ui.R and server.R, say with dump. Then we can read the data in the server with

Your turn: Read the 3 data sets available on github

Solution: app8/app.R


10. Summary statistics

In the text area we want a table of summary statistics. The idea here is to use R syntax to create a character vector which has the lines of the HTML code.

Alternatives:

  1. Générer une table R et ensuite générer du code HTML avec kable
  2. Utiliser renderDataTable(). C’est l’exercice suivant.

11. Table presentations

These tables rarely look very good. To change their appearance we need to use cascading style files. The easiest way is to include that in the ui.R.

Les fonctions d’output renderDataTable() et `dataTableOutput() permettent d’afficher une table.

Note: ceci fait appel au package dt qui gère les tableaux de données.


12. Several panels

Often it is a good idea to have several panels to show different things. Say we want to separate the text from the graph.


13. Graph displays

Again there are items on the left that only make sense for the graphs, so they should only appear when the Graph panel is selected. Again conditionalPanel to the rescue!


14. Animation

When generating random data we might want to do this a number of times. Slowly, so one can watch the changes.

On ui side use :

sliderInput("k","Repeat!",min=1, max=10, value=0,step=1,
                animate=animationOptions(interval = 500,playButton="Go!")
         )

on server side use:


15. Use the gglot2 library

Now let us draw the graphs with ggplot2. For this purpose, you must add require(ggplot2) on server side and change the render plot function call for histogram and boxplot.