title: "How can I conditionally format the letters in a datatable?
body: <p>I am new to R, I am currently trying to make a table that shows me what the variation is between 2 days and that once this variation is calculated if it is negative that the sea turns out with red letters if not then green</p>
  
  <p><a href="https://i.stack.imgur.com/nfKFH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/nfKFH.png" alt="enter image description here"></a></p>
  
  <hr>
  
  <p>title: "<strong>Area Comercial Consumo</strong>"
  output: 
    flexdashboard::flex_dashboard:
      orientation: columns</p>
  
  <h2>    vertical_layout: fill</h2>
  
  <pre><code>################## librerias #####################
  library(flexdashboard)
  library(tidyverse)
  library(readxl)
  library(shiny)
  library(formattable)
  library(DT)
  library(htmlTable)
  library(sqldf)
  library(ggplot2)
  library(data.table)
  library(dplyr)
  library(tidyr)
  library(shinydashboard)
  
  ##################  Datos   #######################
  base1     &lt;- read_excel("C:/Karlo disco duro/AVANCE.xlsx",sheet = "Base1")
  base2     &lt;- read_excel("C:/Karlo disco duro/AVANCE.xlsx",sheet = "Base2")
  finmes &lt;- read_excel("C:/Karlo disco duro/AVANCE.xlsx",sheet = "Finmes")
  </code></pre>
  
  <h2>{width=20%}  {width=6%}</h2>
  
  <h2><strong>Avance de colocaciones diarias</strong></h2>
  
  <pre><code>
  Green0 = "#DeF7E9"
  Green = "#71CA97"
  Red = "#ff7f7f"
  
  data1&lt;-subset(base1,select = c("TipoCredito","SaldoCapital"))
  data2&lt;-subset(base2,select = c("TipoCredito","SaldoCapital"))
  finmes1&lt;-subset(finmes,select = c("TipoCredito","SaldoCapital"))
  final1&lt;-data.frame(sqldf(
    "select TipoCredito, sum(SaldoCapital) as SALDO from data1 group by TipoCredito"
  ))
  final2&lt;-data.frame(sqldf(
    "select TipoCredito, sum(SaldoCapital) as SALDO from data2 group by TipoCredito"
  ))
  finmes2&lt;-data.frame(sqldf(
    "select TipoCredito, sum(SaldoCapital) as SALDO from finmes1 group by TipoCredito"
  ))
  
  tablaa&lt;-data.frame(final1)
  tablab&lt;-data.frame(final2)
  tablafinmes&lt;-data.frame(finmes2)
  
  final3&lt;-data.frame(sqldf(
    "select A.TipoCredito, A.SALDO as '23Agosto', B.SALDO as '24Agosto', (B.SALDO - A.SALDO) as VARIACION
    from tablaa A
    join tablab B on A.TipoCredito = B.TipoCredito
    group by A.TipoCredito"
  ))
  
  finalfinmes &lt;- data.frame(sqldf(
    "select F.TipoCredito, F.SALDO as '31Julio', B.SALDO as '24Agosto', (B.SALDO - F.SALDO) as VARIACION
    from tablafinmes F
    join tablab B on F.TipoCredito = B.TipoCredito
    group by F.TipoCredito"
  ))
  </code></pre>
  
  <pre><code>
  my.options &lt;- list (autoWidth = FALSE,
                     searching = FALSE,
                     ordering = FALSE,
                     lengthChange = FALSE,
                     lengthMenu = FALSE,
                     pageLength = FALSE,
                     paging = FALSE,
                     info = FALSE)
  ##final&lt;-formattable(final3,list(VARIACION = color_tile("red", "green")))
  
  ##final3.1&lt;-formattable(final3, list(`VARIACION`=formatter("span", style = x ~ style(color = ifelse(x &lt; 0, "red", "green")))))
  ##finalfinmes1&lt;-formattable(finalfinmes, list(`VARIACION`=formatter("span", style = x ~ style(color = ifelse(x &lt; 0, "red", "green")))))
  
  Tablafinal&lt;-datatable(final3,options = list(autoWidth = TRUE, columnDefs = 
                        list(list(width = '190px', targets = c(1,2,3,4))),pageLength = 7,searching = TRUE,
                     ordering = TRUE,
                     lengthChange = TRUE,
                     lengthMenu = FALSE,
                     pageLength = FALSE,
                     paging = FALSE,
                     info = FALSE), colnames = c('Tipo de credito','23 de Agosto','24 de Agosto', 'Variacion Diaria')) 
  
  Tablafinalfinmes&lt;-datatable(finalfinmes,options = list(autoWidth = TRUE, columnDefs = 
                        list(list(width = '190px', targets = c(1,2,3,4))),pageLength = 7,searching = TRUE,
                     ordering = TRUE,
                     lengthChange = TRUE,
                     lengthMenu = FALSE,
                     pageLength = FALSE,
                     paging = FALSE,
                     info = FALSE),colnames = c('Tipo de credito','31 de Julio','24 de Agosto', 'Variacion fin de mes'))
  ##%&gt;% formatStyle('VARIACION',color = ifelse('VARIACION'&gt;0,'Green','Red')))
  
  ##Tablafinal$VARIACION &lt;- ifelse(Tablafinal$VARIACION &lt; 0, 1, ifelse(Tablafinal$VARIACION &gt; 0, 2,3))
  
  Tablafinal1&lt;- formatStyle(Tablafinal,
                          columns = c(4),
                          fontFamily = "Arial",
                          fontSize = "16px", 
                          color = ifelse('VARIACION'&lt; -0,'#ed1c16','#0ca649'))
                         ## backgroundColor = styleEqual(c(1, 2,3), c('green', 'yellow','red')))
                          ##color = styleEqual(c(-1,0,1),c('green','blue','Yellow')))
  
  Tablafinalfinmes1&lt;- formatStyle(Tablafinalfinmes,
                          columns = c(4),
                          fontFamily = "Arial",
                          fontSize = "16px",
                          color = ifelse('VARIACION'&lt; -0,'#ed1c16','#0ca649'))
  
  Tablafinal1$VARIACION &lt;- ifelse(Tablafinal1$VARIACION &lt; 0, 1, 
                      ifelse(Tablafinal1$VARIACION &gt; 0, 2))
  
  ab &lt;- Tablafinal1 %&gt;% formatCurrency (c('X23Agosto','X24Agosto','VARIACION'), 's/ ') %&gt;% formatStyle('TipoCredito',target = 'row',backgroundColor = styleEqual(c('CONSUMO NO REVOLVENTE','CONSUMO REVOLVENTE','HIPOTECARIO'),c('#f8fb63','#f8fb63','#f8fb63'))) %&gt;% formatStyle('VARIACION',fontWeight = 'bold',fontSize = "13px") 
  
  ##ab1 &lt;- ab %&gt;% formatStyle(columns = c('VARIACION'),valueColumns = c('VARIACION'),target='row',Color = styleEqual(c(-1,1),
  ##                                                                      c('red','green')))
  
  tf &lt;- Tablafinalfinmes1 %&gt;% formatCurrency (c('X31Julio','X24Agosto','VARIACION'),'s/ ')%&gt;% formatStyle('TipoCredito',target = 'row',backgroundColor = styleEqual(c('CONSUMO NO REVOLVENTE','CONSUMO REVOLVENTE','HIPOTECARIO'),c('#f8fb63','#f8fb63','#f8fb63'))) %&gt;% formatStyle('VARIACION',fontWeight = 'bold',fontSize = "13px")
  
  ##ui&lt;- fluidPage (navlistPanel(tabPanel("Variacion Diaria",ab),
  ##                  tabPanel("Variacion Mensual")))
  ##ui&lt;-formattable(ab, list(VARIACION = color_tile("red", "green")))
  ui&lt;- fluidPage (tabsetPanel(tabPanel("Variacion Diaria",ab),
                tabPanel("Variacion Mensual",tf)))
  </code></pre>
  
  <h1>INICIO</h1>
  
  <h2>row{.tabset} {width=80}</h2>
  
  <pre><code>ui
  </code></pre>