--- title: "Getting started" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{Getting started} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r, include = FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) ``` ```{css, echo=FALSE} .custom_note { border: solid 3px #0b788e; background-color: #08505e; padding: 5px; margin-bottom: 10px; border-radius: 3px; } .custom_note > p, .custom_note > p > code { color: white; background: #08505e; } .custom_note > p > code > a:any-link { text-decoration-color: white; } ``` ## Eager and lazy evaluation Before we start writing code, it is important to understand the data structures in `polars` and hence in `tidypolars`. To use `tidypolars`, you need to import data as Polars `DataFrame`s or `LazyFrame`s. A `DataFrame` is very similar to the standard R `data.frame` (or `tibble` in the `tidyverse`). All functions that are applied to a `DataFrame` are **eagerly** evaluated. This means that they are executed one after the other, without knowing where in the data pipeline they are located. Therefore, applying a function on a `DataFrame` returns another `DataFrame` that you can directly explore. A `LazyFrame`, on the other hand, doesn't immediately run the functions applied to it. Instead, the data pipeline is built but isn't executed until some specific functions are called (see below). This is **lazy** evaluation, and the advantage of this approach is that it allows for *query optimizations*.
Eager vs Lazy: a brief example
Suppose you have some data on several countries and several years. You might want to sort the data by country and year, but you are only interested in a subset of countries.
If you sort the data and then filter it, you may waste some time and energy as sorting is much slower than filtering. But keeping track of the optimal order of operations is hard.
Using a LazyFrame allows to bypass that: before the query is executed, it is optimized in various ways. In this case, polars detects that a filter is called after a sort and rearranges the code to run the filter as early as possible and the sort afterwards. This kind of optimization is not possible with a DataFrame, since all functions are immediately evaluated.
From R to Polars
In some examples or some tutorials, the functions as_polars_df()
and as_polars_lf()
are sometimes used to convert an existing R
data.frame to a Polars DataFrame or LazyFrame. Those are merely convenience
functions to quickly convert an existing dataset to Polars, which is
useful for showcase purposes. However, this conversion from R to Polars has
some cost and it hurts the performance.
In real-life usecases, be sure to load
the data with the read_\*()
or the scan_\*()
functions
mentioned above.
Evaluate a lazy query
Several functions trigger the evaluation of a lazy query: `compute()`, `collect()`, `as.data.frame()`, and `as_tibble()`. If you want to return a Polars DataFrame, use `compute()`. If you want to return a standard R data.frame, for example to use it in statistical analysis, use any of the three other functions. Be aware that if the dataset is too big compared to your available memory, this will crash the R session.