<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Freddie Tasker on Medium]]></title>
        <description><![CDATA[Stories by Freddie Tasker on Medium]]></description>
        <link>https://medium.com/@fredtasker?source=rss-e171f5380924------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*DCNOLjrTLUkT-60s.jpg</url>
            <title>Stories by Freddie Tasker on Medium</title>
            <link>https://medium.com/@fredtasker?source=rss-e171f5380924------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 05:31:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@fredtasker/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Automating Running Routes in R]]></title>
            <link>https://medium.com/data-science/automating-running-routes-in-r-1e97eb4ed716?source=rss-e171f5380924------2</link>
            <guid isPermaLink="false">https://medium.com/p/1e97eb4ed716</guid>
            <category><![CDATA[hands-on-tutorials]]></category>
            <category><![CDATA[r]]></category>
            <category><![CDATA[towards-data-science]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[api]]></category>
            <dc:creator><![CDATA[Freddie Tasker]]></dc:creator>
            <pubDate>Tue, 06 Oct 2020 10:36:13 GMT</pubDate>
            <atom:updated>2020-10-06T15:09:53.840Z</atom:updated>
            <content:encoded><![CDATA[<h4><a href="https://towardsdatascience.com/tagged/hands-on-tutorials">Hands-on Tutorials</a></h4><h4>Using the Graphhopper API (with help from rvest and PostcodesIO) to run down every Mews in London</h4><p>Winter has arrived in the UK and as everyone else gets their boilers hurriedly repaired, my primary concern is keeping weekly running mileage up while temperatures go the other way.</p><p>Exploring new bits of London seemed a good idea, and not having to think about what route to take seemed even better. So the plan is to run down every Mews in London and let R decide how to do it.</p><p>There’s snippets of code in this article, the rest is at <a href="https://github.com/fredwise/run_the_mews">fredwise/run_the_mews</a>.</p><h3>The Mews</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*IHNP9yYU4BEWVQ3q" /><figcaption>Photo by <a href="https://unsplash.com/@brunus?utm_source=medium&amp;utm_medium=referral">Bruno Martins</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Hidden behind some of London’s grand terraced houses are 386 original and surviving <a href="https://en.wikipedia.org/wiki/Mews">mews</a>. These little streets are a remnant of bona fide horse power, and while the stables and carriage houses are now mostly garages and ground floor kitchens, they still make good sight-seeing.</p><h4>Finding Them</h4><p><a href="https://everchangingmews.com/">Everchanging Mews</a> has done the leg work here and over the last 10 years they have visited over 1,200 mews, cataloguing them on their encyclopaedic website.</p><p>The list of original / surviving mews is split over 8 pages and each listing has a web page with further details. <a href="https://developers.google.com/web/tools/chrome-devtools">Chrome Dev Tools</a> finds the element that holds this web address and a bit of work with the R package <a href="https://github.com/tidyverse/rvest">rvest</a> lists them.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a02ff524a47df10555c2753eda7f7d4b/href">https://medium.com/media/a02ff524a47df10555c2753eda7f7d4b/href</a></iframe><h4>Geocoding Them</h4><p>Each of these web pages has a postcode and <a href="https://docs.ropensci.org/PostcodesioR/">PostcodesioR</a> will query <a href="http://postcodes.io/">postcodes.io</a> for the location data. A little bit of manual cleaning up is involved here where the postcode is dodgy or doesn’t exist in the <a href="http://postcodes.io/">postcodes.io </a>database (7 of the 386). After that, just run them through <a href="https://www.rdocumentation.org/packages/PostcodesioR/versions/0.1.1/topics/postcode_lookup">postcode_lookup</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0723dd3db5e81ed0b920e6d60b40553c/href">https://medium.com/media/0723dd3db5e81ed0b920e6d60b40553c/href</a></iframe><h4>Mapping Them</h4><p>R at its finest here, we just need to pass the longitudes and latitudes to the leaflet as a shape file.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dacdb01f3f171ebe3dff59a7612027c4/href">https://medium.com/media/dacdb01f3f171ebe3dff59a7612027c4/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/562/1*FTv_m-Nwf4FtJQ7FngLJ4Q.png" /><figcaption>The 386 original / surviving mews on a page</figcaption></figure><h4>Routing Them</h4><p>Hopefully the real value in all this comes from automating the route generation. The original problem statement was:</p><blockquote>Find the shortest total route, in routes of 20km or less, that covers all 386 properties. With each route starting at the same place.</blockquote><p><a href="https://www.graphhopper.com/">GraphHopper</a> is a brilliant free resource for this kind of route optimisation. It allows routing of all sorts of vehicles (in this case feet), but its free API only allows up to 5 locations per request.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/273/1*Bkl1zUHlO33piAmhSilG-A.png" /><figcaption>GraphHopper will even let you route a scooter.</figcaption></figure><p>A simple solution is to randomly select 5 locations at a time. This does come at the cost of not optimising over the total distance, however all that means in this case is that more running gets done and no mews gets visited twice (as long as the random sampling is done without replacement) so it should do!</p><h4>Using the GraphHopper API</h4><p>GraphHopper’s <a href="https://docs.graphhopper.com/">documentation</a> is great and all that’s required is passing the latitude and longitude of the 5 locations in the right format. The snippet below shows a function that builds this request as an R list and converts it to JSON using <a href="https://rdrr.io/cran/jsonlite/">jsonlite</a>. A reminder that full scripts can be found on <a href="https://github.com/fredwise/run_the_mews">Github</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/707aaaf530ed1f1a85d9938ad31cb409/href">https://medium.com/media/707aaaf530ed1f1a85d9938ad31cb409/href</a></iframe><p>The 78 queries for our 5 location running routes get posted to the GraphHopper API using httr::POST</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9ae03b370238beaf7a2a55d7fc40c646/href">https://medium.com/media/9ae03b370238beaf7a2a55d7fc40c646/href</a></iframe><p>Convert the response paths co-ordinates into simple features using the <a href="https://r-spatial.github.io/sf/">sf package</a> and they can be plotted as PolyLines in <a href="https://rstudio.github.io/leaflet/">leaflet</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0badc837d2e9da805877e63d1fa3f5aa/href">https://medium.com/media/0badc837d2e9da805877e63d1fa3f5aa/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/945/1*stYRGI9e9bFcEZVxIO70kg.png" /><figcaption>Graphopper does the hard work of optimising the routes between our sets of five mews</figcaption></figure><h4>LineString to GPX</h4><p>Last but not least it would be useful to have these routes on a device to follow the route. Just pass the simple feature created from each GraphHopper response path to sf::st_write</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a08e0eafd26ac30bcf0e2f1e55982187/href">https://medium.com/media/a08e0eafd26ac30bcf0e2f1e55982187/href</a></iframe><p>Ouila, 78 GPX files and 78 days of running without having to think where to go!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1e97eb4ed716" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/automating-running-routes-in-r-1e97eb4ed716">Automating Running Routes in R</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Interactive Power BI Custom Visuals with R]]></title>
            <link>https://medium.com/data-science/interactive-power-bi-custom-visuals-with-r-a6a4ac998710?source=rss-e171f5380924------2</link>
            <guid isPermaLink="false">https://medium.com/p/a6a4ac998710</guid>
            <category><![CDATA[plotly]]></category>
            <category><![CDATA[power-bi]]></category>
            <category><![CDATA[r]]></category>
            <category><![CDATA[ggplot]]></category>
            <category><![CDATA[towards-data-science]]></category>
            <dc:creator><![CDATA[Freddie Tasker]]></dc:creator>
            <pubDate>Tue, 02 Jun 2020 07:01:01 GMT</pubDate>
            <atom:updated>2020-06-04T20:04:10.401Z</atom:updated>
            <content:encoded><![CDATA[<h4>Interactive visuals with ggplot, plotly and the Power BI CLI</h4><p>Sometimes the right Power BI visual just isn’t there. Trying to create an executive summary of project management data recently I hit just this wall.</p><p>The idea was a report page that would be a front page to users - a quick signpost before they got into the detail of the other report pages, and something that would easily translate to a slide in a presentation. Attempts at slimmed down Gantt charts were all messy and unwieldly.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/748/0*rlaO3gdi3WCtQqQB.jpg" /><figcaption>A great example from the 2003 SARS outbreak of where causally linked information can be powerful, but often project management data can overload a computer screen. <a href="https://www.thelancet.com/journals/lancet/article/PIIS0140-6736(03)15259-2/fulltext">Molecular epidemiology of the novel coronavirus that causes severe acute respiratory syndrome (The Lancet)</a></figcaption></figure><h3>The Power BI R Script Visual</h3><p>After a bit of hard thought (playing) a basic horizontal timeline seemed to work well. It would focus on the project milestones, use colour to describe the project status and try its best to get the project related text as readable as possible.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/427/1*lbr7JSpFe_QU77ISTL5-yw.png" /><figcaption>The R script visual, quick to develop and (sometimes irritatingly) customisable</figcaption></figure><p>There didn’t seem to be a ready made visual in <a href="https://appsource.microsoft.com/en-GB/marketplace/apps?product=power-bi-visuals">Microsoft’s AppSource </a>that would do this, so turned to the R Script Visual. With a bit of R knowledge, these are quick to develop and allow the use of the endlessly customisable ggplot to get it looking exactly how you want it (n.b. other R plotting packages are available).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W_pGwD5XoPjIF9z9MeV6Nw.png" /></figure><p>Here’s how it ended up. Project data has been replaced with the UK’s prime ministers and the project status colour now tells us what party they’re in. It might be handy in your next Zoom quiz. <a href="https://github.com/fredwise/PM_timeline/blob/master/pm_timeline_ggplot.R">Code here</a>. Thank you to <a href="https://benalexkeen.com/creating-a-timeline-graphic-using-r-and-ggplot2/">Ben Alex Keen,</a> who has a great article on the construction of this kind of timeline in R.</p><p>The R visual dropped into the report as expected. Moving from accessing the JSON data directly in R to dealing with the data from the Power BI R Script Visual needed some variable renaming and formatting .The Power BI friendly code for the visual<a href="https://github.com/fredwise/PM_timeline/blob/master/pm_timeline_ggplot_powerbi.R"> is here</a>.</p><p>The only thing these R plots are missing is a bit of interactivity. You can restrict the data going into them with Power BI’s slicers and filters but you can’t hover over and get a tooltip, or zoom in on an interesting bit.</p><h3>Plotly</h3><p>Thanks to the <a href="https://plotly.com/r/">plotly package </a>, we can turn our ggplot object into some html and add at least some of this interactivity.</p><pre>g = ggplotly(p, tooltip = &#39;text&#39;) %&gt;%<br>  layout(legend = list(<br>    orientation = &#39;h&#39;,<br>    y = 1.1,<br>    x = 0.1<br>  ));</pre><p>Plotly will also host these charts, and you can push them up from within R.</p><pre>Sys.setenv(plotly_username = &#39;xxx&#39;)<br>Sys.setenv(plotly_api_key = &#39;xxxxxxxxxxx&#39;)</pre><pre>api_create(g)</pre><p><a href="https://chart-studio.plotly.com/~fred_tasker/7.embed">Whig, Tory, Conservative, Peelite, Liberal, Labour, Whig, Tory, Conservative, Peelite, Liberal, Labour | line chart made by Fred_tasker | plotly</a></p><h4>The Tooltip</h4><p><a href="https://plotly-r.com/improving-ggplotly.html">As well as a host of other plotly feature</a>s, we now have a hover over tooltip. Really useful in providing some more information in this fairly compact graph. This was defined using a dummy aesthetic in the geom_text call, named ‘text’ (ggplot ignores this aesthetic).</p><pre>#Tooltip for plotly<br>prime_ministers[,text:=paste0(<br>  name, &#39;, &#39;,  party, &#39;&lt;br&gt;&#39;<br>  ,format(start_date,&#39;%d %b %Y&#39;),&#39; to &#39;,format(end_date,&#39;%d %b %Y&#39;)<br>)]</pre><pre># Show project text<br>p &lt;- p +  geom_text(aes(label = name,<br>                        <strong>text=text</strong>,x=decade<br>                         ,y = text_position,colour=party), <br>                     size = 3, family = &#39;sans&#39;,show.legend = F, fontface=&#39;bold&#39;)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/655/1*u6xCHEZGxb7oUGZPJPFDSA.png" /><figcaption>A custom tooltip is passed from ggplot to the plotly object using the dummy ‘text’ aesthetic in the final call to geom_text</figcaption></figure><h4>ggplotly quirks</h4><p>A quick side note on using ggplotly for this task.</p><p><strong>geom_label: </strong>In our conversion to plotly from ggplot using ggplotly, geom_label isn’t supported, so the transparent rectangles behind each text label have been replaced with font coloring.</p><p><strong>Legend naming: </strong>There was also some strange behaviour with the legend, ‘Whig’ became ‘(Whig,1)’ etc. This was caused by the ggplot object having both a colour scale (for text and points) and a fill scale (not used, since no geom_label). The manual fill scale was removed, but it would be annoying if a fill scale was needed. The only solution seemed to be manually adjusting the plotly object json.</p><h3>Package R Code into Power BI Custom Visual</h3><p>The final step is getting this plotly object into Power BI.</p><p>We can’t just paste our R code directly into the R Script Visual. The ggplotly function outputs an HTML object and Power BI is expecting an R plot object.</p><p>Well it turns out that with not too much work, we can create our own custom visual to import into Power BI. Here’s a quick step by step.</p><p><strong>1</strong> - <a href="https://nodejs.org/en/">Install node.js</a></p><p><strong>2</strong> - Install the powerbi-visuals-tools package with Powershell</p><pre><em>npm install -g powerbi-visuals-tools</em></pre><p><strong>3</strong> - Create an rhtml template</p><pre>pbiviz new fredTimelineVis -t rhtml</pre><p><strong>4</strong> - Find the folder and files that this command creates (this depends on the working directory of your Powershell). Then find the script.r file, and replace the ggplot code with your own</p><p>On my computer the rhtml files were output to C:/Users/FredTasker/fredTimelineVis. The script.R has an example ggplot chunk, and a ggplotly line, which I replaced with those above.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/651/1*HZEDuXf4eKnvADewPfANSw.png" /><figcaption>Packaging up the prime ministers. Replace the ggplot / ggplotly bits of the script.R file that <em>powerbi-visuals-tools</em> creates.</figcaption></figure><p>A couple of notes:</p><ul><li>Make sure you have the right library declarations at the top of script.R and remember that R packages <a href="https://docs.microsoft.com/en-us/power-bi/connect-data/service-r-packages-support">need to exist in Power BI online </a>if that’s where you are going to use it</li><li>The data that’s coming from Power BI is referred to as the Values object in R.</li></ul><p>Here’s the <a href="https://github.com/fredwise/PM_timeline/blob/master/fredTimelineVis/script.r">script.R</a> file used in this example.</p><p><strong>5 - </strong>Add author, description and support URL to the pbiviz.json</p><p>This is important, as package compiling will fail if you don’t. pbviz.json will be in the same directory as your scripts.R file.</p><pre>...<br>&quot;description&quot;:<strong><em>&quot;Horizontal timeline visual build using ggplot and plotly in R&quot;</em></strong>,<br>&quot;supportUrl&quot;:<strong><em>&quot;</em></strong><a href="https://medium.com/@fredtasker/interactive-power-bi-custom-visuals-with-r-a6a4ac998710"><strong>https://medium.com/@fredtasker/interactive-power-bi-custom-visuals-with-r-a6a4ac998710</strong></a><strong><em>&quot;</em></strong>,<br>&quot;gitHubUrl&quot;:<strong>&quot;</strong><a href="https://github.com/fredwise/PM_timeline"><strong>https://github.com/fredwise/PM_timeline</strong></a><strong>&quot;</strong>},<br>...<br>&quot;author&quot;:{&quot;name&quot;:<strong><em>&quot;Fred Tasker&quot;</em>,</strong>&quot;email&quot;:<strong><em>&quot;xxxx.com&quot;</em></strong>}<br>...</pre><p><strong>6</strong> -Go back to PowerShell, navigate to your script.r directory and package it all up, ready for Power BI to consume.</p><p>In my case:</p><pre>cd C:\Users\FredTasker\fred_timeline_vis</pre><pre>pbiviz package</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/761/1*Drf3j99jWzhEqAy7NI0-kg.png" /><figcaption>Success!</figcaption></figure><p>If you run into issues,here’s the <a href="https://github.com/microsoft/PowerBI-visuals/blob/master/RVisualTutorial/CreateRHTML.md">Microsoft help page for R HTML</a>.</p><h3>Import into Power BI</h3><p>Now that the custom visual is packaged, we just need to import it into Power BI and drop our data fields into the visual.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/915/1*H-Yjoiv02-P2lTeRGhBH4w.png" /><figcaption>Import the pbiz vis file from the dist directory of your compiled package</figcaption></figure><p>Well this was all very exciting. A really quick way to build interactive custom visuals in Power BI.</p><p>Have a look at the resulting dashboard <a href="https://app.powerbi.com/view?r=eyJrIjoiOWEyYjJlYTYtNWQwNi00NGU1LTliNDEtODg2YzhhODQxYzY3IiwidCI6IjMyZTk3MjNkLWY2YjMtNDE5NS05MjlhLTkyZGFkMzIwNmRiMiJ9">on the Power BI service</a>, you’ll see that Power BI’s Publish to Web doesn’t currently support the static R visual. To see the static graph in the PBIX file and all other scripts mentioned here head to <a href="https://github.com/fredwise/PM_timeline">GitHub.</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*64obPpM6iqfIGZ_CHdSkgA.png" /><figcaption>We do more than just Prime Ministers! Feel free to contact us for a chat <a href="https://www.wisetechltd.com/">https://www.wisetechltd.com/</a></figcaption></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a6a4ac998710" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/interactive-power-bi-custom-visuals-with-r-a6a4ac998710">Interactive Power BI Custom Visuals with R</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>