-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.Rmd
91 lines (66 loc) · 3.19 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
---
output: github_document
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
lgr::get_logger("mlr3")$set_threshold("warn")
```
# mlr3db
<!-- badges: start -->
[](https://github.com/mlr-org/mlr3db/actions/workflows/r-cmd-check.yml)
[](https://cran.r-project.org/package=mlr3db)
[](https://stackoverflow.com/questions/tagged/mlr3)
[](https://lmmisld-lmu-stats-slds.srv.mwn.de/mlr_invite/)
<!-- badges: end -->
Package website: [release](https://mlr3db.mlr-org.com/) | [dev](https://mlr3db.mlr-org.com/dev/)
Extends the [mlr3](https://mlr3.mlr-org.com/) package with a DataBackend to transparently work with databases.
Two additional backends are currently implemented:
* `DataBackendDplyr`: Relies internally on the abstraction of [dplyr](https://dplyr.tidyverse.org/) and [dbplyr](https://dbplyr.tidyverse.org/).
This allows working on a broad range of DBMS, such as SQLite, MySQL, MariaDB, or PostgreSQL.
* `DataBackendDuckDB`: Connector to [duckdb](https://cran.r-project.org/package=duckdb).
This includes support for Parquet files (see example below).
To construct the backends, you have to establish a connection to the DBMS yourself with the [DBI](https://cran.r-project.org/package=DBI) package.
For the serverless SQLite and DuckDB, we provide the converters `as_sqlite_backend()` and `as_duckdb_backend()`.
## Installation
You can install the released version of mlr3db from [CRAN](https://CRAN.R-project.org) with:
```{r, eval = FALSE}
install.packages("mlr3db")
```
And the development version from [GitHub](https://github.com/) with:
```{r, eval = FALSE}
# install.packages("devtools")
devtools::install_github("mlr-org/mlr3db")
```
## Example
### DataBackendDplyr
```{r}
library("mlr3db")
# Create a classification task:
task = tsk("spam")
# Convert the task backend from a in-memory backend (DataBackendDataTable)
# to an out-of-memory SQLite backend via DataBackendDplyr.
# A temporary directory is used here to store the database files.
task$backend = as_sqlite_backend(task$backend, path = tempfile())
# Resample a classification tree using a 3-fold CV.
# The requested data will be queried and fetched from the database in the background.
resample(task, lrn("classif.rpart"), rsmp("cv", folds = 3))
```
### DataBackendDuckDB
```{r}
library("mlr3db")
# Get an example parquet file from the package install directory:
# spam dataset (tsk("spam")) stored as parquet file
file = system.file(file.path("extdata", "spam.parquet"), package = "mlr3db")
# Create a backend on the file
backend = as_duckdb_backend(file)
# Construct classification task on the constructed backend
task = as_task_classif(backend, target = "type")
# Resample a classification tree using a 3-fold CV.
# The requested data will be queried and fetched from the database in the background.
resample(task, lrn("classif.rpart"), rsmp("cv", folds = 3))
```