This section provides an overview of what shiny is, and why a developer might want to use it.
It should also mention any large subjects within shiny, and link out to the related topics. Since the Documentation for shiny is new, you may need to create initial versions of those related topics.
The simplest way to include plots in your shinyApp is to use plotOutput
in the ui and renderPlot
in the server. This will work with base graphics as well as ggPlot
s
library(shiny)
library(ggplot2)
ui <- fluidPage(
plotOutput('myPlot'),
plotOutput('myGgPlot')
)
server <- function(input, output, session){
output$myPlot = renderPlot({
hist(rnorm(1000))
})
output$myGgPlot <- renderPlot({
ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) + geom_point()
})
}
shinyApp(ui, server)
Tables are most easily included with the DT package, which is an R interface to the JavaScript library DataTables.
library(shiny)
library(DT)
ui <- fluidPage(
dataTableOutput('myTable')
)
server <- function(input, output, session){
output$myTable <- renderDataTable({
datatable(iris)
})
}
shinyApp(ui, server)
Shiny can run as a standalone application on your local computer, on a server that can provide shiny apps to multiple users (using shiny server), or on shinyapps.io.
install.packages("shiny")
if installing from CRAN, or devtools::install_github("rstudio/shiny")
if installing from the RStudio Github repository. The Github repository hosts a development version of Shiny which can possibly have more features when compared to the CRAN version, but it may also be unstable.Each shiny
app contains two parts: A user interface definition (UI
) and a server script (server
). This example shows how you can print "Hello world" from UI or from server.
UI.R
In the UI you can place some view objects (div, inputs, buttons, etc).
library(shiny)
# Define UI for application print "Hello world"
shinyUI(
# Create bootstrap page
fluidPage(
# Paragraph "Hello world"
p("Hello world"),
# Create button to print "Hello world" from server
actionButton(inputId = "Print_Hello", label = "Print_Hello World"),
# Create position for server side text
textOutput("Server_Hello")
)
)
Server.R
In the server script you can define methods which manipulate data or listen to actions.
# Define server logic required to print "Hello World" when button is clicked
shinyServer(function(input, output) {
# Create action when actionButton is clicked
observeEvent(input$Print_Hello,{
# Change text of Server_Hello
output$Server_Hello = renderText("Hello world from server side")
})
})
How to run?
You can run your app in several ways:
runApp('your dir path')
shinyApp(ui,server)
to run your appResult
In this example you will see some text and a button:
And after button click the server responds: