Skip to content

Commit

Permalink
Merge pull request JuliaPlots#111 from mkborregaard/deprecate_old_Dat…
Browse files Browse the repository at this point in the history
…aFrame_syntax

Deprecate old DataFrames syntax
  • Loading branch information
mkborregaard authored Nov 27, 2017
2 parents 035058f + 641c1e0 commit 7d6adc0
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 490 deletions.
4 changes: 0 additions & 4 deletions src/StatPlots.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import Loess

@shorthands cdensity

export groupapply
export get_groupederror
export @df

include("df.jl")
Expand All @@ -34,8 +32,6 @@ include("violin.jl")
include("hist.jl")
include("marginalhist.jl")
include("bar.jl")
include("shadederror.jl")
include("groupederror.jl")



Expand Down
82 changes: 8 additions & 74 deletions src/dataframes.jl
Original file line number Diff line number Diff line change
@@ -1,75 +1,9 @@

# handle grouping by DataFrame column
function Plots.extractGroupArgs(group::Symbol, df::AbstractDataFrame, args...)
Plots.extractGroupArgs(collect(df[group]))
end

# if it's one symbol, set the guide and return the column
function handle_dfs(df::AbstractDataFrame, d::KW, letter, sym::Symbol)
get!(d, Symbol(letter * "guide"), string(sym))
processDFarg(df, sym)
end

# if it's one symbol, set the guide and return the column
function handle_dfs(df::AbstractDataFrame, d::KW, letter, expr::Expr)
get!(d, Symbol(letter * "guide"), replace(string(expr), ":", ""))
processDFarg(df, expr)
end

# if it's an array of symbols, set the labels and return a Vector{Any} of columns
function handle_dfs(df::AbstractDataFrame, d::KW, letter, syms::AbstractArray)
get!(d, :label, reshape(Symbol.(syms), 1, length(syms)))
processDFarg(df, syms)
end

# for anything else, no-op
function handle_dfs(df::AbstractDataFrame, d::KW, letter, anything)
anything
end

hcat_extend(a::AbstractArray, b::Number) = hcat(a, fill(b, size(a,1)))
hcat_extend(a::Number, b::AbstractArray) = hcat(fill(a, size(b,1)), b)
hcat_extend(a, b) = hcat(a, b)

# allows the passing of expressions including DataFrame columns as symbols
processDFarg(df::AbstractDataFrame, sym::Symbol) = haskey(df, sym) ? collect(df[sym]) : sym
function processDFarg(df::AbstractDataFrame, syms::AbstractMatrix)
ret = processDFcol(df, syms[:,1])
for i in 2:size(syms, 2)
tmp = processDFcol(df, syms[:,i])
ret = hcat_extend(ret, tmp)
end
ret
end

processDFarg(df::AbstractDataFrame, expr::Expr) = eval(processDFsym(df, expr))
processDFarg(df::AbstractDataFrame, anything) = anything

processDFcol(df, col::AbstractArray{T}) where {T <: Union{Expr, Symbol}} = processDFarg(df, col[1])
processDFcol(df, anything) = anything

# the processDFsym! functions work with expressions and pass results to processDFarg for final eval - this to allow recursion without eval
processDFsym(df::AbstractDataFrame, s::Symbol) = haskey(df,s) ? :(collect($(df)[$s])) : :($s)
processDFsym(df::AbstractDataFrame, s::QuoteNode) = haskey(df,s.value) ? :(collect($(df)[$s])) : :($s)
processDFsym(df::AbstractDataFrame, s) = :($s)

function processDFsym(df::AbstractDataFrame, expr::Expr)
arg = map(x->processDFsym(df,x), expr.args)
ret = copy(expr)
ret.args = arg
return ret
end


# if a DataFrame is the first arg, lets swap symbols out for columns
@recipe function f(df::AbstractDataFrame, args...)
# if any of these attributes are symbols, swap out for the df column
for k in (:fillrange, :line_z, :marker_z, :markersize, :ribbon, :weights, :xerror, :yerror, :hover, :bar_width, :fill_z, :fillalpha, :fillcolor, :linealpha, :linecolor, :linestyle, :linewidth, :markeralpha, :markercolor, :markershape, :markerstrokecolor, :series_annotations, :seriesalpha, :seriescolor)
if haskey(plotattributes, k)
plotattributes[k] = processDFarg(df, plotattributes[k])
end
end

# return a list of new arguments
tuple(Any[handle_dfs(df, plotattributes, (i==1 ? "x" : i==2 ? "y" : "z"), arg) for (i,arg) in enumerate(args)]...)
end
# deprecate the old DataFrame syntax
@recipe f(df::AbstractDataFrame, args...) = error("""
The `plot(df, args...)` syntax for plotting DataFrames has been deprecated.
Instead use the @df macro, e.g.
mydata = DataFrame(a = 1:10, b = 10*rand(10), c = 10 * rand(10))
@df mydata plot(:a, [:b :c], colour = [:red :blue])
"""
)
Loading

0 comments on commit 7d6adc0

Please sign in to comment.