library("shiny")
library("flexdashboard")
library("ggplot2")


## Subset of the ggplot2movies data set
data(movies, package="jrShiny")
top_movies = movies[order(-movies$rating), 
                    c("title", "year", "budget", "rating", "Romance", "Action", "Animation"), ]
colnames(top_movies) = c("Title", "Year", "Budget", "Rating","Romance", "Action", "Animation")
theme_set(theme_bw())

Selections {.sidebar}

Select the movie genre to display

## The shiny part
selectInput("movie_type", label = "Movie genre", 
            c("Romance", "Action", "Animation"))

Tables

Column {.tabset .tabset-fade}

Top 10 movies (static table)

## Exercise for the reader!: 
## t_m is calculated twice, bad....
## Use reactive to avoid duplication
renderTable({
  t_m = top_movies[top_movies[input$movie_type]==1,]
  t_m[1:10, 1:4]
})

Top 10 movies (dynamic table)

DT::renderDataTable({
  t_m = top_movies[top_movies[input$movie_type]==1,]
  DT::datatable(t_m[,1:4], rownames = FALSE)
})

Base graphics

Rating vs Length

renderPlot({
  an = movies[movies[input$movie_type]==1,]
  jrShiny::setnicepar() 
  plot(an$rating, an$length, ylab="Length", xlab="Rating", 
       pch=21, bg="steelblue", ylim=c(0, max(an$length)), 
       xlim=c(1, 10), main=paste0(input$movie_type, " movies"))
  grid()
})

Movie Length

renderPlot({
  an = movies[movies[input$movie_type]==1,]
  jrShiny::setnicepar()
  hist(an$length, breaks="fd", col="steelblue", xlab="Movie Length", 
       main="Histogram of movie length", xlim=c(0, max(an$length)))
  grid()
})

htmlwidget and value boxes

Column 1 {data-width=200}

Length vs rating

This example makes use of plotly and ggplot2. There is also a valuebox showing the number of terrible movies.

library(plotly)
renderPlotly({
  an = movies[movies[input$movie_type]==1,]
  g = ggplot(an, aes(length, rating)) + 
    geom_point(size=0.5, aes(text=paste("Film: ", title))) + 
    xlab("Length") + ylab("Rating") + 
    ylim(c(0, 10)) 
  ggplotly(g)
})

Value boxes

renderValueBox({
  an = movies[movies[input$movie_type]==1,]
  valueBox(sum(an$rating <3), icon = "ion-videocamera", 
           caption="Movies Rated less than 3", color="red")
})

Column 2 {data-width=300}

Movie ratings over number

This example makes use of the dygraphs R package. The dygraphs package provides rich facilities for charting time-series data in R.

library(dygraphs)
renderDygraph({
  years = movies[movies$year > 1929 & movies[input$movie_type]==1,]
  rat_by_year = tapply(years$rating, years$year, mean)

  x = ts(as.vector(rat_by_year), start=1930)
  y = cbind(Rating=x)
  dygraph(y, main = "Ratings over the years", 
          ylab = "Ratings", group="Ratings") %>% 
    dyRangeSelector() %>%
    dyOptions(stepPlot = TRUE) %>%
    dySeries("V1", label = "Rating")
})

Number of movies made

library(dygraphs)
renderDygraph({
  years = movies[movies$year > 1929 & movies[input$movie_type]==1,]
  num_by_year = tapply(years$rating, years$year, sum)
  ## Create ts object
  x = ts(as.vector(num_by_year), start=1930)
  y = cbind(Rating=x)

  ## Plot code
  dygraph(y, main = "Number of movies over the years", 
          ylab = "No. of movies",group="Ratings") %>% 
    dyRangeSelector() %>%
    dyOptions(stepPlot = TRUE) %>%
    dySeries("V1", label = "No. of Movies made")
})


jr-packages/jrShiny documentation built on Feb. 16, 2020, 9:13 p.m.