title: How to conditionally color table elements using formattable and color_tile?
body: <p>I am trying to set up a table with colored tiles that are colored conditionally based on the average of each column. Basically, the tile would be red if the value is below the average, and green if above the average. I used a simple if else statement in my function below.  </p>
  
  <p>I will eventually use the "formattable" package and color_tile function within the package. </p>
  
  <p>I have attempted to write my own function to pick the colors, but it is only successfully labelling the first row in my data frame.</p>
  
  <pre><code>#library(formattable) # not used in this example
  green &lt;- "#71CA97"
  red &lt;- "#ff7f7f"
  
  col1 &lt;- c(1.2, 4.2, 5.6, 7.1)
  col2 &lt;- c(5.0, 1.3, 10.3, 6.0)
  col3 &lt;- c(4.7, 6.3, 1.5, 6.3)
  mydata &lt;- data.frame(col1, col2, col3)
  
  colorPicker &lt;- function(x) {
       if(x &lt;= 5) {return("red")}
       else {return("green")}
  }
  
  tile.colors &lt;- lapply(c(mydata), colorPicker)
  </code></pre>
  
  <p>Warning message:
  In if (x &lt;= 5) { :
    the condition has length > 1 and only the first element will be used</p>
  
  <p>"tile.colors" returns the correct colors, but only for the first row.</p>
  
  <p>I would eventually call "tile.colors" in the formattable function, but for now I am just trying to get the color picking function correct.</p>
  
  <p>Is there a more efficient way to accomplish this task?</p>