Professional Documents
Culture Documents
This lesson
will get you started building Shiny apps right away.
If you still havent installed the Shiny package, open an R session, connect to the internet, and run
> install.packages("shiny")
a server script
The user-interface (ui) script controls the layout and appearance of your app. It is defined in a source script
named ui.R. Here is the ui.R script for the Hello Shiny example.
ui.R
library(shiny)
shinyUI(fluidPage(
# Application title
titlePanel("Hello Shiny!"),
sidebarLayout(
sidebarPanel(
sliderInput("bins",
"Number of bins:",
min = 1,
max = 50,
value = 30)
),
mainPanel(
plotOutput("distPlot")
)
))
The server.R script contains the instructions that your computer needs to build your app. Here is the server.R script
for the Hello Shiny example.
server.R
library(shiny)
shinyServer(function(input, output) {
})
})
At one level, the Hello Shiny server.R script is very simple. The script does some calculations and then plots a
histogram with the requested number of bins.
However, youll also notice that most of the script is wrapped in a call to renderPlot. The comment above the
function explains a bit about this, but if you find it confusing, dont worry. Well cover this concept in much more
detail soon.
Play with the Hello Shiny app and review the source code. Try to develop a feel for how the app works.
Your R session will be busy while the Hello Shiny app is active, so you will not be able to run any R commands. R is
monitoring the app and executing the apps reactions. To get your R session back, hit escape or click the stop sign
icon (found in the upper right corner of the RStudio console panel).
Installation:
To install the stable version from CRAN, simply run the following from an R console:
install.packages("shiny")
To install the latest development builds directly from GitHub, run this instead
if (!require("devtools"))
install.packages("devtools")
devtools::install_github("rstudio/shiny")
Shiny versions 0.10.2.2 and below used the Bootstrap 2 web framework. After 0.10.2.2, Shiny switched
to Bootstrap 3. For most users, the upgrade should be seamless. However, if you have have
customized your HTML-generating code to use features specific to Bootstrap 2, you may need to
update your code to work with Bootstrap 3.
If you do not wish to update your code at this time, you can use the shinybootstrap2 package for
backward compatibility.
If you prefer to install an older version of Shiny, you can do it using the devtools package: