hughjonesd / huxtable

An R package to create styled tables in multiple output formats, with a friendly, modern interface.
http://hughjonesd.github.io/huxtable
Other
321 stars 28 forks source link

Conditional formatting - wrongly applying format to top row #84

Closed awechsler1 closed 6 years ago

awechsler1 commented 6 years ago

Hi David,

I emailed you about this earlier, but here's a little more explanation of what exactly is going on:

Let's say my initial data set consists of the following columns: group, id, count, rank, coefficient, probability. I am then making a huxtable with distinct ids as the rows and with the columns labeled as the "effect" for each group - within each table cell, I have a + or - depending on the coefficient. I then use the following code to loop through each group name within the huxtable and format the table accordingly: huxtable_name -> huxtable_name %>% set_text_color(where(as.numeric(unlist(huxtable_name[,paste(group_name, "Count", sep = "_")])) < 20), which(colnames(huxtable_name) == paste(group_name, "Effect", sep = "_")), "gray") %>% set_text_color(where(as.numeric(unlist(huxtable_name[,paste(group_name, "Rank", sep = "_")])) <= 5.), which(colnames(huxtable_name) == paste(group_name, "Effect", sep = "_")), "red") %>% set_background_color(where(as.numeric(unlist(huxtable_name[,paste(group_name, "Prob", sep = "_")])) < .05), which(colnames(huxtable_name) == paste(group_name, "Effect", sep = "_")), "light blue")

Unfortunately, what ends up happening is that in the very top row sometimes, the + or - sign for various groups will turn red even if that particular id is not in the top 5 rank...but if there is an id further down that same column that does have a top 5 rank. Everything else works perfectly, and the code itself doesn't seem wrong; when checking the boolean statement for that top row, the TRUE/FALSE values align with whether or not the rank is less than 5, but the formatting seems to happen anyway.

Let me know if any of this isn't clear!

hughjonesd commented 6 years ago

Could you provide a self-contained example that I can run? Provide toy data with dput. My first thought is that you are trying to use where to select rows, but where returns a matrix and should be used only in the 3-argument form of set_* calls. If so, this is a bug in the code and not in huxtable. But I may be wrong.

awechsler1 commented 6 years ago

This is some sample data - the code seems to work perfectly everywhere else. huxtable_example.txt

hughjonesd commented 6 years ago

Please could you provide a minimal, self-contained example? I tried to run the code in the text file and got:

Error: unexpected '<' in:
"        "Group_7_Effect"), class = c("data.table", "data.frame"), row.names = c(NA, 
          -48L), .internal.selfref = <"

In particular, I am more likely to be able to help if it is clear that the error can be reproduced in a simple example that should obviously work (e.g. not using where in the 4-argument form of a set_ call).

For reference on how to provide a MCVE, see https://stackoverflow.com/help/mcve and https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example

hughjonesd commented 6 years ago

Closing. Please reopen if you still feel there is a bug!