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
Passing a list of inputs parametrically #5
Comments
What about this? devtools::install_github("zeehio/condformat") # Custom discrete color values can be specified with a function. The function takes
# the whole column and returns a vector with the colours.
# Custom discrete color values can be specified with a function. The function takes
# the whole column and returns a vector with the colours.
color_pick <- function(column) {
sapply(column,
FUN = function(value) {
if (value < 4.7) {
return("red")
} else if (value < 6.0) {
return("yellow")
} else {
return("blue")
}
})
}
condformat(head(iris)) + rule_fill_discrete_("Sepal.Length", ~ color_pick(Sepal.Length), colours = identity) |
As requested #5. See the example in `?rule_fill_discrete_` (unreleased into CRAN)
Yea man.. that's excellent. But again we are specifying the column to color based on explicit names... even more complicated is the fact that within the same That's kind of the priority 1 I think, to create a function that allows for the rules to be applied to columns to be done so without hard-coding. One of these latest commits broke our simple (but hacky) solution:
And also the more complicated one that applies formatting on some columns based on some others:
Something about the way |
I believe now we have the best of both worlds: Both of your functions are working and we have a future-proof way of programming based on formulas. I believe you will find worth reading the examples in |
I think there's a problem with
It seems the threshold values aren't being applied? |
I fixed the examples in the stackoverflow answer but I missed that one in the package. There are two corrections needed:
This is the corrected example: # Use it programmatically:
color_column_larger_than_threshold <- function(x, column, threshold) {
condformat(x) +
rule_fill_discrete_(column,
expression=~ uq(as.name(column)) > uq(threshold))
}
color_column_larger_than_threshold(iris[c(1,51,101),], "Sepal.Length", 6.3)
Already fixed in master. Sorry for all the bugs and recent confusions. I am writing my PhD thesis and I am doing my best to cope with these projects on the few spare moments I have left 😓 |
No worries man, thanks for your efforts! I'm very happy to say that I am now able to apply my use case! Here's what I ended up doing, feel free to put as an example... or I can PR if u prefer.
Feel free to close issue! Y gracias loco... te pasaste. Suerte con tu tesis. |
I am not a friend of This would be my solution: helperfun <- function(data, cols_x, cols_y) {
stopifnot(length(cols_x) == length(cols_y))
rule_list <- lapply(seq_len(length(cols_x)), function(i) {
rule_fill_discrete_(columns = cols_x[i],
expression = ~ color_pick(uq(as.name(cols_y[i]))),
colours = identity)
})
Reduce(`+`, rule_list, init = condformat(data))
}
data(iris)
x <- c("Sepal.Length","Sepal.Width","Petal.Length","Petal.Width")
y <- c("Sepal.Length","Sepal.Length","Petal.Length","Petal.Length")
helperfun(iris, x, y) This example is one of those cases where library(lazyeval)
# Using a for loop:
# All the formulas in formulas_for will share the same environment,
# and after the for loop the value of `i` will be 3.
formulas_for <- list()
for (i in 1:3) {
formulas_for[[i]] <- ~ i + 1
}
# Using lapply:
formulas_lapply <- lapply(1:3, function(i) ~ i + 1)
# Each formula has its own environment because it belongs to a different function call
# This always returns i+1 because i evaluates to 3 in all the cases (shared i)
print(sapply(formulas_for, function(x) f_eval(x)))
# 4, 4, 4
# This works as I would like to because each formula is evaluated in its own environment:
print(sapply(formulas_lapply, function(x) f_eval(x)))
# 2, 3, 4 Gracias por las pruebas y por los ánimos con la tesis! |
Hi, take a look here: http://stackoverflow.com/questions/41402379/how-can-i-pass-character-strings-as-independent-parameters-after-a
Perhaps the functions being exposed by Gregor and by myself (Amit Kohli) could be useful inclusions to this packet.
The only addition I could think of could be to extend the function to accept inputs for thresolds and colors. Something like:
I'm close with this:
But there's two problems with my approach: 1) the parameter that decides the color is somehow Column 1 (in the xample above it's the Sepal.Length) rather than whatever I'm passing as y 2) This is still messy... I'm passing the sapply portion w/ the colors in the call, not in the
color.picker
function... which is messy and weird.I think this would be really cool if the
color.picker
function accepted a df as input, with column 1 being the thresholds, and column 2 the colors... and then passing that modifiedcolor.picker
function to themap2
command inCondFormatForInput2
.What do you think?
The text was updated successfully, but these errors were encountered: