--- title: "Stat. 651 Interactive Data graphics" --- Examples of using R packages that depend on JavaScript and D3. We will be testing out a number of [htmlwidgets](http://www.htmlwidgets.org/index.html) # Datatable Check out the [DT](http://www.htmlwidgets.org/showcase_datatables.html). It makes very nice interactive tables that include the ability to search for rows of data. ```{r} library(DT) datatable(mtcars) ``` ```{r} library(nycflights13) datatable(planes) ``` Leaflet The [leaflet](http://rstudio.github.io/leaflet/) package is very useful for making maps. ```{r} library(leaflet) m <- leaflet() %>% addTiles() %>% # Add default OpenStreetMap map tiles addMarkers(lng=174.768, lat=-36.852, popup="The birthplace of R") m # Print the map ``` Lets find our current location using [latlong.net](https://www.latlong.net/) and make a map. So now ```{r} m_csueb <- leaflet() %>% addTiles() %>% # Add default OpenStreetMap map tiles addMarkers(lng=-122.053765, lat=37.656057, popup="Stat. 651 on the CSU East Bay campus.") m_csueb # Print the map ``` # Plot.ly ```{r} library(tidyverse) library(plotly) library(mdsr) library(babynames) Beatles <- babynames %>% filter(name %in% c("John", "Paul", "George", "Ringo") & sex == "M") %>% mutate(name = factor(name, levels = c("John", "George", "Paul", "Ringo"))) datatable(Beatles) beatles_plot <- Beatles %>% ggplot(aes(x = year, y = n)) + geom_line(aes(color = name), size = 2) beatles_plot getwd() write_csv(Beatles, file.path("Beatles.csv")) ``` ```{r} ggplotly(beatles_plot) ``` # Dygraphs ```{r} library(dygraphs) Beatles %>% filter(sex == "M") %>% select(year, name, prop) %>% pivot_wider(names_from = name, values_from = prop) %>% dygraph(main = "Popularity of Beatles names over time") %>% dyRangeSelector(dateWindow = c("1940", "1980")) ``` # Streamgraphs The [streamgraph](https://hrbrmstr.github.io/streamgraph/) package is not available on CRAN. It needs to be installed using devtools. To install uncomment the first two lines of the code chunk below. When asked to update other packages, select No. ```{r} # library(devtools) # install_github("hrbrmstr/streamgraph") library(streamgraph) library(RColorBrewer) Beatles %>% streamgraph(key = "name", value = "n", date = "year") %>% sg_fill_brewer("Accent") ``` ```{r} library(stringr) babynames %>% filter(str_detect(name, "^Kr")) %>% group_by(year, name) %>% tally(wt=n) %>% streamgraph("name", "n", "year") ``` ```{r} babynames %>% filter(str_detect(name, "^I")) %>% group_by(year, name) %>% tally(wt=n) %>% streamgraph("name", "n", "year", offset="zero", interpolate="linear") %>% sg_legend(show=TRUE, label="I- names: ") ``` # gganimate ```{r} #| eval: false library(gganimate) library(transformr) beatles_animation <- beatles_plot + transition_states( name, transition_length = 2, state_length = 1 ) + enter_grow() + exit_shrink() animate(beatles_animation, height = 400, width = 800) ``` # Flexdashboard Check out the [flexdashboard](https://rmarkdown.rstudio.com/flexdashboard/) website. To create a new flex dashboard we will install the package and then load an R Markdown Template. ```{r} library(flexdashboard) ``` # Shiny documents Check out [Shiny documents](https://bookdown.org/yihui/rmarkdown/shiny-documents.html) Chapter in the [RMarkdown book](https://bookdown.org/yihui/rmarkdown/). To create a new Shiny document we will load an R Markdown Template. # Shiny App packages 1) [seasonalview](http://www.seasonal.website/), this link runs it online. You can install the package and run it locally. ```{r} #| eval: false #| library(seasonalview) help(seasonalview) m <- seas(AirPassengers) view(m) # standalone() ```