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())
Select the movie genre to display
## The shiny part selectInput("movie_type", label = "Movie genre", c("Romance", "Action", "Animation"))
## 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] })
DT::renderDataTable({ t_m = top_movies[top_movies[input$movie_type]==1,] DT::datatable(t_m[,1:4], rownames = FALSE) })
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() })
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() })
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) })
renderValueBox({ an = movies[movies[input$movie_type]==1,] valueBox(sum(an$rating <3), icon = "ion-videocamera", caption="Movies Rated less than 3", color="red") })
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") })
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") })
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.