19:26, 4th July 2021
Passing arguments to an R script from command lines
There are two main approaches to passing external arguments to an R script from the command line. The first uses the built-in R function commandArgs, which scans the arguments supplied when the R session was invoked, storing them in a vector that the script can then reference by position, with the ability to set default values or return errors when required arguments are missing. The second approach uses the optparse package, which works in a Python-like style by allowing named flags and options to be declared with their types, default values and help messages, producing a more structured and user-friendly interface that also generates automatic help documentation when requested.
09:31, 3rd July 2021
R provides two versions each of the logical AND and OR operators, namely the shorter vectorised forms (& and |) and the longer scalar forms (&& and ||). The shorter forms compare pairs of elements across two vectors and return a result of the same length, making them well-suited for filtering rows in a data frame. The longer forms, by contrast, only examine the first element of each input and return a single scalar value, with R 4.2.0 introducing warnings when vectors longer than one element are passed to them.
The longer forms also support short-circuit evaluation, meaning the second operand is not assessed if the result can be determined from the first alone. Because if() statements require a scalar condition, the longer forms are generally preferred in control-flow programming, though care must be taken to supply genuinely scalar inputs.
The functions all() and any() offer a clean way to reduce a logical vector to a scalar by applying AND or OR across all elements respectively. Finally, NA values complicate logical operations by infecting results, though the isTRUE() and isFALSE() functions provide a reliable means of testing whether a value is strictly and unambiguously TRUE or FALSE.
16:54, 2nd July 2021
Speeding Up R Shiny – The Definitive Guide
Optimising the performance of R Shiny applications is achievable through a combination of thoughtful development practices and targeted technical interventions. Proper data handling is a foundational concern, including preprocessing static data in advance and choosing appropriate storage solutions based on data size, with faster reading and writing functions offering measurable gains over base R defaults.
Because R is single-threaded, delegating long-running tasks to separate processes using packages such as promises or shiny.worker can prevent one user's activity from blocking others, resulting in a smoother concurrent experience. Understanding Shiny's scoping rules allows developers to share objects efficiently across sessions where appropriate, while keeping user-specific data private.
Reducing reliance on renderUI in favour of update functions and leveraging JavaScript where possible minimises unnecessary communication between the browser and the server. Caching repeated, resource-heavy operations using either the memoise package or Shiny's built-in bindCache mechanism can significantly reduce rendering times, particularly when outputs depend on a finite set of input combinations.
Serving an application via a platform that supports multiple concurrent R processes improves scalability further. Finally, profiling tools such as profvis help identify which specific functions are consuming the most time, enabling developers to focus optimisation efforts precisely where they are needed rather than relying on guesswork.
16:48, 2nd July 2021
Code performance in R: Parallelization
Parallelisation in R allows multiple independent tasks to run simultaneously across processor cores, potentially reducing computation time significantly. By default, R uses only one core, but the parallel package enables multi-core processing. On Linux and Mac systems, this is achieved by replacing the standard lapply function with mclapply, which distributes tasks across available cores.
On Windows, the process is more involved, requiring the creation of a cluster using makeCluster, running computations with parLapply and then closing the cluster with stopCluster. Windows also differs in that each parallel process requires its own copy of data in working memory, rather than sharing a single copy, and each process starts in an empty environment, meaning objects must be manually exported before use.
Parallelisation is not always beneficial, as the initialisation process itself takes time, making it counterproductive for fast tasks but worthwhile for those running for minutes or longer. The optimal number of cores to use depends on available working memory and whether the computer needs to remain usable for other tasks during computation.
09:19, 30th June 2021
Common Format and MIME Type for Comma-Separated Values (CSV) Files
RFC 4180, published in October 2005 by Yakov Shafranovich, formally documents the Comma-Separated Values (CSV) format and registers the associated MIME type text/csv with IANA. Although CSV had long been used for exchanging and converting data between spreadsheet programmes, no formal specification had previously existed, leading to inconsistent implementations across different systems.
The format structures data in records separated by line breaks, with fields within each record divided by commas, and allows for an optional header line identifying field names. Fields may be enclosed in double quotes, which is required when a field contains commas, line breaks or double quotes, with any double quote appearing within a quoted field escaped by a preceding double quote.
The registered MIME type supports optional parameters for character set encoding and header presence, defaults to US-ASCII and uses CRLF for line breaks. Security considerations note that whilst CSV data are generally passive, there is a theoretical risk of malicious binary data being embedded to exploit buffer overruns in processing programmes, and that private data may be inadvertently shared through the format.
09:26, 27th June 2021
Create and Preview RMarkdown Documents with QBit Workspace
RMarkdown is a document format rooted in the concept of Literate Programming, a paradigm introduced by Donald Knuth that combines code outputs with written content. While it is widely associated with the R programming community and the RStudio IDE, it supports a broad range of language engines, with version 1.33 of the knitr package offering 44 options including Python, SQL, Julia and many others.
Through the pandoc document converter, RMarkdown can produce a variety of output formats, such as static HTML files, PDF documents generated via LaTeX, Microsoft Word documents, PowerPoint presentations and flexdashboard layouts, all configurable through the YAML header. QBit Workspace allows users to author and preview RMarkdown documents directly in a browser, with an instant preview feature in the Viewer pane designed to accelerate the development process and support the creation of all the aforementioned output formats.
14:49, 25th June 2021
Comparing data frames in R can be achieved using several packages, each offering different levels of detail. The dplyr package provides a straightforward approach through its all_equal function, which returns TRUE when two data frames are identical and describes the specific row differences when they are not. The arsenal package offers a more comprehensive comparison via its comparedf function, producing detailed summaries that highlight differing variables, unequal values and observations that appear in one data frame but not the other. The diffdf package similarly identifies and reports differences between data frames, flagging unequal values by variable and row number.
These tools handle various scenarios, including comparing identical data frames, those with differing values and those with different numbers of rows altogether. Of the three approaches, dplyr is generally considered the most accessible for quick comparisons.
12:37, 24th June 2021
Top 5 tricks to make Matplotlib plots look better
Creating visually appealing data visualisations is an important skill for data scientists, and several straightforward techniques can significantly improve the quality of graphs produced using Python libraries such as Matplotlib and Seaborn. Applying a predefined plot theme, such as ggplot or one of the many Seaborn styles, instantly changes the overall look and feel of a chart with minimal effort.
Adjusting the colours of individual bars or lines, drawing from a library of over 950 named colours, allows specific data points to be highlighted through contrast. Changing the font family ensures that charts blend cohesively with the typography used in a wider presentation, while Seaborn's set context feature scales all visual elements, including fonts and titles, to suit different display settings such as paper, poster or talk formats. Finally, applying a colour palette to a plot unifies its tones and creates a more polished, harmonious appearance compared to using default, uncoordinated colours.
12:36, 24th June 2021
Choosing Colormaps in Matplotlib
Matplotlib offers a wide range of built-in colormaps, organised into several categories to suit different types of data. Selecting the right one depends on factors such as whether the data has a natural ordering, a critical midpoint value or repeating endpoints, as well as any conventions expected by the intended audience.
Perceptually uniform colormaps, in which equal steps in data correspond to equal perceived steps in colour, are generally the best choice, since the human brain responds more reliably to changes in lightness than to changes in hue. Sequential colormaps are suited to ordered data, diverging colormaps work well when the data varies around a meaningful central value, cyclic colormaps are appropriate for values that wrap around at their endpoints and qualitative colormaps are used for unordered categorical data.
A separate miscellaneous category includes colormaps designed for specific purposes, such as topographic or depth visualisation. Lightness values are also important when considering how a plot will appear when printed in greyscale, as colormaps that increase monotonically in lightness tend to reproduce more clearly, whereas those with irregular lightness patterns can result in unreadable output. Awareness of colour vision deficiencies is also advisable, and avoiding colormaps that combine red and green reduces the risk of problems for a significant portion of viewers.
12:15, 24th June 2021
Rotate Tick Labels in Matplotlib
Rotating axis labels in Matplotlib can be achieved through several methods, applicable to both the X and Y axes. At the figure level, plt.xticks() and plt.yticks() allow rotation to be set directly, while at the axes level, options include using ax.set_xticklabels() or ax.set_yticklabels(), iterating over tick labels and applying tick.set_rotation() to each, or using ax.tick_params() with a labelrotation argument. It is important to call plt.draw() before accessing tick labels when working at the axes level, as labels are only populated after the plot is drawn. For plots displaying dates, which often overlap and become unreadable without adjustment, Matplotlib provides the fig.autofmt_xdate() and fig.autofmt_ydate() functions as a convenient alternative to manual rotation.