R-Shiny trainings organized by IFB
ui <- fluidPage(
titlePanel("Workshop - Example 1 - Basic Histogram"),
sidebarLayout(
sidebarPanel(
numericInput(inputId="n",
label="Number of observations",
value=1000) ),
mainPanel(plotOutput(outputId="plot"))
)
)
server <- function(input, output) {
data <- reactive({
x <- rnorm(input$n)
x })
output$plot <- renderPlot({
hist(data(), breaks=50, main="", xlab="x")
})
}
shinyApp(ui = ui, server = server)
Bin numbers: Change the application to allow the user to enter the numbers of bins when plotting the histogram.
Add some text to the output: Change the application to add some text to the output. For example, indicate the number of observations and bins defined in parameters.
Choose mean and standard deviation: Change the application so that
Different displays: Change the application so that the user can choose to do a histogram or a boxplot.
Change the appearance of the widgets: you can customize the size of the bars and plots:
Panel appears and disappears:
An ugly feature of our application: 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: use the conditionalPanel()
function.
Access predefined data sets
a. Save the data sets in the same folder as app.R
github contains 3 datasets that you can use: euros, forbes, newcomb.
b. Let the user choose (using a drop down menu) to either generate the random data or use one of several datafiles.
c. We can read the data in the server with a command like
if(input$dataset=="Newcomb's Speed of Light") {
x <- read.csv("newcomb.csv")
return(x)
}
Dataset upload
a. Let the user upload its own dataset (a file containing one value per row, no headers) so that it is plotted by the app.
Hint: use the fileInput() function; note that this function does not return the file itself, but a structure containing metadata about the file, including a link that you can then use to open the file (have a look at the help page for more information).
b. Add an output widget that displays the content of the file as a table
Hint: use the tableOutput() widget.
Several tabs: Often it is a good idea to have several tabs to organize the output when showing different information. Say we want to separate the text, the graph and the table display
Hint: look at the tabsetPanel() function.
Use another library: let us draw the graphs with ggplot2 instead of base R. For this purpose, you must add require(ggplot2) on server side and change the render plot function call for histogram and boxplot.
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:
if(input$dataset=="Random") {
for(i in 1:input$k) mu<-input$mu
return(rnorm(input$n,input$mu,input$sig))
}
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.
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.
Download a file: let the user download a file produced by the application.
Use the plotly or D3 libraries
=========