Press "Enter" to skip to content

Category: R

Testing API Packages in R

Jamie Owen does some testing:

This blog post is a follow on to our API as a package series, which looks to expand on the topic of testing {plumber} API applications within the package structure leveraging {testthat}. As a reminder of the situation, so far we have an R package that defines functions that will be used as endpoints in a {plumber} API application. The API routes defined via {plumber} decorators in inst simply map the package functions to URLs.

Jamie covers a lot of testing ground in that post as well, so check it out.

Comments closed

Using Google Fonts on RMarkdown Pages

Thomas Williams has a change of font:

Setting a different font is a minor tweak to an R Markdown file that might help it fit better with a corporate or company look. Google Fonts is an industry-accepted method to reference fonts on web pages and can be used with R Markdown rendered by Shiny. There’s no need to download anything while developing the R Markdown file in RStudio, or viewing it in a web browser from a Shiny server.

I’ve recently used this technique to integrate the “Atkinson Hyperlegible” font, a font with “…greater legibility and readability for low vision readers…” from https://brailleinstitute.org/freefont. Since Shiny themes already specify a font, the steps below show how to override that with the Google Font, demo’d with the spacelab Shiny theme.

Click through to see how you can use this font, as well as others, in Shiny apps.

Comments closed

Parameter Reloading in RMarkdown

Thomas Williams wants to improve the user experience:

Recently I needed to reload a parameter, without reloading the page. The parameter was bound to a data frame, where end-users selected a value and then I looked up other fields in the data frame further down the page (for example, a name was selected, but I wanted the identifier from the same record). It wasn’t exactly intuitive, so here’s how I did it.

Click through for code and explanation.

Comments closed

Tips on Logging in R Packages

Jamie Owen continues a series on building a package around an API:

Part 1 of this series laid out some ideas for how one might structure a {plumber} application as an R package, inspired by solutions such as {golem} and {leprechaun} for {shiny}. In this installment of the series we look at adding some functions to our package that will take care of logging as our application runs. If you haven’t already, we recommend reading the first installment of this series as the example package created for that post will form the basis of the starting point for this one.

Read the whole thing.

Comments closed

Survival Analysis Model Explanations with survex

Mikolaj Spytek promotes an R package:

You can learn about it in this blog, but long story short, survival models (most often) predict a survival function. It tells us what is the probability of an event not happening until a given time t. The output can also be a single value (e.g., risk score) but these scores are always some aggregates of the survival function and this naturally leads to a loss of information included in the prediction.

The complexity of the output of survival models means that standard explanation methods cannot be applied directly.

Because of this, we (I and the team: Mateusz KrzyzińskiHubert Baniecki, and Przemyslaw Biecek) developed an R package — survex, which provides explanations for survival models. We hope this tool allows for more widespread usage of complex machine learning survival analysis models. Until now, simpler statistical models such as Cox Proportional Hazards were preferred due to their interpretability — vital in areas such as medicine, even though they were frequently outperformed by complex machine learning models.

Read on to dive into the topic. H/T R-Bloggers.

Comments closed

Keyboard Focus on Controls in Shiny

Thomas Williams wants to take control:

It is possible – though not the default – set set keyboard focus on load to a control in an R Markdown web page rendered with Shiny.

Setting keyboard focus when an R Markdown page is loaded is beneficial to users, who can start interacting with the page without having to first click the control.

Click through to learn how. This is one of the biggest losses we have in the GUI era: that you have a mouse necessitates using the mouse for everything. But that’s a “get off my lawn” rant for another day.

Comments closed

Setting a Loading Message for a DataTable in RMarkdown

Thomas Williams wants you to wait patiently:

Waits are inevitable, whether getting data from an API or database, or manipulating data in an interactive R Markdown document. Showing a “loading” or “updating” message is a beneficial incremental improvement to users’ experience.

The code at https://github.com/thomasswilliams/r-markdown-snippets/blob/main/loading-message-datatable.Rmd, when run from RStudio, demonstrates using CSS and pseudo-elements to display text in a DataTables (DT package).

Read on to see an example of how to use this.

Comments closed

Structuring an API Project in R

Jamie Owen begins a series on building APIs as R packages:

At Jumping Rivers we were recently tasked with taking a prototype application built in {shiny} to a public facing production environment for a public sector organisation. During the scoping exercise it was determined that a more appropriate solution to fit the requirements was to build the application with a {plumber} API providing the interface to the Bayesian network model and other application tools written in R.

When building applications in {shiny} we have for some time been using the “app as a package” approach which has been popularised by tools like {golem} and {leprechaun}, in large part due to the convenience that comes with leveraging the testing and dependency structure that our R developers are comfortable with in authoring packages, and the ease with which one can install and run an application in a new environment as a result. For this project we looked to take some of these ideas to a {plumber} application. This blog post discusses some of the thoughts and resultant structure that came as a result of that process.

Read on for the first post in the series, dealing with some of the prep work.

Comments closed

Declaring a Minimum R Version in Packages

Hugo Gruson and Maelle Salmon show how to set a minimum version of R itself in a package:

There have been much talk and many blog posts about R package dependencies. Yet, one special dependency is more rarely mentioned, even though all packages include it: the dependency on R itself. The same way you can specify a dependency on a package, and optionally on a specific version, you can add a dependency to a minimum R version in the DESCRIPTION file of your package. In this post we shall explain why and how.

Read on for that explanation, as well as a lot of depth on why you might choose a particular R version, popular R versions (at least by number of packages), and what some of the largest maintainers do.

Comments closed