Git Product home page Git Product logo

Comments (3)

markfairbanks avatar markfairbanks commented on July 19, 2024 1

@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.

markfairbanks avatar markfairbanks commented on July 19, 2024

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.

markfairbanks avatar markfairbanks commented on July 19, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.