Search code examples
rformattable

How to make a table with formattable in R?


I'm trying to make a table in R with formattable but some rows are returning NA and I don't know how to remove them.

In Excel the table looks like this, and this is how I would like it to be:

enter image description here

In R looks like this:

enter image description here

How can I make this table in R looks like in Excel?

Code:

library(readxl)
library(formattable)

indicadores <- read_excel("Dados/Tabela Indicadores.xlsx")

tabela_indicadores <- formattable(indicadores,
                                  align = c("c", "c", "c", "c"))

File that I'm using: https://drive.google.com/file/d/1CDhMkW7l78WEj45mYZ86LTY6PjCyhsSd/view?usp=sharing


Solution

  • The easiest way to do this is to replace the missing values with a character, "-" or "":

    indicadores$Tema <- ifelse(is.na(indicadores$Tema), "-", indicadores$Tema)
    tabela_indicadores <- formattable(indicadores, align = c("c", "c", "c", "c"))
    

    I am assuming you do not want to exclude the lines completely. If you do want to remove them, use

    tabela_indicadores <- formattable(na.omit(indicadores), align = c("c", "c", "c", "c"))