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.
Copy, debug and run the first application (app1) presented today.
ui <- fluidPage(
titlePanel("Workshop - Example 1 – Basic Histogram"),
sidebarLayout(
sidebarPanel(
numericInput(inputId="n",
label="Number of observations",
value=1000) ),
mainPanel(plotOutput("plot"))
)
)
server <- function(input, output) {
data <- reactive({
x <- rnorm(input$n)
x })
output$plot <- renderPlot({
hist(data(), 50, main="", xlab="x")
})
}
shinyApp(ui = ui, server = server)
Solution: app1/app1.R
Change the application app1 to allow the user to enter the numbers of bins when plotting the histogram
Solution: app2/app.R
Change the application app1 to add some text as a response from server.
Solution: app3/app.R
Change app1 so that
Solution: app4/app.R
Change app1 so that the user can choose to do a histogram or a boxplot
Solution: app5/app.R
Just about anything can be made to look the way you want it.
Solution: app6/app.R
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
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
Let the user upload a dataset(a file containing one value per row, no headers) for display.
... (manque le début)
server <- function(input, output) {
data <- reactive({
inFile <- input$file
if (is.null(inFile)) {
x <- rnorm(input$n)
} else {
x <- read.csv(inFile$datapath)[,1]
}
return(x)
})
}
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.
output$text <- renderText({
x <- data()
line <- "<table border=1>"
line[2] <- "<tr><th>Sample Size</th>"
...
line[4] <- "</table>"
line
})
Alternatives:
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.
Often it is a good idea to have several panels to show different things. Say we want to separate the text from the graph.
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!
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:
if (input$dataset == "Random") {
for (i in 1:input$k) {
mu <- input$mu
}
return(rnorm(input$n,input$mu,input$sig))
}
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.