title: How to apply formattable formats to multiple columns?
body: <p>I have two data.frames (each originally a dimension from a larger parent 3-d array).  One holds numeric values.  The other has T/F values indicating whether the confidence interval for each value in the first array overlaps a reference confidence interval.  The confidence intervals are different for every value in the array, so in formatting the table I can’t refer to constants, only to the array of T/F values.</p>
  
  <p>I want to show a table of the first array, with background color of each cell based on the second array.  So that formattable can see the columns with T/F values, I created a single data frame that binds the columns from both 3rd dimensions.  In the real data there are ~20 columns of numeric values.  Here is a simplified example:</p>
  
  <pre><code> orig.data &lt;- array(dim = c(3, 4, 2))
   dimnames(orig.data) &lt;- list (c("site1", "site2", "site3"), c("model1", "model2", "model3",
     "model4"), c("mean.val", "is.in.CI"))
   orig.data[,,1] &lt;- round(runif(12, 2, 10), 2)
   orig.data[,,2] &lt;- as.logical(round(runif(12, 0, 1)))
   ft.data &lt;- data.frame(orig.data[,,2], stringsAsFactors = F)
   colnames(ft.data) &lt;- paste0("match.", colnames(ft.data))
   ft.data &lt;- cbind(data.frame(orig.data[,,1], stringsAsFactors = F), 
      ft.data)
  </code></pre>
  
  <p>I can create the table formatting I want by calling each column by name.  There are two special considerations.  First, for the first four columns, the choice of background color is conditional on a second column.  Second, the last four columns I would like to hide.  Is there a way to do this with apply or some similar succinct dynamic syntax?</p>
  
  <p>Here is the long version that I’d like to consolidate.</p>
  
  <pre><code> yes.color &lt;- "lightgreen"
   no.color &lt;- "pink"
   formattable::formattable(ft.data, list(
       `model1` = formatter("span", style = ~ style(display = "block",
          "border-radius" = "4px", "padding-right" = "4px",
          "background-color" =  ifelse(`match.model1`, yes.color, no.color))),
       `model2` = formatter("span", style = ~ style(display = "block",
          "border-radius" = "4px", "padding-right" = "4px",
          "background-color" =  ifelse(`match.model2`, yes.color, no.color))),
       `model3` = formatter("span", style = ~ style(display = "block",
          "border-radius" = "4px", "padding-right" = "4px",
          "background-color" =  ifelse(`match.model3`, yes.color, no.color))),
       `model4` = formatter("span", style = ~ style(display = "block",
          "border-radius" = "4px", "padding-right" = "4px",
          "background-color" =  ifelse(`match.model4`, yes.color, no.color))),
       match.model1 = F,
       match.model2 = F,
       match.model3 = F,
       match.model4 = F))
  </code></pre>
  
  <p>This question is similar to the second of my questions and is unanswered:  <a href="https://stackoverflow.com/questions/53308518/loop-hide-columns-and-r-formattable">Loop, hide columns and r formattable</a></p>
  
  <p>Failed attempts to automate hiding the T/F columns follow.  I don’t have any ideas for automating the 2-column references.</p>
  
  <pre><code> formattable(ft.data[, 1:4]) 
  </code></pre>
  
  <p>If I omit the columns with the T/F designations, the formatter doesn’t know they exist.</p>
  
  <pre><code>  area(col = 5:8) = F)) # no effect
  </code></pre>
  
  <p>Outside the formattable command, create a string:</p>
  
  <pre><code> formatter.string &lt;- paste( unlist(paste0("match.", c(“model1”, “model2”, “model3”, “model4”), " = #F,\n\t")), collapse='')
  </code></pre>
  
  <p>then within the list for formattable, add</p>
  
  <pre><code>  eval(parse(formatter.string)))) # no effect  OR
    lapply(5:8, function(m.col){m.col = F}) # also no effect
  </code></pre>