<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>R on The Final Artefact</title><link>https://www.thefinalartefact.xyz/tags/r/</link><description>Recent content in R on The Final Artefact</description><generator>Hugo</generator><language>en-gb</language><lastBuildDate>Mon, 03 Jan 2022 00:00:00 +0000</lastBuildDate><atom:link href="https://www.thefinalartefact.xyz/tags/r/index.xml" rel="self" type="application/rss+xml"/><item><title>Using RScript for R Installation Management</title><link>https://www.thefinalartefact.xyz/post/rscriptt-for-r-managment/</link><pubDate>Mon, 03 Jan 2022 00:00:00 +0000</pubDate><guid>https://www.thefinalartefact.xyz/post/rscriptt-for-r-managment/</guid><description>&lt;p&gt;Most frequently, users tend to undertake common R installation and management tasks from within the R session. Frequently making use of commands, like &lt;code&gt;install.packages&lt;/code&gt;, &lt;code&gt;update.packages&lt;/code&gt; or &lt;code&gt;old.packages&lt;/code&gt; to obtain or update packages or update/verify the existing packages. Those common tasks can also be accomplished via the GUI offered within RStudio, which provides an effortless mechanism for undertaking basic package management tasks. This is approach is usually sufficient for the vast majority of cases; however, there are some examples when working within REPL^[REPL stands for &lt;strong&gt;R&lt;/strong&gt;ead &lt;strong&gt;E&lt;/strong&gt;val &lt;strong&gt;P&lt;/strong&gt;rint &lt;strong&gt;L&lt;/strong&gt;oop and is usually delivered in a form of an interactive shell. While working in Python users would commonly access REPLY by running &lt;code&gt;python&lt;/code&gt; or &lt;code&gt;ipython&lt;/code&gt;, &lt;a href="https://pythonprogramminglanguage.com/repl/"&gt;more details&lt;/a&gt;.] to accomplish common installation tasks is not hugely convenient.&lt;/p&gt;</description></item><item><title>Beauty of R and Big-O</title><link>https://www.thefinalartefact.xyz/post/beauty-of-r-and-big-o/</link><pubDate>Thu, 09 Dec 2021 00:00:00 +0000</pubDate><guid>https://www.thefinalartefact.xyz/post/beauty-of-r-and-big-o/</guid><description>&lt;script src="index_files/kePrint/kePrint.js"&gt;&lt;/script&gt;
&lt;link href="index_files/lightable/lightable.css" rel="stylesheet" /&gt;
&lt;h2 id="big-o"&gt;Big-O&lt;/h2&gt;
&lt;p&gt;The purpose of this is not to provide yet another primer on the Big-O/$\Omega$/$\Theta$ notation but to share my enduring appreciation for working with R. I will introduce Big-O only briefly to provide context but I would refer all of those who are interested to the linked materials.&lt;/p&gt;
&lt;h2 id="what-is-big-sth-notation"&gt;What is Big-sth notation&lt;/h2&gt;
&lt;p&gt;When analysing functions, we may be interested in knowing how fast a function grows. For instance, for function &lt;code&gt;\(T(n)=4n^2-2n+2\)&lt;/code&gt;, after ignoring constants, we would say that &lt;code&gt;\(T(n)\)&lt;/code&gt; grows at the order of &lt;code&gt;\(n^2\)&lt;/code&gt;. With respect to the &lt;em&gt;Big-O&lt;/em&gt; notation we would write &lt;code&gt;\(T(n)=O(n^2)\)&lt;/code&gt;^[MIT. (2021, December 9). Big O notation. Introduction to Computers and Programming. Retrieved December 26, 2021, from &lt;a href="https://web.mit.edu/16.070/www/lecture/big_o.pdf"&gt;https://web.mit.edu/16.070/www/lecture/big_o.pdf&lt;/a&gt;]. Most commonly, in computer science, we would differentiate between Big O, Big Theta &lt;code&gt;\((\Theta)\)&lt;/code&gt; and Big Omega &lt;code&gt;\((\Omega)\)&lt;/code&gt;. In a nutshell, the differences between those common notations can be summarised as follows:&lt;/p&gt;</description></item><item><title>R-based metaprogramming strategies for handling Hive/CSV interaction (Part I, imports)</title><link>https://www.thefinalartefact.xyz/post/importing-csv-to-hive/</link><pubDate>Fri, 13 Aug 2021 00:00:00 +0000</pubDate><guid>https://www.thefinalartefact.xyz/post/importing-csv-to-hive/</guid><description>&lt;h2 id="background"&gt;Background&lt;/h2&gt;
&lt;p&gt;Handling Hive/CSV interaction is a common reality of many analytical and data environments. The question on exporting data from Hive to CSV and other formats is frequently raised on online forums with answers frequently suggestring making use of &lt;a href="https://en.wikipedia.org/wiki/Sed"&gt;&lt;code&gt;sed&lt;/code&gt;&lt;/a&gt; that combined with nifty regular expressions pipes Hive output into a flat CSV files as an exporting solution. Import of large amounts of data is best handled by suitable tools like &lt;a href="https://flume.apache.org"&gt;Apache Flume&lt;/a&gt;. That is fine for simpler tables but may prove problematic for tables with a large amount of unstructured text. Frequently analysts and data scientists are faced with a challenge with storing data Hive on a irregular semi-regular basis. For instance, a job may produce new forecastring scenarios that we may want to make available through a Hive tables.&lt;/p&gt;</description></item><item><title>Why regex is not fuzzy matching</title><link>https://www.thefinalartefact.xyz/post/why-regex-is-not-fuzzy-matching/</link><pubDate>Tue, 29 Jun 2021 00:00:00 +0000</pubDate><guid>https://www.thefinalartefact.xyz/post/why-regex-is-not-fuzzy-matching/</guid><description>&lt;p&gt;Recently, I cam across an interesting discussion on StackOverflow^[SO discussion on: &lt;a href="https://stackoverflow.com/a/68182330/1655567"&gt;&lt;em&gt;Fuzzy Join with Partial String Match in R&lt;/em&gt;&lt;/a&gt;] pertaining to approach to fuzzy matching tables in R. Good answer contributed by one of the most resilient and excellent contributors to whom I owe a lot of thanks for help suggested relying on regular expression, combining this with basic string removal and transformations like &lt;code&gt;toupper&lt;/code&gt; to deterministically match the tables. The solution solved the problem and was accepted.&lt;/p&gt;</description></item><item><title>Using R for File Manipulation</title><link>https://www.thefinalartefact.xyz/post/file-manipulation-in-r/</link><pubDate>Mon, 29 Mar 2021 00:00:00 +0000</pubDate><guid>https://www.thefinalartefact.xyz/post/file-manipulation-in-r/</guid><description>&lt;h2 id="challenge"&gt;Challenge&lt;/h2&gt;
&lt;p&gt;File manipulation is a frequent task unavoidable in almost every IT business process. Traditionally, file manipulation tasks are accomplished within the ramifications of specific tools native to a given system. As such, the one may consider writing and scheduling shell scriptt to undertake frequent file operations or using more specific purpose-built tools like &lt;a href="https://linux.die.net/man/8/logrotate"&gt;&lt;code&gt;logrotate&lt;/code&gt;&lt;/a&gt; in order to archive logs or tools like &lt;a href="https://kafka.apache.org/20/documentation.html"&gt;Kafka&lt;/a&gt; are used to build streaming-data pipelines. R is usually though of as a statistical programming language or as an environment for a statistical analysis. The fact that R is a mature programming language able to successfully accomplish a wide array of traditional tasks is frequently ignored. What constitutes a &lt;em&gt;programming language&lt;/em&gt; is a valid question. Wikipedia offers somehow wide definition:&lt;/p&gt;</description></item><item><title>Inserting Data into Partitioned Table</title><link>https://www.thefinalartefact.xyz/post/inserting-data-into-partitioned-table/</link><pubDate>Fri, 26 Feb 2021 00:00:00 +0000</pubDate><guid>https://www.thefinalartefact.xyz/post/inserting-data-into-partitioned-table/</guid><description>&lt;h2 id="rationale"&gt;Rationale&lt;/h2&gt;
&lt;p&gt;Maintaining partitioned Hive tables is a frequent practice in a business. Properly structured tables are conducive to achieving robust performance through speeding up query execution (see Costa, Costa, and Santos 2019). Frequent use cases pertain to creating tables with hierarchical partition structure. In context of a data that is refreshed daily, the frequently utilised partition structure reflects years, months and dates.&lt;/p&gt;
&lt;h2 id="creating-partitioned-table"&gt;Creating partitioned table&lt;/h2&gt;
&lt;p&gt;In HiveQL we would create the table with the following structure using the syntax below. In order to keep the development tidy, I’m creating a separate database on Hive which I will use for the purpose of creating tables for this article.&lt;/p&gt;</description></item><item><title>Poor Man's Robust Shiny App Deployment (Part II)</title><link>https://www.thefinalartefact.xyz/post/sample-analytical-app-with-shiny/</link><pubDate>Fri, 12 Feb 2021 00:00:00 +0000</pubDate><guid>https://www.thefinalartefact.xyz/post/sample-analytical-app-with-shiny/</guid><description>&lt;h2 id="introduction"&gt;Introduction&lt;/h2&gt;
&lt;p&gt;This article draws on the past post concerned with utilisation of &lt;a href="https://github.com/ThinkR-open/golem"&gt;&lt;code&gt;golem&lt;/code&gt;&lt;/a&gt; for robust deployment of analytical and reporting solutions. For this article, we will assume that we are working with defined working requirements that utilise some of the Labour Market Statistics disseminated through the &lt;a href="https://www.nomisweb.co.uk"&gt;&lt;em&gt;nomis&lt;/em&gt;&lt;/a&gt; portal.&lt;/p&gt;
&lt;h2 id="change-plan"&gt;Change Plan&lt;/h2&gt;
&lt;h3 id="what-we-have"&gt;What we have&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Reporting requirements&lt;/li&gt;
&lt;li&gt;Past scriptts we used to create reports with accompanying instructions&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="what-we-want"&gt;What we want&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;em&gt;Stronger business continuity&lt;/em&gt; - we want to be able to give some access to this project and don&amp;rsquo;t be concerned with missing files, outdated unavailable documentation and questions on how to produce updated reports. We want self-encompassing entity that takes of care of its technical requirements and user-interaction^[Good parallel can be drawn between this approach and manuals available with life-saving equipment. Equipment delivers technical capacity and manual ensures operational capacity. In case of an inexperienced user one is not useful without the other. We want to ensure that user with minimum required capacity can use the tools correctly.]&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Better reproducibility&lt;/em&gt; - Easier way to re-run reports on custom parameters&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Improved efficiency&lt;/em&gt; - We want to have a possibility of quickly creating updated and re-running past reports using the app.&lt;/li&gt;
&lt;li&gt;&lt;em&gt;Better development:&lt;/em&gt;
&lt;ul&gt;
&lt;li&gt;We want to ensure that any change requests to our reporting/analytical stack won&amp;rsquo;t break crucial functionalities.&lt;/li&gt;
&lt;li&gt;We want to modularise development so new corporate branding or visualisation requirements can be applied with no (or minimal) integration in analytical function&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id="framework"&gt;Framework&lt;/h2&gt;
&lt;h2 id="package"&gt;Package&lt;/h2&gt;
&lt;p&gt;Future robust development owes a lot to solid foundations. As the aim is to capitalise on the robust R package architecture, we will look to leverage available supporting packages. As a first step, we will construct a new Shiny/R package infrastructure using &lt;code&gt;golem&lt;/code&gt;.&lt;/p&gt;</description></item><item><title>Poor Man's Robust Shiny App Deployment</title><link>https://www.thefinalartefact.xyz/post/poor-man-s-robust-shiny-app-deployment/</link><pubDate>Thu, 23 Jul 2020 00:00:00 +0000</pubDate><guid>https://www.thefinalartefact.xyz/post/poor-man-s-robust-shiny-app-deployment/</guid><description>&lt;h2 id="not-so-uncommon-problem"&gt;Not so uncommon problem&lt;/h2&gt;
&lt;p&gt;&lt;a href="https://rstudio.com/products/connect/"&gt;RStudio Connect&lt;/a&gt; and more modest &lt;a href="https://www.shinyproxy.io"&gt;Shiny Proxy&lt;/a&gt; come to mind as most obvious solutions for deploying Shiny applications in production. Application servers are ideal for deploying applications that are to be consumed on a regular basis by larger audiences. In addition to serving the application, managing dependencies and user access or logging user activity are common tasks we would expect for a publishing platform to address. Frequently, however, deployment of Shiny application is directed at smaller audiences and less frequent usage. In such a situation, are availability, accessibility and user access management requirements will be often more modest. Commonly,in business a modelling or analytical solution can be packaged in Shiny application facilitating periodical re-run of models with different parameters and updated data sets. Such solutions can be conveniently utilised to facilitated development of monthly or quarterly reports. If the app is used once per month/quarter by a narrow user group the need to deploy it on the server is not well articulated. In that particular case we are mostly interested in ensuring that we can:&lt;/p&gt;</description></item><item><title>Three-Way Operator in R</title><link>https://www.thefinalartefact.xyz/post/three-way-operator-in-r/</link><pubDate>Fri, 08 May 2020 00:00:00 +0000</pubDate><guid>https://www.thefinalartefact.xyz/post/three-way-operator-in-r/</guid><description>&lt;p&gt;Is there a merit for a three-way operator in R?&lt;/p&gt;
&lt;h2 id="background"&gt;Background&lt;/h2&gt;
&lt;p&gt;In C++20 revision added &amp;ldquo;spaceship operator&amp;rdquo;, &lt;a href="https://en.cppreference.com/w/cpp/language/operator_comparison#Three-way_comparison"&gt;which is defined as follows&lt;/a&gt;:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-r" data-lang="r"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="c1"&gt;# if lhs &amp;lt; rhs&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="c1"&gt;# if lhs &amp;gt; rhs&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="c1"&gt;# if lhs and rhs are equal/equivalent.&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;h2 id="r-implementation"&gt;R implementation&lt;/h2&gt;
&lt;p&gt;The behaviour can be achieved in R in multiple ways. A one straightforward approach would involve making use of the &lt;code&gt;ifelse&lt;/code&gt; statement&lt;/p&gt;
&lt;h2 id="ifelse-implementation"&gt;&lt;code&gt;ifelse&lt;/code&gt; implementation&lt;/h2&gt;
&lt;p&gt;Basic approach would involve comparing the two figures and respectively returning &lt;code&gt;-1&lt;/code&gt; or &lt;code&gt;1&lt;/code&gt; consistently with the definition above.&lt;/p&gt;</description></item><item><title>Interactively Loading Shiny Modules</title><link>https://www.thefinalartefact.xyz/post/interactive-module/</link><pubDate>Sat, 24 Nov 2018 00:00:00 +0000</pubDate><guid>https://www.thefinalartefact.xyz/post/interactive-module/</guid><description>&lt;h2 id="tldr"&gt;TL;DR&lt;/h2&gt;
&lt;p&gt;If you want to see the implemented solution, please refer to:
GitHub repo.&lt;/p&gt;
&lt;h2 id="context"&gt;Context&lt;/h2&gt;
&lt;p&gt;Shiny is a widely popular web application framework for a R. In simple tearms it enables any R programmer to develop and deploy web application. This application could be simple - an interactive document consisting of a few charts and tables or a c complex &amp;ldquo;behemoth&amp;rdquo; with multiple functionalities enabling end-users to run models, query external data, generate exportable reports and sophisticated visuals.&lt;/p&gt;</description></item><item><title>ASCII charts in R</title><link>https://www.thefinalartefact.xyz/post/ascii-charts-in-r/</link><pubDate>Fri, 05 Jun 2015 00:00:00 +0000</pubDate><guid>https://www.thefinalartefact.xyz/post/ascii-charts-in-r/</guid><description>&lt;p&gt;In Stata it is possible to use function plot in order to get a simple scatter plot in Stata console. As of Stata eight, plot is no longer supported but remains a useful tool for quickly exploring relationships between variables. Using plot on the auto data provides the following results:&lt;/p&gt;
&lt;p&gt;&lt;a href="https://www.thefinalartefact.xyz/post/ascii-charts-in-r/images/stata_text_plot.png" target="_blank" rel="noopener noreferrer"&gt;
&lt;img alt="Stata Textual Plot" loading="lazy" src="https://www.thefinalartefact.xyz/post/ascii-charts-in-r/images/stata_text_plot.png"&gt;
&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Now the question is: can we achieve the same level of convenience in R? Of course. The txtplot package authored by Bjoern Bornkamp provides similar functionality. Executing the code below will generate nice text plot straight in the R console:&lt;/p&gt;</description></item><item><title>Managing rows in the ggplot legend</title><link>https://www.thefinalartefact.xyz/post/managing-rows-in-the-ggplot-legend/</link><pubDate>Sat, 28 Mar 2015 00:00:00 +0000</pubDate><guid>https://www.thefinalartefact.xyz/post/managing-rows-in-the-ggplot-legend/</guid><description>&lt;p&gt;After developing the Shiny App sourcing live labour market data from NOMIS. I wanted to accommodate a convenient way of managing rows in the legend. In particular, I wanted to account for the situation where end-user may select a number of geographies that will only conveniently fit into two or more rows. After transposing the data to long format, guessing the number of elements in the legend is relatively simple as it will correspond to the number of unique geographies passed via the subset command.&lt;/p&gt;</description></item><item><title>Amusing way to get user input windows in R</title><link>https://www.thefinalartefact.xyz/post/amusing-way-to-get-user-input-windows-in-r/</link><pubDate>Wed, 01 Feb 2012 00:00:00 +0000</pubDate><guid>https://www.thefinalartefact.xyz/post/amusing-way-to-get-user-input-windows-in-r/</guid><description>&lt;p&gt;In an unlikely scenario that beautiful &lt;a href="http://shiny.rstudio.com/" target="_blank" rel="noopener"&gt;Shiny &lt;/a&gt;apps do not meet your analytical requirements and developing a full-blown user interface. in &lt;a href="http://www.ggobi.org/rgtk2/"&gt;RGtk2&lt;/a&gt; may seem to be a little too much, there is a third, often overlooked solution, - package &lt;a href="https://cran.r-project.org/web/packages/svDialogs/index.html"&gt;svDialogs&lt;/a&gt; by Philippe Grosjean. The package in a convenient way enables user to create various interface gadgets. For example the code:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;div class="chroma"&gt;
&lt;table class="lntable"&gt;&lt;tr&gt;&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code&gt;&lt;span class="lnt"&gt;1
&lt;/span&gt;&lt;span class="lnt"&gt;2
&lt;/span&gt;&lt;span class="lnt"&gt;3
&lt;/span&gt;&lt;span class="lnt"&gt;4
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class="lntd"&gt;
&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-r" data-lang="r"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;svDialogs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;## Let&amp;#39;s keep some data in one place&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;user_figure&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;-&lt;/span&gt; &lt;span class="n"&gt;svDialogs&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;dlg_input&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;p&gt;would result in the following window being presented to the user:&lt;/p&gt;</description></item></channel></rss>