title: Create a function using gt package to display tick & cross against approved & non approved customers in R?
body: <p>I am new in R &amp; have created some <code>Classification models</code>. By using those I need to display tick &amp; cross against approved and rejected customers based <code>Class</code> column.</p>
  <p>I picked up a piece of code from somewhere that helps in creating star Ratings against each and it uses <code>gt</code> package</p>
  <p><strong>dataframe</strong></p>
  <pre><code>df_test &lt;- cbind(prob = predict(model_ranger_py, newdata = test, type = &quot;prob&quot;)[,&quot;yes&quot;], 
                          Class = y_test) %&gt;% 
              rename(Class = y)
  
  
  df_test 
  
  ############ output #############
  
         prob    Class
        &lt;dbl&gt;    &lt;fctr&gt;
  
  3   0.4906592   no      
  6   0.6123333   no      
  12  0.3746750   no      
  14  0.4906592   no      
  22  0.7820000   yes     
  24  0.5333956   no      
  29  0.5281762   no      
  45  0.7413333   no      
  46  0.7413333   no      
  50  0.5333956   no
  53  0.5333956   no      
  54  0.7560000   yes     
  57  0.4906592   no      
  59  0.5281762   no      
  62  0.7413333   no      
  64  0.6626619   no      
  68  0.4906592   no      
  74  0.7413333   no      
  75  0.5333956   yes     
  76  0.5333956   no
  </code></pre>
  <p><strong>Reference code to create star ratings by using <code>gt</code> &amp; <code>fontawesome</code> packages (<em>this works</em>)</strong></p>
  <pre><code>library(tidyverse)
  library(gt)
  library(htmltools)
  library(fontawesome)
  </code></pre>
  <ol>
  <li>Creating function</li>
  </ol>
  <pre><code>rating_stars5 &lt;- function(rating, max_rating = 5){
    rounded_rating &lt;- floor(rating + 0.5)
    stars &lt;- lapply(seq_len(max_rating), function(i){
      if(i &lt;= rounded_rating){
        fontawesome::fa(&quot;star&quot;, fill = &quot;orange&quot;)
      } else{
        fontawesome::fa(&quot;star&quot;, fill = &quot;grey&quot;)
      }
    })
    label &lt;- sprintf(&quot;%s out of %s&quot;, rating, max_rating)
    # label &lt;- glue(&quot;{rating} out of {max_rating}&quot;)
    div_out &lt;- div(title = label, &quot;aria-label&quot; = label, role = &quot;img&quot;, stars)
    
    as.character(div_out) %&gt;% 
      gt::html()
  }
  </code></pre>
  <ol start="2">
  <li>Applying function on dataframe</li>
  </ol>
  <pre><code>df_test %&gt;% 
  
    # creating customerid based on row index
    mutate(customerid = row.names(.)) %&gt;% 
    
    # converting to 5 bins to match 5 stars
    mutate(rating = cut_number(prob, n =5) %&gt;% as.numeric()) %&gt;%
    mutate(rating = map(rating, rating_stars5)) %&gt;% 
    arrange(customerid) %&gt;% 
      
    # to limit the number of rows in rmarkdown rendered doc
    head(n = 15) %&gt;% 
    
    gt() %&gt;% 
    tab_header(title = gt::md(&quot;__BankMarketing Term Plan Customer Response Likelyhood__&quot;)) %&gt;% 
    tab_spanner(
      label = gt::html(&quot;&lt;small&gt;High Stars = higher chances&lt;/small&gt;&quot;),
      columns = vars(customerid, prob, Class)
    ) %&gt;% 
    
    # table styling to reduce text size
    tab_style(
      style = cell_text(size = px(12)),
      locations = cells_body(
        columns = vars(customerid, prob, Class)
      )
    ) %&gt;% 
    cols_label(
      customerid = gt::md(&quot;__CUSTOMER__&quot;)
    ) 
    
  </code></pre>
  <p>This creates a nice html table:</p>
  <p><a href="https://i.stack.imgur.com/dYTS3.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dYTS3.jpg" alt="enter image description here" /></a></p>
  <p><strong>Issue:</strong></p>
  <p>In above <strong>html table</strong> instead of <strong>Star Ratings</strong> I am trying to get <strong>tick/cross</strong> based on <strong>yes/no</strong> from <strong>class</strong> column but unable to do it. This is what I have tried:</p>
  <pre><code># 1. creating function
  
  rating_yes_no &lt;- function(Class){
    
    check_cross &lt;- lapply(Class, function(i){
      if(i == &quot;yes&quot;){
        fontawesome::fa(&quot;check&quot;, fill = &quot;green&quot;)
      } else{
        fontawesome::fa(&quot;times&quot;, fill = &quot;red&quot;)
      }
    })
    label &lt;- sprintf(&quot;%s&quot;, check_cross)
    # label &lt;- glue(&quot;{check_cross} &quot;)
    div_out &lt;- div(title = label, &quot;aria-label&quot; = label, role = &quot;img&quot;, check_cross)
    
    as.character(div_out) %&gt;% 
      gt::html()
  }
  
  # 2. Applying function
  
  df_test %&gt;% 
    mutate(customerid = row.names(.)) %&gt;% 
    
    mutate(class_rating = map(class_rating, rating_yes_no)) %&gt;% 
    arrange(customerid) %&gt;% 
      
    # to limit the number of rows in rmarkdown rendered doc
    head(n = 15) %&gt;% 
    
    gt() %&gt;% 
    tab_header(title = gt::md(&quot;__BankMarketing Term Plan Customer Response Likelyhood__&quot;)) %&gt;% 
    tab_spanner(
      label = gt::html(&quot;&lt;small&gt;High Stars = higher chances&lt;/small&gt;&quot;),
      columns = vars(customerid, prob, Class)
    ) %&gt;% 
    
    # table styling to reduce text size
    tab_style(
      style = cell_text(size = px(12)),
      locations = cells_body(
        columns = vars(customerid, prob, Class)
      )
    ) %&gt;% 
    cols_label(
      customerid = gt::md(&quot;__CUSTOMER__&quot;)
    ) 
  </code></pre>