Skip to content

Commit

Permalink
support for all basic operators, either with two columns or with one …
Browse files Browse the repository at this point in the history
…column and a symbol
  • Loading branch information
rdboyes committed Apr 7, 2024
1 parent 11cfafa commit df710ca
Showing 1 changed file with 96 additions and 5 deletions.
101 changes: 96 additions & 5 deletions src/transforms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -200,13 +200,104 @@ function make_add_const_function(constant::Real)
end

Base.:+(s1::Symbol, s2::Symbol) = [s1, s2] => AesTransform(add_cols_fn)
Base.:+(s1::Symbol, s2::Real) = s1 => AesTransform(make_add_const_function(s2))
Base.:+(s2::Real, s1::Symbol) = s1 => AesTransform(make_add_const_function(s2))
#import Base.:-
#import Base.:/
#import Base.:*
Base.:+(s1::Symbol, s2::Real) = [s1] => AesTransform(make_add_const_function(s2))
Base.:+(s2::Real, s1::Symbol) = [s1] => AesTransform(make_add_const_function(s2))

import Base.:-

function subtract_cols_fn(target::Symbol, source::Vector{Symbol}, data::DataFrame)
result = data[!, source[1]] .- data[!, source[2]]
return Dict{Symbol, PlottableData}(
target => PlottableData(
result,
identity,
nothing,
nothing
)
)
end

function make_subtract_const_function(constant::Real)
return function add_const_fn(target::Symbol, source::Vector{Symbol}, data::DataFrame)
result = data[!, source[1]] .- constant
return Dict{Symbol, PlottableData}(
target => PlottableData(
result, # get the column out of the dataframe
identity, # apply generic_fn to it
nothing,
nothing
)
)
end
end

Base.:-(s1::Symbol, s2::Symbol) = [s1, s2] => AesTransform(subtract_cols_fn)
Base.:-(s1::Symbol, s2::Real) = [s1] => AesTransform(make_subtract_const_function(s2))
Base.:-(s2::Real, s1::Symbol) = [s1] => AesTransform(make_subtract_const_function(s2))

import Base.:/

function divide_cols_fn(target::Symbol, source::Vector{Symbol}, data::DataFrame)
result = data[!, source[1]] ./ data[!, source[2]]
return Dict{Symbol, PlottableData}(
target => PlottableData(
result,
identity,
nothing,
nothing
)
)
end

function make_divide_const_function(constant::Real)
return function add_const_fn(target::Symbol, source::Vector{Symbol}, data::DataFrame)
result = data[!, source[1]] ./ constant
return Dict{Symbol, PlottableData}(
target => PlottableData(
result, # get the column out of the dataframe
identity, # apply generic_fn to it
nothing,
nothing
)
)
end
end

Base.:/(s1::Symbol, s2::Symbol) = [s1, s2] => AesTransform(divide_cols_fn)
Base.:/(s1::Symbol, s2::Real) = [s1] => AesTransform(make_divide_const_function(s2))
Base.:/(s2::Real, s1::Symbol) = [s1] => AesTransform(make_divide_const_function(s2))

import Base.:*

function multiply_cols_fn(target::Symbol, source::Vector{Symbol}, data::DataFrame)
result = data[!, source[1]] .* data[!, source[2]]
return Dict{Symbol, PlottableData}(
target => PlottableData(
result,
identity,
nothing,
nothing
)
)
end

function make_multiply_const_function(constant::Real)
return function add_const_fn(target::Symbol, source::Vector{Symbol}, data::DataFrame)
result = data[!, source[1]] .* constant
return Dict{Symbol, PlottableData}(
target => PlottableData(
result, # get the column out of the dataframe
identity, # apply generic_fn to it
nothing,
nothing
)
)
end
end

Base.:*(s1::Symbol, s2::Symbol) = [s1, s2] => AesTransform(multiply_cols_fn)
Base.:*(s1::Symbol, s2::Real) = [s1] => AesTransform(make_multiply_const_function(s2))
Base.:*(s2::Real, s1::Symbol) = [s1] => AesTransform(make_multiply_const_function(s2))


# tweaks
Expand Down

0 comments on commit df710ca

Please sign in to comment.