3 min read

The package {patch}: Code Completion

What is this about

Code completion is a major productivity tool that is made available to us within the RStudio IDE. This helps us complete various object and variable names while writing R code. This looks something as seen in the picture below (Yes! functions are objects too.).

Code Completion in RStudio

Most of the people who have been using RStudio IDE, even for for a little while, would be familiar with this feature. However, a limitation is that the code completion will not aid further when one reaches a certain point, consider this for instance. penguins %>% filter(species == ). It would be magical if the one could be provided with all the values the species column takes in the penguins data set.

This is now possible with a new package, in development, by Indranil called patch.

What does {patch} do

If we continue with our example, penguins %>% filter(species == )

The {patch} package will allow code completion after one has reached this point. It will prompt with all unique values of the variable species.

This is how it looks.

Code Completion {patch}

Pressing the key after the == sign in line 12 will prompt the user with all possible values of the variable species takes.

This is magical. I say this for the following reasons.

  • If you are like me, prone to making spelling errors. This is a life saver.
  • Save time by reducing instance of copy pasting.
  • One less thing to check when reading to debug your code.

However, I have two questions I wonder about.

  • Does this work with variables that are of character class?
  • Does this work with variables that have values in Devnagri Script?

To check this I shall do a little test. But before that, a little thing on installing the package.


Loading packages that are needed

source(system.file("embedded","usecases.R",package = "patch"))
## These are meant for R-Studio
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##     filter, lag
## The following objects are masked from 'package:base':
##     intersect, setdiff, setequal, union
library(tibble) # to create small tibble to carry out a little test
## Warning: package 'tibble' was built under R version 4.0.3

One need not explicitly load the {patch} package.

Now, back to the little test.

Creating a small tibble.

  var_factor = as.factor(
    c("biryani", "Kebab", "jalebi", "rajma")
  var_char = c("raita", "pyaaz", "imarti", "nimbu"),
  var_devnagri = c("सालन","हरी चटनी", "रबडी", "चावल")
) -> food_condiment

We have a table of three columns. The first column is of class factor and the other two are of class character. The third one is in devnagri script.

We know that the code completion by {patch} works on variables that are pf class factors. Following clip shows that it works on variable of character class and/or in devnagri script.

food_condiment %>% 
  filter(var_char == "imarti", var_devnagri == "रबडी") %>%