1

A strange behavior:

data(iris)
library(condformat)
#library(knitr)
df <- iris[c(1:5,70:75, 120:125),]

cd <- condformat(df)

cd <- rule_text_bold(cd, 2, df[,2] == apply(df[,2:3], 1, FUN=min))
cd <- rule_text_bold(cd, 3, df[,3] == apply(df[,2:3], 1, FUN=min))

cd

Produces the correct output with the minimum of rows 2 and 3 in bold.

enter image description here

But when I switch to a for loop:

data(iris)
library(condformat)
#library(knitr)
df <- iris[c(1:5,70:75, 120:125),]

cd <- condformat(df)

for (j in 2:3){
  cd <- rule_text_bold(cd, j, df[,j] == apply(df[,2:3], 1, FUN=min))
}
cd

It breaks. Only one of the columns is highlighted. Why? I need to do this for large tables and need the loop!

enter image description here

1 Answer 1

1

Maybe this question would belong better in stackoverflow.

Anyway, the way condformat works is by capturing the expressions and evaluating them when the rendering is needed, similarly to what ggplot2 does. This is done using a technique called Non Standard Evaluation (NSE), leveraging on the rlang package.

You can read more about NSE here: https://dplyr.tidyverse.org/articles/programming.html

rlang provides an unquote operator called "bang-bang" !!. This operator allows the user to unquote parts of the expression, to replace variables with their values.

See for instance how the !! operator is used in your example below to replace the value of j:

data(iris)
library(condformat)
#library(knitr)
df <- iris[c(1:5,70:75, 120:125),]

cd <- condformat(df)

for (j in 2:3){
  cd <- rule_text_bold(cd, !!j, df[,!!j] == apply(df[,2:3], 1, FUN=min))
}
cd

with the bangbang operator the code works as expected

On condformat 0.8.0, you may see a warning once per session due to recent updates of the rlang package. The warning won't appear in the next version (currently at GitHub).

2
  • Thanks for the explanation and the fix! As an aside, can I somehow generate just the tuples without the tabular boilerplate? kable seems limited in how much I can control tabular rdrr.io/cran/knitr/src/R/table.R Commented Nov 17, 2019 at 22:05
  • I don't know if I fully understand what you want. Internally, condformat creates a character matrix with the LaTeX code that should go into each table cell. Would a function that returns that matrix be good enough for you? Please open a GitHub issue if that's the case and I'll do my best to create it.
    – zeehio
    Commented Nov 18, 2019 at 7:00

Not the answer you're looking for? Browse other questions tagged .