<?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 Eric Bonucci on Medium]]></title>
        <description><![CDATA[Stories by Eric Bonucci on Medium]]></description>
        <link>https://medium.com/@ericbonucci?source=rss-311efc5c09------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*uuIGKkPQoEe61JLGEinUvA.jpeg</url>
            <title>Stories by Eric Bonucci on Medium</title>
            <link>https://medium.com/@ericbonucci?source=rss-311efc5c09------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 12:40:44 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ericbonucci/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[Forecasting of content readers]]></title>
            <link>https://ericbonucci.medium.com/forecasting-of-content-readers-8a95e9f0ca94?source=rss-311efc5c09------2</link>
            <guid isPermaLink="false">https://medium.com/p/8a95e9f0ca94</guid>
            <category><![CDATA[content-creation]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[forecasting]]></category>
            <category><![CDATA[r]]></category>
            <dc:creator><![CDATA[Eric Bonucci]]></dc:creator>
            <pubDate>Mon, 18 Jan 2021 10:58:43 GMT</pubDate>
            <atom:updated>2021-01-18T10:58:43.918Z</atom:updated>
            <content:encoded><![CDATA[<h4>A data science project applied to online content creation</h4><p>I have written a handful of articles on Medium: my goal is to share them with as many readers as possible.</p><p>Having <strong>goals</strong> and <strong>expectations</strong> is essential to foster motivation. Exceeding expectations is a powerful source of motivation, but not meeting them can put the whole project in danger.</p><p>Based on a scientific approach, I wanted to set <em>reasonable </em>expectations<em> </em>so that I maximise my chances to exceed them, and benefit from this motivation boost to move faster towards my goal.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*sW8oNuOOhSw0wLlJK2lJ9Q.jpeg" /><figcaption>My crystal ball seems to be broken… <a href="https://unsplash.com/photos/gO0byDPgxzI">Source</a></figcaption></figure><p>Before diving into technical details, have you heard about <a href="https://amzn.to/3hkhkgr">Make Your Brain Work by Amy Brann</a>? It is an amazing book on how to make the most out of your brain.</p><p>The author analyses day-to-day business situations from a neuroscience point of view and shows how they can relate to famous scientific experiments. She also provides tips and discusses benefits of mastering our brain’s functionalities. Definitely recommended!</p><p><em>Note that the provided link is an Amazon affiliated link: I may earn money from this recommendation.</em></p><blockquote>These series of articles describe a real-world data science project, where the goal is to <strong>predict</strong> <strong>how many readers blog posts are likely to reach within a given period of time</strong>. I will use them to set personal expectations but one can imagine that a business could adjust marketing expenses according to these predictions to reach its goals.</blockquote><p>This project will typically follow 4 steps.</p><h4>Project planning</h4><ol><li>Scrap historical data from Medium statistics page</li><li>Prepare the data to make it suitable for machine learning</li><li>Build a predictive machine learning model</li><li>Estimate accuracy of predictions</li></ol><p>Let’s think about a few obstacles we may find during this journey:</p><ul><li><strong>Lack of historical data</strong>: predictions may be of high uncertainty if there are not much data points (i.e. daily readers)</li><li><strong>Lack of predictive features</strong>: who knows how the Medium algorithm works?</li></ul><h3>Scrap the data</h3><p>In machine learning, the main assumption is that <em>the future is like the past, </em>so predictions are made out of historical data<em>.</em></p><p>Historical readers data is located inside the <em>stats </em>web page of Medium accounts.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SJnYuiVdk7bkbfHDxhHoQw.png" /><figcaption>Daily readers data can be visualized with a bar chart</figcaption></figure><p>With R, there are two well-known packages used for scraping the web: rvest which is part of the <a href="https://www.tidyverse.org/">tidyverse</a> and RSelenium which was developed by <a href="https://ropensci.org/">rOpenSci</a>. Both packages can be installed through CRAN.</p><blockquote>As a rule of thumb, I choose RSelenium when I need to interact with the website before scraping the data (navigate, click…), and rvest when basically all the data can be found inside the source code of the web page.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*h6g1xvT2bng663bODvj8tQ.png" /><figcaption>French equivalent of Right click &gt; Inspect. Makes your web browser display source code of a given element on the web page</figcaption></figure><p>🎉 Readers data is directly accessible from the source code of the page, at least for the last 30 days. This is the best scenario as you just need to read the source code of the page to get the data of interest. However, a click is required to display older data points, so we may need RSelenium at some point…</p><p>… but I was not granted access to the <em>stats</em> page (error code 401). According to this <a href="https://en.wikipedia.org/wiki/List_of_HTTP_status_codes">error code mapping</a>, signing in is required before heading to the <em>stats</em> page…</p><p>… but Medium sends <em>magic links</em> inside emails for users to authenticate and sign in, so my RSelenium bot would have to check my emails inbox and click this link to keep going… This is where I decided to end my web scraping journey. 😌</p><blockquote>I have a basic knowledge of web scraping, and I would be very interested to read from you if you know how to reach Medium account’s stats page programmatically!</blockquote><h4>Practical solution</h4><p>This is one of these moments where you can transform having just a few data points into an opportunity.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VPtSJVjLMkuogs-01pCq_Q.png" /><figcaption>That is also web scraping.</figcaption></figure><p>I manually downloaded each page for the last 4 months I have been writing on Medium. All the daily readers data is located into these downloaded web pages.</p><h4>Data quality</h4><p>I performed a new extraction a few days later to make sure the readers data remained unchanged: there is no re-count process or adjustments once a day ended.</p><p>However, the final day, for which data is incomplete, will be ignored in the rest of the study.</p><h3>Data wrangling</h3><p>I use rvest to navigate inside the downloaded web pages, as if I was actually visiting the <em>stats </em>page!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RWvKWuun3t5OoQjXhGdLIA.png" /><figcaption>Web scraped data is often messy HTML code. Readers data is located in the last attribute.</figcaption></figure><p>The data-tooltip attribute’s values contains the number of<strong> readers</strong> at a given <strong>date</strong>. The former is straight forward whereas the latter requires a few operations to build a date matching <a href="https://en.wikipedia.org/wiki/ISO_8601">ISO standard</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mWeGpBQj5lcYn-yFiNLQHA.png" /></figure><p>Extracting the number of <strong>readers</strong> basically consists in extracting the first numbers from the raw text.</p><p>The <strong>month</strong> is extracted as a plain text and mapped to its corresponding number (1-January…12-December). The <strong>day</strong> is ending the raw text and the <strong>year</strong> is assumed 2020. Concatenating them all together creates a <strong>date</strong> in a convenient format.</p><p>We now have readers historical data for each day:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/927/1*gyrifeARi2_UWMXxWTWDBA.png" /></figure><h3>Conclusion</h3><p>It was the first step of this data science project: find and load the data.</p><p>It was not possible to use web scrapping tools to collect the data automatically. In any case if it can be done later, the rest of the pipeline will still be valid (as long as Medium does not modify the source code 😊).</p><p>Although we performed some wrangling operations to start working with historical readers data, there is still a lot of data processing tasks before building our first forecasting model.</p><p>The next article will focus on feature engineering to enrich this initial data set and create predictive inputs to leverage when forecasting readers. For that purpose I tracked all the actions I have performed on social networks to share my articles…</p><p>…Stay tuned!</p><h4>References</h4><ul><li>rOpenSci GitHub page for RSelenium: <a href="https://github.com/ropensci/RSelenium">https://github.com/ropensci/RSelenium</a></li><li>tidyverse GitHub page for rvest: <a href="https://github.com/tidyverse/rvest">https://github.com/tidyverse/rvest</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8a95e9f0ca94" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deploy a Shiny web application online for free]]></title>
            <link>https://ericbonucci.medium.com/deploy-a-shiny-web-application-online-for-free-d3fa65feedcc?source=rss-311efc5c09------2</link>
            <guid isPermaLink="false">https://medium.com/p/d3fa65feedcc</guid>
            <category><![CDATA[rstats]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[analytics]]></category>
            <category><![CDATA[shiny]]></category>
            <dc:creator><![CDATA[Eric Bonucci]]></dc:creator>
            <pubDate>Wed, 02 Dec 2020 13:01:22 GMT</pubDate>
            <atom:updated>2020-12-02T13:01:22.170Z</atom:updated>
            <content:encoded><![CDATA[<h4>Give access to your products using 3 R commands</h4><p>Release data science work through online applications creates tangible value for your customers. In this article, I provide a step by step tutorial to deploy a Shiny application using <strong>shinyapps.io</strong>, a cloud service developed by RStudio which offers a free pricing option.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZNF596ogj9a0FspW-fAD_g.jpeg" /><figcaption>Take your projects out of the museum <a href="https://unsplash.com/photos/VbI0LMaGMlg">Source</a></figcaption></figure><p>The 3 most common cloud services used by data science teams are Amazon Web Services (AWS), Google Cloud Platform (GCP) and Microsoft Azure (Azure).</p><p>Leveraging Docker container technology, these platforms may provide entry points to machine learning models or analytics web applications. These tools are then accessible online.</p><p>Amazon, Google and Microsoft offer free trials, which simply put, consist in a given amount of money to test the platform for a period of 1 to 12 months. In the end of the trial, you will have to upgrade to a paid account to keep using the service.</p><p><strong>However, even in the context of a free trial, there is a risk of receiving a huge bill at the end of the month. </strong>It is not always possible to set a limit to services usage, which would be convenient to avoid exceeding the free trial money. Moreover, at the end of the trial, your deployed applications will be out-of-service if you don’t upgrade to a paid account.</p><p>Not really suitable for someone trying to monkey with Shiny web applications…</p><p>As the goal here is just to release a Shiny application online, here’s a very simple solution involving $0, where exceeding usage limit is not possible: <strong>shinyapps.io</strong> by RStudio</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*375mGjW0OsalcuRHNqFgAw.png" /><figcaption>Pricing is crystal clear, as of November 2020</figcaption></figure><p>The free option offers 5 applications and 25 hours of monthly usage. <strong>No credit card required to sign up, and a service that stops whenever usage limit is reached</strong>. In my opinion, this is a really good option to safely start deploying Shiny applications online.</p><blockquote>Wait… you do not have any Shiny application yet? No worries, 24 lines of code are enough to start building your own. <em>🚀</em></blockquote><p>The sign up process is straight forward: you need to create an account and define your server’s root URL. Don’t forget to verify your email address… and that’s it!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2_83kMq3nE_QBVh6FRaQQw.png" /></figure><p>Once you landed on your dashboard, you find 3 steps to deploy your Shiny web application online, each step being a single R command I discuss below.</p><h4>Step 1 / 3</h4><p>To release your application on <strong>shinyapps.io</strong>, you need rsconnect package: this is a programmatic interface for deployments developed by the RStudio team. It contains functions to deploy a Shiny web application directly from the R console!</p><blockquote>Need a quick reminder on how to install a R package? Check out the first lines of my previous article!</blockquote><p><a href="https://ericbonucci.medium.com/essential-r-packages-for-data-science-projects-d79cb5698b96">Essential R packages for data science projects</a></p><h4>Step 2 / 3</h4><p>Authorize your computer to perform deployment operations on your behalf using rsconnect::setAccountInfo command.</p><p>Basically, you must provide a token and a secret, available on your <strong>shinyapps.io</strong> dashboard: just click the button <em>Copy to clipboard</em> and paste the result in your R console.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rW0PMbGSnyS69lO7wilCWg.png" /><figcaption>The <em>shinyapps.io</em> dashboard, the first time you log in</figcaption></figure><h4>Step 3 / 3</h4><p><a href="https://ericbonucci.medium.com/24-lines-of-code-to-build-a-shiny-web-application-from-scratch-3f5c730239d7">24 lines of code to build a Shiny web application from scratch</a></p><p>Now it is time to deploy your Shiny application using rsconnect::deployApp. You may give any path as a parameter of this function, as long as it leads to the app.R file of your web application. Mine is located in the current directory:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rI8dMIHOpZm3n3rGTHAFDA.png" /><figcaption>In a few seconds, the app is built and deployed on shinyapps.io</figcaption></figure><p>Before testing the app, let’s have a look at the dashboard on shinyapps.io: the application is now online and running!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*e-XR75U1Wbr0Pmq1UF70nA.png" /></figure><p>A click on the name of the application leads to a more detailed dashboard:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VDVQOZjzwNsc0gty_eCr3Q.png" /><figcaption>Link to the application is in the <strong>URL</strong> row</figcaption></figure><p>There are a lot of functionalities here and some of them are unknown to me (<em>Size: large</em>?). This dashboard deserve an entire article so stay tuned. 😉</p><p>Now let us have a look at our deployed application, assuming you started from the one provided, after clicking on its URL…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7m71D4-E995Gi59JvW5d6w.png" /></figure><p>Oops. Fortunately the error is clear: runApp command should be remove when the application is deployed.</p><p>However keeping this command to run the Shiny application in RStudio viewer pan is really convenient. <a href="https://stackoverflow.com/questions/31423144/how-to-know-if-the-app-is-running-at-local-or-on-server-r-shiny">RStudio software engineer Yihui Xie suggests this line of code</a> to detect whether the Shiny application runs locally or on the server.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4cdb966230487c9aefcff4f50866ffec/href">https://medium.com/media/4cdb966230487c9aefcff4f50866ffec/href</a></iframe><p>It basically detects if the environment variable SHINY_PORT is set, which is the case when the application is running on a Shiny Server instance. Here we create a logical variable to decide if runApp should be called or not in app.R file.</p><blockquote>You may find the full project <a href="https://github.com/eric-bonucci/shiny_tutorial/tree/main/1_deployed_basic_application">on my Github account</a></blockquote><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/87eb80596e9197a0e11b334101c1a4e8/href">https://medium.com/media/87eb80596e9197a0e11b334101c1a4e8/href</a></iframe><p>Now after deploying again using the previous rsconnect command, the previous version of our application is erased on the shinyapps.io platform. At the end of the process, let us go back on our dashboard, and click the application’s URL:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vmTxCQj4l-AjwKw_dRmaEA.png" /><figcaption>This incredible Shiny application is now online!</figcaption></figure><h4>Final words</h4><p>You are now able to build and deploy a Shiny application online for free, which is an opportunity for you to share some work with anyone having the URL of your application. I hope you enjoyed this article, feel free to share some links to your applications 😉 feedback is welcome!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d3fa65feedcc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[24 lines of code to build a Shiny web application from scratch]]></title>
            <link>https://ericbonucci.medium.com/24-lines-of-code-to-build-a-shiny-web-application-from-scratch-3f5c730239d7?source=rss-311efc5c09------2</link>
            <guid isPermaLink="false">https://medium.com/p/3f5c730239d7</guid>
            <category><![CDATA[analytics]]></category>
            <category><![CDATA[shiny]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[r]]></category>
            <dc:creator><![CDATA[Eric Bonucci]]></dc:creator>
            <pubDate>Tue, 10 Nov 2020 14:03:45 GMT</pubDate>
            <atom:updated>2020-11-10T14:36:12.311Z</atom:updated>
            <content:encoded><![CDATA[<h4>Including comments 😉</h4><p>Web applications have become very popular in the data industry to give access to self-service analytics tools. In a nutshell, these applications enable users to run <em>data processing scripts</em> without any technical knowledge.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RVPRxqz2VUuY7NGXSXzmtw.jpeg" /><figcaption>A good-looking web application. Source: <a href="http://unsplash.com/">unsplash.com</a></figcaption></figure><p>Extremely flexible, these standalone tools may contain basic features (e.g. data visualization) as well as advanced functionalities (e.g. label training data for machine learning).</p><p><strong>Having a background in web development is not required for data experts</strong>. High-level libraries were developed for the most commonly used programming languages in the industry: <a href="https://shiny.rstudio.com/">Shiny</a> (R) and <a href="https://bokeh.org/">Bokeh</a> (Python).</p><p>Below in this article, you will find a total of 24 explained lines of code to build a Shiny application from scratch: it can be used as a starting point for your projects.</p><p>Shiny was first released in 2012 and requires no specific knowledge in web development, but I assume you have some working knowledge of R. If not, check out my previous article which has a quick reminder on how to install R packages, and you are good to go!</p><p><a href="https://ericbonucci.medium.com/essential-r-packages-for-data-science-projects-d79cb5698b96">Essential R packages for data science projects</a></p><p>You may find the script files used in this article <a href="https://github.com/eric-bonucci/shiny_quick_start">on this GitHub repository</a>.</p><p>Let’s dive in the technical details.</p><p>The application code is separated into two scripts: <strong>environment.R</strong> and <strong>app.R</strong>, the former having just a few lines to load packages and declare variables used later.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/40495a4ec1d6e89d0ced2f99c7eada83/href">https://medium.com/media/40495a4ec1d6e89d0ced2f99c7eada83/href</a></iframe><p>The shiny package contains all the required functions to build a web application, simply put, it is the package that makes everything possible. On top of that, shinydashboard<strong> </strong>provides high level functions to create an attractive user interface.</p><blockquote>Separating this part of the code from the main script that builds and runs the Shiny app will help as the number of libraries and variables rapidly grows with the size of the web application.</blockquote><p>The main script <strong>app.R</strong> contains the following operations, to create the web application:</p><ol><li>Define the user interface, using shinydashboard functions</li><li>Define the server function, which contains the logic behind the user interface (e.g. code triggered after a button is hit)</li><li>Build and run the app</li></ol><p>In this example, the server function<em> </em>is empty as this app as no functionalities yet. It just sets up everything required to create an app from scratch. A good-looking app that does nothing 🙃.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3208e429cc45846b29c9dd73ce07532a/href">https://medium.com/media/3208e429cc45846b29c9dd73ce07532a/href</a></iframe><p>Before having a look at the result, let’s describe with more details each stage of the script:</p><h4>1. The user interface</h4><p>The user interface is made of a dashboard <em>page</em>, containing a <em>header</em> (at the top), a <em>sidebar</em> (on the left) and a <em>body</em> (on the right). The <em>title</em> defined when creating the header is displayed in the upper left-hand corner of the web page. Inside the <em>sidebar</em> was created a <em>menu</em>, which is empty for the moment but still has to have a unique string identifier: ID_UI_SIDEBAR<em>. </em>Following the same logic, inside the <em>body </em>was created an <em>output </em>which may basically contain any UI element. This <em>output </em>also has its unique string identifier: ID_UI_BODY<em>.</em></p><blockquote>Adding the version number to the title of the application may help future users to know which variant of the tool they are working with.</blockquote><p>A few words about UI elements identifiers: <strong>they are essential for the server to be able to refer to each UI element</strong>, and that’s why they have to be unique. For example, when implementing a given action triggered when a button is hit, the button’s identifier is used as an input for the server to detect the hit and perform the relevant action.</p><blockquote>Declaring the identifiers as variables in <strong>environment.R </strong>instead of directly using their string representation enables auto-completion, which is convenient when implementing the server code.</blockquote><h4>2. The server</h4><p>As mentioned, the server is an empty function in this example, so there is not much to say here. This is where data experts writes their data processing code, with slight modifications so that it is <em>connected </em>to inputs from the user interface. This part of the application will be discussed in a different article, where the application actually does something 🙃.</p><blockquote>The server function may also take <em>session</em> as a parameter, which contains non-input informations (e.g. metadata). More details can be found <a href="https://shiny.rstudio.com/articles/client-data.html">in this RStudio article</a>.</blockquote><h4>3. Running the app</h4><p>The web application is created and ran using shinyApp and runApp functions, or clicking <em>Run App </em>button in RStudio.</p><blockquote>The additional parameter launch.browser is used to display the application in the RStudio Viewer pan: it avoids opening a new window for each run, which is the default setting when this parameter is not provided.</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IC7XJK-gBro4ujylplMKlw.jpeg" /><figcaption>The Shiny web application runs in the Viewer pan of RStudio, by default in the lower right-hand corner</figcaption></figure><p>Okay… I maybe should have added at least one UI element, but still, this web application already looks nice, doesn’t it ?</p><h4>Conclusion</h4><p>24 lines of code are enough to start a Shiny web application project, thanks to fantastic packages shiny and shinydashboard.</p><p>Still, there is a lot more to cover but this is a first step into R Shiny development, which is a real opportunity for businesses to create valuable analytics tools.</p><p>Thanks for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3f5c730239d7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Essential R packages for data science projects]]></title>
            <link>https://ericbonucci.medium.com/essential-r-packages-for-data-science-projects-d79cb5698b96?source=rss-311efc5c09------2</link>
            <guid isPermaLink="false">https://medium.com/p/d79cb5698b96</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[rstudio]]></category>
            <category><![CDATA[community]]></category>
            <category><![CDATA[productivity]]></category>
            <category><![CDATA[r]]></category>
            <dc:creator><![CDATA[Eric Bonucci]]></dc:creator>
            <pubDate>Sat, 15 Aug 2020 16:29:12 GMT</pubDate>
            <atom:updated>2020-11-12T09:37:01.238Z</atom:updated>
            <content:encoded><![CDATA[<h4>Leverage genius work from R community in your projects!</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Kqj7RxbXhpAtsdd6QFxIaA.jpeg" /></figure><p>There are more than 16 000 packages on the <a href="https://cran.r-project.org/"><em>Comprehensive R Archive Network (CRAN)</em></a><em> </em>that gather a lot of commonly used methods in data science projects. Time runs fast, and it may takes days to code functionalities for sometimes basic tasks… Fortunately, we can leverage many packages to focus on what is essential for projects to be successful!</p><h4>Quick reminder: install and use packages</h4><p>The most common way is to install a package directly from CRAN using the following R command:</p><pre># this command installs tidyr package from CRAN<br>install.packages(&quot;tidyr&quot;)</pre><p>Once the package is installed on your local machine, you don’t need to run this command again, unless you want to update the package with its latest version! If you want to check the version of a package you installed, you may use:</p><pre># returns tidyr package version<br>packageVersion(&quot;tidyr&quot;)</pre><p><a href="https://rstudio.com/products/rstudio/">RStudio IDE</a> also provides a convenient way to check if any update is available for installed packages in <strong>Tools/Check for packages updates…</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/470/1*M3jgxxfjmqSqyVHBwaUXzw.png" /><figcaption>Update all your packages in a few clicks using RStudio</figcaption></figure><p>Last but not least: how to use a package now it is installed :) You may either specify the package name in front of its included method:</p><pre>stringr::str_replace(&quot;Hello world!&quot;, &quot;Hello&quot;, &quot;Hi&quot;)</pre><p>Or run the following command to load all the package’s functions at once:</p><pre># load a package: it will throw an error if package is not installed<br>library(stringr)</pre><p>Now you’re ready to go!</p><p>If you want to learn basically everything about R packages development, I highly recommend <a href="http://r-pkgs.had.co.nz/intro.html"><em>Hadley Wickham R packages book (free online version)</em></a>.</p><h4>Fetching data</h4><p>Fetching data is often the starting point of a data science project: data can be located in a database, an Excel spreadsheet, a comma-separated values (csv) file… it is essential to be able to read it regardless of its format, and avoid headaches before even starting to work with the data!</p><ul><li><strong>When data is located in a .csv files or any delimited-values file</strong></li></ul><p>The readr package provides functions that are up to <strong>10 times faster than base R</strong> functions to read rectangular data.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/221/1*oP09AajaWAU6IEEhCJ9AjQ.png" /><figcaption>Great R packages usually have a dedicated hex sticker: <a href="https://github.com/rstudio/hex-stickers">https://github.com/rstudio/hex-stickers</a></figcaption></figure><p>Convenient methods exist for <strong>reading and writing</strong> standard .csv files as well as custom files with a custom values separation symbol:</p><pre># read csv data delimited using comma (,)<br>input_data &lt;- readr::read_csv(&quot;./input_data.csv&quot;)<br># read csv data delimited using semi-colon (;)<br>input_data &lt;- readr::read_csv2(&quot;./input_data.csv&quot;)<br># read txt data delimited using whatever symbols (||)<br>input_data &lt;- readr::read_delim(&quot;./input_data.txt&quot;, delim = &quot;||&quot;)</pre><p>In addition to good looking stickers, great R packages also have <a href="https://rawgit.com/rstudio/cheatsheets/master/data-import.pdf">cheat sheets</a> you can refer to!</p><ul><li><strong>When data is located in an Excel file</strong></li></ul><p>Microsoft Excel has its own file formats (.xls and .xlsx) and is very commonly used to store and edit data. The package readxl enables efficient reading of these files into R, you can even only read a specific spreadsheet:</p><pre># read Excel spreadsheets<br>input_data &lt;- readxl::read_excel(&quot;input_data.xlsx&quot;, sheet = &quot;page2&quot;)</pre><ul><li><strong>When data is located in a database or in the cloud</strong></li></ul><p>When it comes to fetching data from databases, DBI makes it possible to connect to any server, as long as you provide the required credentials, and run SQL queries to fetch data. Because there are many different databases and ways to connect depending on your technical stack, I suggest that you refer to the complete documentation provided by RStudio to find the steps that suit your needs: <a href="https://db.rstudio.com/">Databases using R</a>.</p><p>Make sure to check if a package exists to connect to your favorite cloud services provider! For example, bigrquery enables fetching data from Google BigQuery platform.</p><h4>Wrangling data</h4><p>You may have noticed a lot of the previously mentioned packages are part of the <a href="https://www.tidyverse.org/"><em>tidyverse</em></a>. This collection of packages forms a powerful toolbox that you can leverage throughout your data science projects. Mastering these packages is key to become super efficient with R.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/200/1*Y9TfppHhxuthQz7aA1uwfg.png" /><figcaption>The pipe operator shipped with the <em>magrittr package is a game changer </em><a href="https://github.com/tidyverse/magrittr">https://github.com/tidyverse/magrittr</a></figcaption></figure><p>Data wrangling is made easy using the pipe operator, which goal is simply to <em>pipe</em> left-hand values into right-hand expressions:</p><pre># without pipe operator<br>paste(&quot;Hello&quot;, &quot;world!&quot;)<br># with pipe operator<br>&quot;Hello&quot; %&gt;% paste(&quot;world!)</pre><p>It may not seem obvious in this example, but this is a life-changing trick when you need to perform several sequential operations to a given object, typically a data frame.</p><p>Data frames usually contains your input data, making it the R object you probably work the most with. dplyr is a package that provides useful functions to edit, filter, rearrange or join data frames.</p><pre>library(dplyr)<br># mtcars is a toy data set shipped with base R<br># create a column <br>mtcars &lt;- mtcars %&gt;% mutate(vehicle = &quot;car&quot;)<br># filter on a column<br>mtcars &lt;- mtcars %&gt;% filter(cyl &gt;= 6)<br># create a column AND filter on a column<br>mtcars &lt;- mtcars %&gt;% <br>  mutate(vehicle = &quot;car&quot;) %&gt;%<br>  filter(cyl &gt;= 6)</pre><p>Now you should understand my point about the power of the pipe operator :)</p><p>There is so much more to say about data wrangling that you can find entire books discussing the topic, such as <a href="https://amzn.to/2DNBlNj">Data Wrangling with R</a>. In addition, a key work on leveraging tidyr functionalities is <a href="https://amzn.to/2E6jSPT">R for Data Science</a>. A free online version of the latter can be found <a href="https://r4ds.had.co.nz/">here</a>. Please notice that these are Amazon affiliated links so I will receive a commission if you decide to buy the books.</p><p><strong>Visualization</strong></p><p>One of the main reason R is a very good choice for data science projects may be ggplot2. This package makes it easy and eventually fun to build visualizations that looks good and gather a lot of informations.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/720/1*Fx4qGPeOqbECEKtBcz5WGw.png" /><figcaption>You may find inspirations from this Top 50 ggplot2 visualisation article : <a href="http://r-statistics.co/Top50-Ggplot2-Visualizations-MasterList-R-Code.html">http://r-statistics.co/Top50-Ggplot2-Visualizations-MasterList-R-Code.html</a></figcaption></figure><p>ggplot2 is also part of the tidyverse collection, that’s why it works perfectly with shapes of data you typically obtain after tidyr or dplyr data wrangling operations. Managing to plot histograms and scatter plots is rather quick. Then many additional elements can be used to enhance your plots.</p><h4>Machine learning</h4><p>Another very convenient package is caret, that wraps up a lot of methods typically used in machine learning processes. From data preparation, to model training and performances assessment, you will find everything you need when working on predictive analytics tasks.</p><p>I recommend reading the caret chapter <a href="https://topepo.github.io/caret/model-training-and-tuning.html">about model training </a>where this key task is discussed. Here is a very simple example of how to train a logistic regression:</p><pre>library(dplyr)</pre><pre># say we want to predict iris having a big petal width<br>observations &lt;- iris %&gt;% <br>  mutate(y = ifelse(Petal.Width &gt;= 1.5, &quot;big&quot;, &quot;small&quot;)) %&gt;% <br>  select(-Petal.Width)</pre><pre># set up a a 10-fold cross-validation<br>train_control &lt;- caret::trainControl(method = &quot;cv&quot;, <br>                                     number = 10, <br>                                     savePredictions = TRUE, <br>                                     classProbs = TRUE)</pre><pre># make it reproducible and train the model<br>set.seed(123)<br>model &lt;- caret::train(y ~ .,<br>                      data = observations,<br>                      method = &quot;glm&quot;,<br>                      trControl = train_control,<br>                      metric = &quot;Accuracy&quot;)</pre><h4>Final words</h4><p>Thanks a lot for reading my very first article on Medium! I feel like there is so much more to say in each section, as I did not talk about other super useful packages such as boot, shiny, shinydashboard, pbapply… Please share your thoughts in the comments, I am very interested in feedbacks on what you are willing to explore in future articles.</p><h4>Useful documentations and references</h4><ul><li>readr <a href="https://readr.tidyverse.org/index.html"><em>https://readr.tidyverse.org/index.html</em></a></li><li>readxl <a href="https://readxl.tidyverse.org/"><em>https://readxl.tidyverse.org/</em></a></li><li>bigrquery <a href="https://github.com/r-dbi/bigrquery"><em>https://github.com/r-dbi/bigrquery</em></a></li><li>caret <a href="http://topepo.github.io/caret/index.html"><em>http://topepo.github.io/caret/index.html</em></a></li><li>tidyr <a href="https://tidyr.tidyverse.org/"><em>https://tidyr.tidyverse.org/</em></a></li><li><strong>All RStudio cheatsheets</strong> <a href="https://rstudio.com/resources/cheatsheets/"><em>https://rstudio.com/resources/cheatsheets/</em></a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d79cb5698b96" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>