Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RFC] Establishing a Julian precedent for translating R functions that use delayed evaluation #472

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/DataFrames.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export # reconcile_groups,
rowsums,
rowvars,
save,
@select,
set_group,
set_groups,
SimpleIndex,
Expand Down Expand Up @@ -203,6 +204,7 @@ include("indexing.jl")
include("extras.jl")
include("RDA.jl")
include("dataframe_blocks.jl")
include("select.jl")

##############################################################################
##
Expand Down
27 changes: 27 additions & 0 deletions src/select.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
rewrite(s::Any, dfname::Symbol) = s

function rewrite(ex::Expr, dfname::Symbol)
if ex.head == :quote
return Expr(:ref, dfname, string(ex.args[1]))
else
newargs = Array(Any, length(ex.args))
for i in 1:length(ex.args)
newargs[i] = rewrite(ex.args[i], dfname)
end
return Expr(ex.head, newargs...)
end
end

function rewrite(n::QuoteNode, dfname::Symbol)
return Expr(:ref, dfname, string(n.value))
end

macro select(dfname, constraints)
cleanconstraints = rewrite(constraints, dfname)
return Expr(:ref,
esc(dfname),
esc(cleanconstraints),
Expr(:(:),
1,
Expr(:call, :size, esc(dfname), 2)))
end
20 changes: 20 additions & 0 deletions test/select.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
module TestSelect
using DataArrays
using DataFrames

df = DataFrame(A = 1:3, B = [2, 1, 2])
x = [2, 1, 0]

@select df :A .> 1
@select df :B .> 1
@select df :A .> x
@select df :B .> x
@select df :A .> :B
@select df sin(:A) .> cos(:B)
@select df cos(:A) .> sin(:B)
@select df cos(x) .> sin(:B) + 1
@select df sin(x) .> sin(:B) + 1
@select df sin(:A) .> sin(:B) + 1
@select df sin(:A) .> sin(:B)
@select df sin(:A) .> exp(cos(:B) - 1)
end