Comments (3)
@talegari Just wanted to let you know that your example now works in the new version of tidytable up on CRAN (v0.5.0)
library(tidytable, warn.conflicts = FALSE)
iris_model <- rpart::rpart(Species ~ ., data = iris)
iris %>%
mutate.(pred = predict(iris_model, ., type = "class")) %>%
head()
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species pred
#> 1: 5.1 3.5 1.4 0.2 setosa setosa
#> 2: 4.9 3.0 1.4 0.2 setosa setosa
#> 3: 4.7 3.2 1.3 0.2 setosa setosa
#> 4: 4.6 3.1 1.5 0.2 setosa setosa
#> 5: 5.0 3.6 1.4 0.2 setosa setosa
#> 6: 5.4 3.9 1.7 0.4 setosa setosa
from tidytable.
Thanks for the clear example.
This is actually a data.table
issue with how it "quotes" user code when using their version of mutate (which tidytable
calls in the background).
Fortunately there are two workarounds! One using rlang
and one using a data.table
tool.
data.table solution
In this solution you use data.table's .SD
operator, which stands for "subset of data". It's basically data.table's shorthand for "current dataset".
iris_2 %>%
dt_mutate(., pred = predict(iris_2_model, .SD, type = "class")) %>%
head()
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species pred
#> <dbl> <dbl> <dbl> <dbl> <fct> <fct>
#> 1: 5.1 3.5 1.4 0.2 setosa setosa
#> 2: 4.9 3.0 1.4 0.2 setosa setosa
#> 3: 4.7 3.2 1.3 0.2 setosa setosa
#> 4: 4.6 3.1 1.5 0.2 setosa setosa
#> 5: 5.0 3.6 1.4 0.2 setosa setosa
#> 6: 5.4 3.9 1.7 0.4 setosa setosa
rlang solution
In this solution you "unquote" the "." using rlang's !!
operator, so that data.table
reads it correctly.
iris_2 %>%
dt_mutate(., pred = predict(iris_2_model, !!., type = "class")) %>%
head()
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species pred
#> <dbl> <dbl> <dbl> <dbl> <fct> <fct>
#> 1: 5.1 3.5 1.4 0.2 setosa setosa
#> 2: 4.9 3.0 1.4 0.2 setosa setosa
#> 3: 4.7 3.2 1.3 0.2 setosa setosa
#> 4: 4.6 3.1 1.5 0.2 setosa setosa
#> 5: 5.0 3.6 1.4 0.2 setosa setosa
#> 6: 5.4 3.9 1.7 0.4 setosa setosa
Hope this helps! If you have any questions let me know.
from tidytable.
@talegari I had to roll back this functionality in v0.5.2, as it was causing pretty large performance issues.
Note that this workaround will still work
from tidytable.
Related Issues (20)
- Release v0.10.1
- Tidyverse 2 update HOT 1
- Patch release v0.10.2
- `tidytable` fails with `ceiling_date()` function HOT 2
- `distinct` not working with lists HOT 2
- using `pick()` inside `count()` leads to an unhelpful error HOT 5
- deprecated notation HOT 1
- Bug: unnest() HOT 2
- Failure with dev glue HOT 3
- Release v0.11.0 HOT 1
- When two tidytables are subtracted, the class `tidytable` gets lost on the way. HOT 2
- weird error using testthat HOT 2
- exposing list_flatten HOT 1
- summarize with across behaves in a strange way and gives an error when I use lapply inside
- Renaming in `group_by()` can lead to grouping by nonexistent columns
- ifelse and if_else HOT 1
- pmap is not working for data frames HOT 1
- Error "mutate_rowwise()" several columns. HOT 2
- Group_by bug with purrr::reduce HOT 4
- pmap() doesn't name list elements using existing names HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tidytable.