This project is maintained by sarbal
Let’s make something interactive! Start off by installing the shiny package:
install.packages("shiny")
And then downloading this file:
ui
and server
ui
) defines the structure of the pageui <- fluidPage(
titlePanel("This is the title"),
# Inputs
sidebarLayout(
sidebarPanel(
textInput("one", "Number1"),
textInput("two", "Number2"),
textInput("caption", "Text1"),
actionButton("add", "Add"),
checkboxInput(inputId = "obs", label = strong("Show individual observations"), value = FALSE),
sliderInput("bins", "Number of bins:", min = 1, max = 10, value = 5)
),
# Result display
mainPanel(
textOutput("check"),
plotOutput("distPlot"),
tableOutput("table")
)
)
)
server
), where all the action happensoutput
that contains all of the code needed to update the R objects in your appoutput$check
in server
matches textOutput("check")
in ui
server <- function(input,output,session) {
# Reactive input
observeEvent( input$add,{
x <- as.numeric(input$one)
y <- as.numeric(input$two)
title <- as.character(input$caption)
# Reactive expressions
n <- x+y
d <- round(abs(rnorm(n) ) * n )
m <- mean(d)
sdd <- sd(d)
bins <- seq(min(d), max(d), length.out = input$bins + 1)
# Reactive output
output$check <- renderPrint("Done!")
output$distPlot <- renderPlot({
# Draw the histogram with the specified number of bins
hist(d, breaks = bins, freq=F, col = 'darkgray', border = 'white', main=title)
ld <- density(d)
lines(ld, col=4)
abline(v=m, lty=2, lwd=3, col=4)
if (input$obs) { rug(d) }
})
output$table <- renderTable({
data = cbind(n,m, sdd)
return( data )
})
})
)
shinyApp(ui = ui, server = server)
function | widget |
---|---|
actionButton | Action Button |
checkboxGroupInput | A group of check boxes |
checkboxInput | A single check box |
dateInput | A calendar to aid date selection |
dateRangeInput | A pair of calendars for selecting a date range |
fileInput | A file upload control wizard |
helpText | Help text that can be added to an input form |
numericInput | A field to enter numbers |
radioButtons | A set of radio buttons |
selectInput | A box with choices to select from |
sliderInput | A slider bar |
submitButton | A submit button |
textInput | A field to enter text |
Output function | Creates |
---|---|
dataTableOutput | DataTable |
htmlOutput | raw HTML |
imageOutput | image |
plotOutput | plot |
ableOutput | table |
textOutput | text |
uiOutput | raw HTML |
verbatimTextOutput | text |
render function | Creates |
---|---|
renderDataTable | DataTable |
renderImage | images (saved as a link to a source file) |
renderPlot | plots |
renderPrint | any printed output |
renderTable | data frame, matrix, other table like structures |
renderText | character strings |
renderUI | a Shiny tag object or HTML |
install.packages("shiny")
library(shiny)
runApp("my_app")
We can modify the different bits of code in the ui and server functions to change the display and functionality of the code.
Back to the homepage