# Using @filter of Tidier package inside a function

**URL:** <https://discourse.julialang.org/t/using-filter-of-tidier-package-inside-a-function/127540>\
**Category:** General Usage\
**Tags:** question\
**Created:** [March 31, 2025, 9:38am UTC](https://discourse.julialang.org/t/using-filter-of-tidier-package-inside-a-function/127540 "2025-03-31T09:38:16Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alf/32/51653_2.png) [@Alf](https://discourse.julialang.org/u/Alf)\
**Post date:** [March 31, 2025, 9:38am UTC](https://discourse.julialang.org/t/using-filter-of-tidier-package-inside-a-function/127540/1 "2025-03-31T09:38:16Z")

</div>

Hi!  
I rencently discovered the package Tidier, but I having trouble to use it inside a function. For instance,

```julia
using AlgebraOfGraphics
using DataFrames

# Example function that filters a DataFrame `df` for rows where the column `col`
# (passed as a Symbol) satisfies some condition (here, greater than a threshold)
function filter_by_threshold(df::DataFrame, col::Symbol, threshold)
    @filter(df, col > threshold)
end

# Sample data
df = DataFrame(a = 1:10, b = 11:20)

# Using the function to filter rows where column :a is greater than 5
filtered_df = filter_by_threshold(df, :a, 5)
println(filtered_df)

```

Gives the error

```julia
ERROR: ArgumentError: column name :col not found in the data frame

```

How can I pass the column symbol to @filter?

---

<div class="post-metadata">

**Author:** ![eteppo](https://avatars.discourse-cdn.com/v4/letter/e/90db22/32.png) [@eteppo](https://discourse.julialang.org/u/eteppo)\
**Post date:** [March 31, 2025, 10:16am UTC](https://discourse.julialang.org/t/using-filter-of-tidier-package-inside-a-function/127540/2 "2025-03-31T10:16:45Z")

</div>

Hey! See [Interpolation - TidierData.jl](https://tidierorg.github.io/TidierData.jl/latest/examples/generated/UserGuide/interpolation/). You need to use `@eval @filter(df, $col > threshold)` (currently recommended but long-term on the way out per @kdpsingh below) or `@filter(df, !!col > threshold)`.

---

<div class="post-metadata">

**Author:** ![Alf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alf/32/51653_2.png) [@Alf](https://discourse.julialang.org/u/Alf)\
**Post date:** [March 31, 2025, 2:36pm UTC](https://discourse.julialang.org/t/using-filter-of-tidier-package-inside-a-function/127540/3 "2025-03-31T14:36:35Z")

</div>

I hadn’t heard about Interpolation.  
Thanks!

---

<div class="post-metadata">

**Author:** ![kdpsingh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kdpsingh/32/215018_2.png) [@kdpsingh](https://discourse.julialang.org/u/kdpsingh)\
**Post date:** [March 31, 2025, 5:48pm UTC](https://discourse.julialang.org/t/using-filter-of-tidier-package-inside-a-function/127540/4 "2025-03-31T17:48:00Z")

</div>

Great answer here by @eteppo.

I’ll add that the reason TidierData works like this is that all variables in macros operate in “data frame scope.” So when you reference `col`, by default TidierData assumes that you are referring to a column name named `col`.

If you want to refer to a value outside of the data frame, you have to use interpolation. TidierData was initially built with `!!` (bang-bang) as a lazy interpolation operator. Any variable you prefix with `!!` is assumed to refer to a value outside of the data frame. So in your example, `@filter(df, !!col > threshold)` should also work.

However, there are some places where `!!` currently doesn’t work because the TidierData parsing engine is in need of a refresh. In the meantime, the `@eval`-based solution is the recommended approach if the `!!` approach doesn’t work (although I suspect it will work for your use case).

The main downside to the `@eval` approach is that it introduces a slight compilation delay. Thus, in the long run, the goal will be to remove the need for `@eval`.
