library(sicher)
# Basic type checking
x %:% Numeric %<-% 42
x <- "text" # Error!Error: Type error in 'x': Expected numeric, got string
Received: text
sicher is an R package that brings type safety to R programming, similar to TypeScript for JavaScript. It allows you to define types for your variables and enforce them at runtime, catching type errors early and making your code more robust and maintainable.
%:% and %<-%typed_function()Install the package and start using types immediately:
R is a dynamically typed language, which offers great flexibility but can lead to:
sicher addresses these issues by providing:
Suppose you write a function that calculates the average salary payout for employees. In a real company workflow, the data might come from, a CSV export from HR, manual editing in Excel, a database export and so on. Sometimes the numbers that are saved within those data sources can have invalid types (string for example):
If the inputs have a correct data type, then we don’t have any issue:
Now, imagine that our input as following:
Warning in mean.default(salaries): argument is not numeric or logical:
returning NA
[1] NA
Instead of getting an error, we get a warning and an NA value as a result. This is a very simple function but in a realistic example, you might have a script that uses thousands of lines of code and many functions to obtain a specific result, thus making this kind of debugging harder. We definitely expect more robustness from R.
Using sicher we can make sure that our data types remain valid and as we defined them.
Error: Type error in 'salaries': Expected numeric, got string of length 4
Received: [1800, 2300, 4000, 1222]
Note that when you define a type for a specific variable, that type is guarded throughout the execution of your code:
When working with Rest APIs, you want make sure that the data that you’re getting has a specific shape and type, consider the following API: https://jsonplaceholder.typicode.com/todos/1
It provides us with one object of the shape:
Using sicher you can, similar to the pydantic Python framework, define the expected structure of the response:
<type: {userId: numeric, id: numeric, title: string, completed: bool} >
Then when fetching the data, you can safely check that the response corresponds to the expected object:
$userId
[1] 1
$id
[1] 1
$title
[1] "delectus aut autem"
$completed
[1] FALSE
If the API for whatever reason started to produce invalid responses, sicher would catch it immediately:
Error: Type error in 'completed': Expected bool, got string
Received: yes
Error: Type error: Expected {userId: numeric, id: numeric, title: string, completed: bool}, got list
Details: Missing required field(s): completed (expected fields: userId, id, title, completed)
Received: list with fields: [userId, id, title, new_name_not_recognized]
The above URL returns only 1 object back, if we use the following URL https://jsonplaceholder.typicode.com/todos/ we get several (200) json objects back:
[1] 200
$userId
[1] 1
$id
[1] 1
$title
[1] "delectus aut autem"
$completed
[1] FALSE
Now how to set the type of a list of lists? using the ListOf function:
Now if we try to change the type of one element of the lists:
Error: Type error in 'full_response': Expected list<{userId: numeric, id: numeric, title: string, completed: bool}>, got list of length 200
Received: list of length 200
# Define data frame structure
SalesData <- create_dataframe_type(
col_spec = list(
date = String,
product = String,
quantity = Integer,
price = Numeric,
region = String
))
sales_df <- data.frame(
date = c("2024-01-01", "2024-01-02"),
product = c("Widget A", "Widget B"),
quantity = c(10, 5),
price = c(29.99, 49.99),
region = c("North", "South")
)
validated_sales %:% SalesData %<-% sales_dfError: Type error in 'quantity': Expected integer, got double of length 2
Received: [10, 5]
Integer, Double, Numeric, String, BoolList, DataFrame, FunctionAny, NullTypeA | TypeBNumeric[3] (exactly 3 elements)ListOf(ElementType)typed_function(fn, params, .return)Currently, install from Github with:
# 1. Annotate variables with types
name %:% String %<-% "Alice"
age %:% Numeric %<-% 30
# 2. Create custom types
PositiveNumber <- create_type("positive number",
function(x) is.numeric(x) && all(x > 0))
value %:% PositiveNumber %<-% 42
# 3. Use type modifiers
single_value %:% Scalar(String) %<-% "hello"
constant %:% Readonly(Numeric) %<-% 3.14
maybe_value %:% Optional(String) %<-% NULL
# 4. Create structured types
Person <- create_list_type(list(
name = String,
age = Numeric,
email = Optional(String)
))
person %:% Person %<-% list(
name = "Bob",
age = 25,
email = "bob@example.com"
)See how sicher catches type errors:
Error: Type error in 'x': Expected numeric, got string
Received: text
Error: Type error: Expected {name: string, age: numeric}, got list
Details: Missing required field(s): age (expected fields: name, age)
Received: list with fields: [name]