<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/mapping-medical-cannabis-dispensaries-from-pdf-table-to-google-map-with-r/"></div></div><div><a data-url="https://www.r-bloggers.com/mapping-medical-cannabis-dispensaries-from-pdf-table-to-google-map-with-r/" data-counturl="https://www.r-bloggers.com/mapping-medical-cannabis-dispensaries-from-pdf-table-to-google-map-with-r/" data-count="horizontal" data-text="Mapping medical cannabis dispensaries: from PDF table to Google Map with R" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://blog.revolutionanalytics.com/2016/08/dispensaries.html"> Revolutions</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div>
<p>by Sheri Gilley, Microsoft Senior Software Engineer</p>
<p>In 2014, Illinois passed into law the creation of a medical cannabis pilot program. As my son has cancer and marijuana could greatly help with some of his symptoms, we eagerly applied for a card when registration was available early in 2015. The first dispensaries were not available until November 2015. At that time there were 9 dispensaries; the PDF file with a table of dispensary names and locations provided by the Illinois Department of Health was an adequate way to find a dispensary.</p>
<p>In the time that dispensaries have been available, my son has been in various hospitals and facilities in and around the city of Chicago.  First we were in Park Ridge, then Hyde Park, then Hinsdale, then downtown Chicago and now finally back home in Oak Park.  As we moved around the city, I would use that same PDF file to locate the dispensary closest to me. The list has grown from 9 names and addresses to 40 today. With 40 entries, the PDF table format is not at all useful for showing where the dispensaries are located.  The entries are listed in the order of the license issue date, making it all the more difficult to see which dispensaries might be easiest for me to visit.</p>
<p>So one weekend I decided to create a map of all the current locations.  Keeping in mind that more dispensaries will be available in the future, I wanted to create code that would read the <a title="IDPH Cannabis Registered Dispensaries" target="_blank" href="http://www.idfpr.com/Forms/MC/ListofLicensedDispensaries.pdf">official list of registered dispensaries</a>, so that updates would be easy as more entries were added.</p>
<p>I knew I could read the text of the file in R using <a target="_blank" href="https://mran.microsoft.com/package/pdftools/">pdftools</a>, and could put the locations onto a google map using <a target="_blank" href="https://mran.microsoft.com/package/googleVis/">googleVis</a>.  The hardest part of the code was trying to filter out the noise included in the text and reliably get the name, address, and phone number of each dispensary into a data frame. A few handy <a target="_blank" href="http://www.rdocumentation.org/packages/base/versions/3.3.1/topics/grep">gsub</a> statements worked their magic and I was left with data ready for mapping.</p>
<p>I added in some geocoding to get the longitude and latitude, thanks to <a title="goecoding tip" target="_blank" href="http://stackoverflow.com/questions/22887833/r-how-to-geocode-a-simple-address-using-data-science-toolbox">this tip</a>.</p>
<p>Finally, after the data manipulation, the code to produce the map itself is rather straightforward:</p>
<pre><code># create id  and LatLong for googleVis map
all$id &lt;- paste(all$name, all$address, all$phone, sep='; ')
all$LatLong = paste(result$lat, result$long, sep=&quot;:&quot;)</code></pre>
<p># Now plot with googleVis<br>
require(googleVis)</p>
<p>g1 &lt;- gvisMap(all, “LatLong” , “id”,<br>
options=list(showTip=TRUE,<br>
showLine=TRUE,<br>
enableScrollWheel=TRUE,<br>
mapType=’normal’,<br>
width=400, height=400<br>
))</p>
<p># this opens a browser with the plot<br>
plot(g1)</p>
<p># write the code that will be used on my website<br>
cat(g1$html$chart, file=”dispensariesIL.html”)</p>
<p>I thought the map might be useful for others in Illinois also interested in finding a dispensary, so I created a <a target="_blank" href="http://medicalcannabisil.weebly.com/find-a-dispensary.html">small website</a> for the map, and include a few other tips and tricks I have learned along the way by being the procurer of medicinal cannabis in my household.</p>
<p>Here is the resulting map, generated on August 12, 2016 (click to see the interactive version):</p>
<p><a target="_blank" href="http://blog.revolutionanalytics.com/2016/08/dispensaries.html"><img sizes="(max-width: 450px) 100vw, 450px" alt="map-2016-08-17" class="wp-image-138104" src="https://i0.wp.com/www.r-bloggers.com/wp-content/uploads/2016/08/map-2016-08-17.png?resize=450%2C354&ssl=1" data-recalc-dims="1"></a></p>
<p>You can create this map yourself with this <a target="_blank" href="https://gist.github.com/sdgilley/15ebf67c5b01d12224f4b103c7065625">R code</a>.</p>
<p></p>
</div>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="https://www.r-bloggers.com/mapping-medical-cannabis-dispensaries-from-pdf-table-to-google-map-with-r/"></div></div><div><a data-url="https://www.r-bloggers.com/mapping-medical-cannabis-dispensaries-from-pdf-table-to-google-map-with-r/" data-counturl="https://www.r-bloggers.com/mapping-medical-cannabis-dispensaries-from-pdf-table-to-google-map-with-r/" data-count="vertical" data-text="Mapping medical cannabis dispensaries: from PDF table to Google Map with R" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://blog.revolutionanalytics.com/2016/08/dispensaries.html"> Revolutions</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/dgXK9LNHZbA"></a></div>