Analytics with Jupyter Notebook
Out of all the tools in in my Analytics toolkit, I am particularly fond of the Jupyter notebook. It is a web application that allows you to combine your executable codes, visualization and documentation into a single document. Jupyter offers an advanced read-eval-print loop (REPL) environment and supports multiple languages (R, Python, Scala).
If you use OneNote or Evernote regularly, chances are you appreciate the convenience of having all your notes in the form of text, images, urls, tables, etc. in a single application and having access to the documents from multiple devices. Jupyter provides similar functionality and convenience and you can save your scripts, charts and documents in a single notebook file. You can then circulate this file within your team or even spin up a notebook server to host the notebook files.
Although Jupyter has gotten a lot of attention in the data science community for supporting reproducible research, it also serves as a great documentation tool. I’ve found it to be a great resource for developing prototypes quickly and socializing the results to the rest of the team. Since most of the popular scientific languages have support for charting and plotting libraries, it can also be used as a lightweight visualization tool.
Here are some examples that illustrate how you can mix R scripts, comments and charts in a notebook:
# Load libraries library(leaflet) library(tidyr) library(htmlwidgets) aspirent_icon <- makeIcon( iconUrl = "black_orage_HD2.png", iconWidth = 20, iconHeight = 10, iconAnchorX = 0, iconAnchorY = 0, ) #popup text popuptext<-'<p><img src= "black_orange_HD2.png" height="40" width="80"/><br /> 3033 Maple Drive Northeast <br/>Atlanta, GA 30305</p>' # create plot objects plot <- leaflet() %>% addProviderTiles("Esri.WorldStreetMap") %>% addPopups(lng =-84.371104,lat=33.837764,popup = popuptext,) #save plot htmlwidgets::saveWidget(widget = plot,file = "plot.html") url<-"<iframe width = 500p height = 300p src='plot.html'></iframe>" # display map IRdisplay::display_html(url)
library(leaflet) library(tidyr) library(htmlwidgets) library(blscrapeR) library(tigris) # Load BLS Data by state for June 2016. We are interesed in the unemployment rates. df<-blscrapeR::get_bls_state("June 2016") # Load shape data, this is useful if you get the statelevel data from a different source. bls states <- tigris::states(cb=T) # Merge the state level date with the shape file merged_dt<- tigris::geo_join(spatial_data = states,data_frame = df,by_sp = "NAME",by_df = "state" ) # sets up the color scheme of the chart pal <- colorNumeric(palette = "YlOrRd", domain = merged_dt$unemployed_rate) # create the plot plot1<-merged_dt%>%leaflet()%>% #addProviderTiles("CartoDB.Positron")%>%#addTiles()%>% fitBounds(lng1 = -124.848974 ,lat1 = 24.396308,lng2 = -66.885444,lat2 =49.384358)%>% addPolygons(fillColor = ~pal(unemployed_rate), fillOpacity = 0.8,, weight = 1, popup = ~paste0(as.character(STUSPS),':',as.character(unemployed_rate)))%>% addLegend("bottomright", pal = pal, values = ~unemployed_rate, title = "Unemployed Rate", opacity = 1) # save plot in plot1.html htmlwidgets::saveWidget(widget = plot1,file = "plot1.html",selfcontained = T) # display plot link<-"<iframe width = 750p height = 500p src='plot1.html'></iframe>" IRdisplay::display_html(link)