# Create temporary local namespace?

**URL:** <https://discourse.julialang.org/t/create-temporary-local-namespace/101227>\
**Category:** General Usage\
**Created:** [July 5, 2023, 7:56pm UTC](https://discourse.julialang.org/t/create-temporary-local-namespace/101227 "2023-07-05T19:56:34Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![hatmatrix](https://avatars.discourse-cdn.com/v4/letter/h/5f8ce5/32.png) [@hatmatrix](https://discourse.julialang.org/u/hatmatrix)\
**Post date:** [July 5, 2023, 7:56pm UTC](https://discourse.julialang.org/t/create-temporary-local-namespace/101227/1 "2023-07-05T19:56:35Z")

</div>

In R you can use the following syntax to access variables within a data frame.

```julia
df = data.frame(x = 1:10, y = 1)
z = with(df, x + y) # => [2, 3, ..., 11]

```

The `with` function is very powerful in that it also allows you to use it like lisp’s `let` macro:

```julia
z = with(list(x = 1:10, y = 1), x + y) # => [2, 3, ..., 11]

```

`with` operates within the context of an `environment` in R, which is like a namespace.

Is there anything similar in Julia that allows you to work with DataFrame columns like this, or bind variables to local symbols?

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [July 5, 2023, 9:09pm UTC](https://discourse.julialang.org/t/create-temporary-local-namespace/101227/2 "2023-07-05T21:09:34Z")

</div>

You can just use dot syntax to access the columns as vectors.

```julia
julia> using DataFrames

julia> df = DataFrame(x=1:10, y=1)
10×2 DataFrame
 Row │ x y
     │ Int64 Int64
─────┼──────────────
   1 │ 1 1
   2 │ 2 1
   3 │ 3 1
   4 │ 4 1
   5 │ 5 1
   6 │ 6 1
   7 │ 7 1
   8 │ 8 1
   9 │ 9 1
  10 │ 10 1

julia> z = df.x .+ df.y
10-element Vector{Int64}:
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11

```

or

```julia
julia> df.z = df.x .+ df.y
10-element Vector{Int64}:
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11

julia> df
10×3 DataFrame
 Row │ x y z
     │ Int64 Int64 Int64
─────┼─────────────────────
   1 │ 1 1 2
   2 │ 2 1 3
   3 │ 3 1 4
   4 │ 4 1 5
   5 │ 5 1 6
   6 │ 6 1 7
   7 │ 7 1 8
   8 │ 8 1 9
   9 │ 9 1 10
  10 │ 10 1 11

```

or

```julia
julia> transform!(df, [:x,:y] => ByRow(+) => :z)
10×3 DataFrame
 Row │ x y z
     │ Int64 Int64 Int64
─────┼─────────────────────
   1 │ 1 1 2
   2 │ 2 1 3
   3 │ 3 1 4
   4 │ 4 1 5
   5 │ 5 1 6
   6 │ 6 1 7
   7 │ 7 1 8
   8 │ 8 1 9
   9 │ 9 1 10
  10 │ 10 1 11

```

---

<div class="post-metadata">

**Author:** ![disberd](https://avatars.discourse-cdn.com/v4/letter/d/8edcca/32.png) [@disberd](https://discourse.julialang.org/u/disberd)\
**Post date:** [July 5, 2023, 9:30pm UTC](https://discourse.julialang.org/t/create-temporary-local-namespace/101227/3 "2023-07-05T21:30:46Z")

</div>

What about the `@with` macro from [DataFramesMeta](https://juliadata.github.io/DataFramesMeta.jl/stable/#with)?

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [July 5, 2023, 9:37pm UTC](https://discourse.julialang.org/t/create-temporary-local-namespace/101227/4 "2023-07-05T21:37:51Z")

</div>

Are you aware of `let` in Julia? [Documentation](https://docs.julialang.org/en/v1/base/base/#let), relevant manual [section](https://docs.julialang.org/en/v1/manual/variables-and-scoping/#Let-Blocks).

---

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [July 5, 2023, 10:00pm UTC](https://discourse.julialang.org/t/create-temporary-local-namespace/101227/5 "2023-07-05T22:00:44Z")

</div>

> [@hatmatrix](#):
>
> bind variables to local symbols

Taking your post’s title at face value, `let` blocks binds local variables to global instances in its header and makes a (temporary) local scope to run the block in. But that’s not really what R is doing there. R is doing non-standard evaluation, which works more like implicit macros on argument expressions; unlike Julia, R arguments are lazily evaluated and make its way into the function as an expression you can opt to keep as one long enough for metaprogramming. Restricting names to a data.frame’s namespace is just one use case; it could also be used to provide strings for plot labels instead of evaluating variables.

Note that `DataFramesMeta.@with` requires you to write column symbols `:x` differently from existing variables `x`. That’s purposely engineered for the distinction, it does not imply that symbols are provided to macros with that syntax; in fact a simple `@macroexpand x` only sees `:x` in its body. The choice to incorporate the `:x` syntax also seems intended to clarify that you’re accessing a column name; a lot of confusion comes from R’s standard vs nonstandard evaluation looking the same at the call site, even being turned on and off by an optional argument.

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [July 5, 2023, 10:01pm UTC](https://discourse.julialang.org/t/create-temporary-local-namespace/101227/6 "2023-07-05T22:01:59Z")

</div>

DataFramesMeta.jl works very well for this

```julia
julia> using DataFramesMeta, Statistics

julia> df = DataFrame(x = rand(10), y = rand(10) .* 100);

julia> @with df begin
           mean(:x .+ :y)
       end
45.7031031775563

```

Doesn’t work on arbitrary lists, though (equivalent in Julia would be a `Dict`, more or less)

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [July 5, 2023, 10:08pm UTC](https://discourse.julialang.org/t/create-temporary-local-namespace/101227/7 "2023-07-05T22:08:07Z")

</div>

You can use property destructuring for this:

```julia
julia> list = (x=1:10, y = 1);

julia> z = let (;x, y) = list
           x .+ y
       end
2:11

```

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [July 5, 2023, 10:38pm UTC](https://discourse.julialang.org/t/create-temporary-local-namespace/101227/8 "2023-07-05T22:38:39Z")

</div>

You can also use `@with` from @Mason’s StaticModules.jl package:

> **[GitHub - MasonProtter/StaticModules.jl: Static module-like namespaces for julia](https://github.com/MasonProtter/StaticModules.jl)**
>
> Static module-like namespaces for julia. Contribute to MasonProtter/StaticModules.jl development by creating an account on GitHub.

It works on any object that supports both `propertynames` and `getproperty`, which includes structs, named tuples, and dataframes.

Although since he didn’t mention it himself, perhaps he prefers not to promote that package. 🙂

---

<div class="post-metadata">

**Author:** ![hatmatrix](https://avatars.discourse-cdn.com/v4/letter/h/5f8ce5/32.png) [@hatmatrix](https://discourse.julialang.org/u/hatmatrix)\
**Post date:** [July 6, 2023, 9:52am UTC](https://discourse.julialang.org/t/create-temporary-local-namespace/101227/10 "2023-07-06T09:52:38Z")

</div>

Someone suggested your StaticModules package - this package is my favorite solution for the DataFrame problem [Create temporary local namespace? - #8 by CameronBieganek](https://discourse.julialang.org/t/create-temporary-local-namespace/101227/8). The destructuring bind you illustrate in your solution is also useful but then becomes cumbersome when you have a large number of columns in the DataFrame.

For the specific example I described, this is a very nice solution:

> using StaticModules  
> z = @with df (x + y)

---

<div class="post-metadata">

**Author:** ![hatmatrix](https://avatars.discourse-cdn.com/v4/letter/h/5f8ce5/32.png) [@hatmatrix](https://discourse.julialang.org/u/hatmatrix)\
**Post date:** [July 6, 2023, 9:53am UTC](https://discourse.julialang.org/t/create-temporary-local-namespace/101227/11 "2023-07-06T09:53:38Z")

</div>

This seems the most general solution. It doesn’t differentiate between column names and global variables as does DataFramesMeta but I assume decides symbol values based on scoping rules - this is also how R works as well.

---

<div class="post-metadata">

**Author:** ![hatmatrix](https://avatars.discourse-cdn.com/v4/letter/h/5f8ce5/32.png) [@hatmatrix](https://discourse.julialang.org/u/hatmatrix)\
**Post date:** [July 6, 2023, 9:59am UTC](https://discourse.julialang.org/t/create-temporary-local-namespace/101227/12 "2023-07-06T09:59:10Z")

</div>

Thanks for the `transform!` example - this is separately useful (similar to R’s `transform`/`mutate`/`within` functions). The dot syntax is of course the obvious solution but my objective was to avoid writing the DataFrame name for each variable - largely for readability.
