This is a study note for using \(shiny\) package for user interface. For more details on the study material see:
# essential
library(shiny)
\(shiny\) is built on the idea of reactive programming that outputs automatically update whenever an input value changes. Reactive expressions keep track of what values they read and what values they change. If those values become “out of date”, they know their return value is out of date and will automatically recalculate. Reactivity automaticaly occurs whenever you use an input value to render an output object:
\[UI: XXXInput(inputId) \Rightarrow Server: renderXXX(\{R\ code\}) \Rightarrow UI: XXXOutput(ouputId)\]
Shiny apps let users interact with data and analysis. And do it all with R. It also can be used to host standalone apps on a webpage or embed them in R Markdown documents or build dashboards. \(Shiny\) is an R package that makes it easy to build interactive web apps straight from R.
ui <- fluidPage()
: Create a page with fluid layout containing the widget and the output object.server <- function(input, output){}
: A server function using reactive expressions to keep track of any input values.shinyApp(ui, server)
: render the shiny app objectlibrary(shiny)
ui <- fluidPage(
# XXXInput(inputId_1)
# XXXOutput(outputId_1)
)
server <- function(input, output) {
output$outputId_1 <- renderXXX({
# R code chunk for the object type XXX depended on some inputId
# Fun_1(input$inputId_1)
})
}
shinyApp(ui, server)
library(shiny)
# additional pkg
# steady data
ui <- fluidPage(
navbarPage("ggfun",
# tap 1
tabPanel("inputId_1",
sidebarLayout(
sidebarPanel(
h3("header"),
#XXXInput(inputId_1)
),
mainPanel(
plotOutput("outputId_1"),
height="auto"
)
))
# tap 2
tabPanel("inputId_2",
sidebarLayout(
sidebarPanel(
h3("header"),
XXXInput(inputId_2)
),
mainPanel(
plotOutput("outputId_2"),
height="auto"
)
))
)
)
# Define server logic required to draw a histogram
server <- function(input, output,session) {
output$outputId_1 <- renderPlot({
# R code chunk for the object type XXX depended on some inputId
# Fun_1(input$inputId_1)
},
height = function() {
session$clientData$outputId_1 * 2/3
})
output$outputId_2 <- renderPlot({
# R code chunk for the object type XXX depended on some inputId
# Fun_2(input$inputId_2)
},
height = function() {
session$clientData$outputId_2 * 2/3
})
}
# Run the application
shinyApp(ui = ui, server = server)
Add elements to the shiny app as argument to:
ui <- fluidPage(
# XXXInput(inputId_1)
# XXXOutput(outputId_1)
)
The elements of the user interface are called widgets. Input widgets have a similar structure:
xxxInput (inputId, label, value)
where:
xxx
: is the name of the widgetinputId
: is the (unique) name you give to the widget (object name)label
: the text that appears with the widget in interfacevalue
: all widgets have a value. Often time, it is the default of the input....
: other parameters are widget-specificThe option of Input function (Widgets) are:
actionButton()
and submitButtion()
: creates a clickable buttoncheckboxInput()
and checkboxGroupInput()
: check box/boxesdateInput()
: calendar to select a datedateRangeInput()
: select a range of datesfileInput()
: upload a filenumericInput()
: input a numeric valueradioButtons()
: select one or more itemssliderInput()
: slide along a range of valuestextInput()
: input a stringselectInput
: multiple select input boxReference: Shiny Widgets Gallery
xxxOutput(outputId)
where:
xxx
: the type of output object to displayoutputId
: the name to give to the output object...
: other parameters are output-specificThe option of Output function are:
htmlOutput()
, imageOutput()
, plotOutput()
, tableOutput()
, textOutput()
: creates what the names says.uiOutput()
: raw HTMLverbatimTextOutput()
: textsave object to display to output$
by building the object to display with renderXXX()
use the input values with input$
server <- function(input, output) {
output$outputId_1 <- renderXXX({
# R code chunk for the object type XXX depended on some inputId
Fun_1(input$inputId_1)
})
}
The option of render function are:
renderTable()
: output an HTML tablerenderDataTable()
: outputs an interactive, sortable data table, from a data frame, matrix, or other table-like structurerenderPlot()
: output an R plotrenderImage()
: print an image to the pagerenderText()
: output text from RrenderPrint()
: output text from print() in RrenderUI()
: output a custom part of the user interfacehtmlOutput()
: output html elements