Tidier ! ! interpolation of external variables

In the latest version of TidierData.jl, we now recommend using native Julia interpolation rather than the !! interpolation as was recommended in earlier versions. This is because native Julia interpolation handles all of the edge cases, which for various parsing reasons are hard to make work in all situations.

Native Julia interpolation in expressions usually works via simple prefixing with $. However, TidierData.jl relies on “non-standard evaluation,” which means that column names are bare and not symbols (i.e., we refer to the column as x rather than :x). So as a result, we can’t simply interpolate a symbol into an expression – we have to evaluate the symbol. The main takeaway is that in TidierData.jl, any expression that includes a $ also needs to be preceded with an @eval.

using Tidier

df = DataFrame(x = 1:5)
y = 5
df2 = DataFrame(y = 5)

@eval @filter(df, x != $y)
4×1 DataFrame
 Row │ x     
     │ Int64 
─────┼───────
   1 │     1
   2 │     2
   3 │     3
   4 │     4

This also works with df2.y.

@eval @filter(df, x != $df2.y)
4×1 DataFrame
 Row │ x     
     │ Int64 
─────┼───────
   1 │     1
   2 │     2
   3 │     3
   4 │     4

And if you’re using a chain, you need to prefix the entire chain with an @eval. For example:

@eval @chain df @filter(x != $df2.y)
4×1 DataFrame
 Row │ x     
     │ Int64 
─────┼───────
   1 │     1
   2 │     2
   3 │     3
   4 │     4

The @eval does make the whole expression a bit verbose, but that’s the cost of our using non-standard evaluation.

3 Likes