Skip to content

knit print polars DataFrame

Description

Mimics Python Polars’ NotebookFormatter for HTML outputs.

Usage

## S3 method for class 'polars_data_frame'
knit_print(x, ...)

# S3 method for class 'polars_series'
knit_print(x, ...)

Arguments

x A polars object
Additional arguments passed to the S3 method. Currently ignored, except two optional arguments options and inline; see the references below.

Details

Outputs HTML tables if the output format is HTML and the document’s df_print option is not “default” or “tibble”.

Or, the output format can be enforced with R’s options function as follows:

  • options(polars.df_knitr_print = “default”) for the default print method.
  • options(polars.df_knitr_print = “html”) for the HTML table.

Value

x invisibly or knit_asis object.

Examples

library("polars")


# Using the default print method
withr::with_options(
  list(polars.df_knitr_print = "default"),
  knitr::knit_print(as_polars_df(mtcars))
)
#> shape: (32, 11)
#> ┌──────┬─────┬───────┬───────┬───┬─────┬─────┬──────┬──────┐
#> │ mpg  ┆ cyl ┆ disp  ┆ hp    ┆ … ┆ vs  ┆ am  ┆ gear ┆ carb │
#> │ ---  ┆ --- ┆ ---   ┆ ---   ┆   ┆ --- ┆ --- ┆ ---  ┆ ---  │
#> │ f64  ┆ f64 ┆ f64   ┆ f64   ┆   ┆ f64 ┆ f64 ┆ f64  ┆ f64  │
#> ╞══════╪═════╪═══════╪═══════╪═══╪═════╪═════╪══════╪══════╡
#> │ 21.0 ┆ 6.0 ┆ 160.0 ┆ 110.0 ┆ … ┆ 0.0 ┆ 1.0 ┆ 4.0  ┆ 4.0  │
#> │ 21.0 ┆ 6.0 ┆ 160.0 ┆ 110.0 ┆ … ┆ 0.0 ┆ 1.0 ┆ 4.0  ┆ 4.0  │
#> │ 22.8 ┆ 4.0 ┆ 108.0 ┆ 93.0  ┆ … ┆ 1.0 ┆ 1.0 ┆ 4.0  ┆ 1.0  │
#> │ 21.4 ┆ 6.0 ┆ 258.0 ┆ 110.0 ┆ … ┆ 1.0 ┆ 0.0 ┆ 3.0  ┆ 1.0  │
#> │ 18.7 ┆ 8.0 ┆ 360.0 ┆ 175.0 ┆ … ┆ 0.0 ┆ 0.0 ┆ 3.0  ┆ 2.0  │
#> │ …    ┆ …   ┆ …     ┆ …     ┆ … ┆ …   ┆ …   ┆ …    ┆ …    │
#> │ 30.4 ┆ 4.0 ┆ 95.1  ┆ 113.0 ┆ … ┆ 1.0 ┆ 1.0 ┆ 5.0  ┆ 2.0  │
#> │ 15.8 ┆ 8.0 ┆ 351.0 ┆ 264.0 ┆ … ┆ 0.0 ┆ 1.0 ┆ 5.0  ┆ 4.0  │
#> │ 19.7 ┆ 6.0 ┆ 145.0 ┆ 175.0 ┆ … ┆ 0.0 ┆ 1.0 ┆ 5.0  ┆ 6.0  │
#> │ 15.0 ┆ 8.0 ┆ 301.0 ┆ 335.0 ┆ … ┆ 0.0 ┆ 1.0 ┆ 5.0  ┆ 8.0  │
#> │ 21.4 ┆ 4.0 ┆ 121.0 ┆ 109.0 ┆ … ┆ 1.0 ┆ 1.0 ┆ 4.0  ┆ 2.0  │
#> └──────┴─────┴───────┴───────┴───┴─────┴─────┴──────┴──────┘
# Returning HTML table
withr::with_options(
  list(polars.df_knitr_print = "html"),
  knitr::knit_print(as_polars_df(mtcars))
)
shape: (32, 11)
mpg cyl disp hp drat wt qsec vs am gear carb
f64 f64 f64 f64 f64 f64 f64 f64 f64 f64 f64
21.0 6.0 160.0 110.0 3.9 2.62 16.46 0.0 1.0 4.0 4.0
21.0 6.0 160.0 110.0 3.9 2.875 17.02 0.0 1.0 4.0 4.0
22.8 4.0 108.0 93.0 3.85 2.32 18.61 1.0 1.0 4.0 1.0
21.4 6.0 258.0 110.0 3.08 3.215 19.44 1.0 0.0 3.0 1.0
18.7 8.0 360.0 175.0 3.15 3.44 17.02 0.0 0.0 3.0 2.0
30.4 4.0 95.1 113.0 3.77 1.513 16.9 1.0 1.0 5.0 2.0
15.8 8.0 351.0 264.0 4.22 3.17 14.5 0.0 1.0 5.0 4.0
19.7 6.0 145.0 175.0 3.62 2.77 15.5 0.0 1.0 5.0 6.0
15.0 8.0 301.0 335.0 3.54 3.57 14.6 0.0 1.0 5.0 8.0
21.4 4.0 121.0 109.0 4.11 2.78 18.6 1.0 1.0 4.0 2.0