library(shiny) data(movies, package = "jrYr")
Create a new R markdown document
File
-> New File
-> R markdown
```r
# R code
```
```r
# R code
```
```r
# R code
```
```r
# R code
```
We can use Shiny functionality within an Rmarkdown file
Just create an R markdown file with the header
\noindent---
title: "First app"
runtime: shiny
---
selectInput(inputId = "movie_type", # unique id label = "Movie genre", # Text for web choices = c("romance", "action", "thriller"), selected = "action")
sliderInput(inputId = "movie_rating", label = "Movie rating", min = 0, max = 10, value = 5)
numericInput(inputId = "movie_length", label = "Movie length", min = 1, max = 330, value = 100)
1) http://shiny.rstudio.com/gallery/widget-gallery.html
Function | Output type
---------|------------
renderPlot()
| R graphics output
renderPrint()
| printed output
renderTable()
| Data frame, matrix
renderText()
| Character vectors
renderText(input$movie_type)
{}
renderText({ type = movies[, input$movie_type] == 1 nrow(movies[type, ]) })
Or
renderPlot({ type = movies[, input$movie_type] == 1 hist(movies[type, ]$length) })
In the previous task we created a selectInput()
to let the user choose a movie classification
selectInput(inputId = "movie_type", label = "Movie classification", choices = c("U", "PG", "12A", "15", "18"), selected = "12A")
renderText()
, display the number of films within each classification
. For example, the number of "PG" films data(movies, package = "jrYr") pg_movies = movies[movies$classification == "PG", ] nrow(pg_movies)
This is inefficient
renderText({ type = movies[, input$movie_type] == 1 nrow(movies[type, ]) }) renderPlot({ type = movies[, input$movie_type] == 1 hist(movies[type, ]$length) })
reactiveValues()
rvs = reactiveValues(data = movies)
observe()
observe({ type = movies[, input$movie_type] == 1 rvs$data = movies[type, ] })
rvs = reactiveValues(data = movies) observe({ type = movies[, input$movie_type] == 1 rvs$data = movies[type, ] }) renderText({ nrow(rvs$data) }) renderPlot({ hist(rvs$data$length) })
We may not want the app to update every time we change an input
observeEvent()
, actionButton()
rvs = reactiveValues(data = movies) actionButton("plot_button", "Plot it now!!!") observeEvent(input$plot, { type = movies[, input$movie_type] == 1 rvs$data = movies[type, ] }) renderPlot({ hist(rvs$data$length) })
library("ggplot2") data(movies, package = "jrYr") g = ggplot(movies, aes(x = length, y = rating)) + geom_point() g
library("plotly") ggplotly(g)
library("ggplot2") library("plotly") data(movies, package = "jrYr") selectInput(inputId = "movie_type", label = "Movie genre", choices = c("romance", "action", "thriller")) rvs = reactiveValues(data = movies) observe({ type = movies[, input$movie_type] == 1 rvs$data = movies[type, ] }) renderPlotly({ ggplotly(ggplot(rvs$data, aes(x = length)) + geom_histogram()) })
library("DT") datatable(movies)
library("ggplot2") library("plotly") library("DT") data(movies, package = "jrYr") selectInput(inputId = "movie_type", label = "Movie genre", choices = c("romance", "action", "thriller")) rvs = reactiveValues(data = movies) observeEvent(input$movie_type, { rvs$data = movies[movies[, input$movie_type] == 1, ] }) renderDataTable({ datatable(rvs$data[, 1:5]) })
ui.R
filelibrary("shiny") fluidPage( titlePanel("Shiny happy people"), #title ## Sidebar with a slider input for no. of points sidebarLayout( sidebarPanel( selectInput(inputId = "movie_type", # unique id label = "Movie genre", # Text for web choices = c("romance", "action", "thriller"), selected = "action") ), ## Show a plot of the generated distribution mainPanel(plotOutput("scatter")) ) )
server.R
filelibrary("shiny") data(movies, package = "jrYr") # Function always has input & output function(input, output) { rvs = reactiveValues(data = movies) observeEvent(input$movie_type, { rvs$data = movies[movies[, input$movie_type] == 1, ] }) output$scatter = renderPlot({ plot(x = rvs$data$duration, y = rvs$data$rating) }) }
Output function | creates
----------------|---------
htmlOutput
| raw HTML
imageOutput
| image
plotOutput
| plot
tableOutput
| table
textOutput
| text
uiOutput
| raw HTML
verbatimTextOutput
| text
fluidPage
functionfluidPage( titlePanel("Title panel"), # Title ## Sidebar style sidebarLayout( sidebarPanel("The sidebar"), mainPanel("Main panel") ) )
sidebarLayout
: swap sidessidebarLayout(position = "right", sidebarPanel("The sidebar"), mainPanel("Main panel") )
sidebarLayout
sidebarLayout( sidebarPanel("The sidebar", p("Choose an option")), mainPanel("Main panel") )
ui = fluidPage( titlePanel("I love movies"), #Title fluidRow(# Define a row column(4, # Two columns: width 4 & 8 wellPanel( selectInput("movie_type", label = "Movie genre", c("Romance", "Action", "Animation")) ) ), column(8, plotOutput("scatter")) ) )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.