Error in gt_tbl

Hello,
I am trying to convert a tibble object into a gt_tbl object, as I cannot apply the function data_colour if my data is not in this format:

nacionalidad <- c("Españoles", "Extranjeros UE", "Extranjeros no UE", "Total")
empleado <- c(84.5, 78.0, 70.1, 23894474)
des1_11 <- c(6.5, 12.0, 16.6, 2054007)
des12 <- c(9.0, 10.0, 13.3, 2637471)
total <- c(26252992, 904100, 1428860, 28585952)
prest <- c(3424.30,2594.51,2548.90,3329.352 )
crosstable1 <- data.frame(nacionalidad, empleado, des1_11, des12, total, prest)
crosstable1 <- as_tibble(crosstable1)
gt1 <- gt(data = crosstable1)

gt1 <- gt1%>% cols_label(nacionalidad = "Nacionalidad", empleado ="Empleados", des1_11="Desempleaddos 1-11 meses",
                         des12="Desempleados 12 meses", total="Total", prest="Media prestaciones por desempleo 2017")

gt1 <- gt1 %>% data_color(data=crosstable1, columns=c(total, prest),
                          colors = scales::col_numeric(
                            palette = paletteer::paletteer_d(
                              palette = "ggsci::red_material"
                            )))

This returns the following error: "The object to data is not a gt_tbl object."

I have used this code to try to convert my data to gt_tbl

crosstable1 <- as_gt(crosstable1)

However, this returns the following error: "Error in matrix(ncol = n) : data is too long
Also: Warning message:
Unknown or uninitialised column: 'gt_calls'."

I would greatly appreciate it if anyone could let me know why these errors occur (I am fairly new to R, and very new with the gt package), or if there is a more efficient way to color tables with the gt function.

You just have some syntax errors, see this example:

library(gt)

crosstable1 <- data.frame(
    nacionalidad = as.factor(c("Españoles", "Extranjeros UE",
                               "Extranjeros no UE","Total")),
    empleado = c(84.5, 78, 70.1, 23894474),
    des1_11 = c(6.5, 12, 16.6, 2054007),
    des12 = c(9, 10, 13.3, 2637471),
    total = c(26252992, 904100, 1428860, 28585952),
    prest = c(3424.3, 2594.51, 2548.9, 3329.352)
)

crosstable1  %>% 
    gt() %>% 
    cols_label(nacionalidad = "Nacionalidad",
               empleado ="Empleados",
               des1_11="Desempleaddos 1-11 meses",
               des12="Desempleados 12 meses",
               total="Total",
               prest="Media prestaciones por desempleo 2017") %>%
    data_color(columns=vars(total, prest),
               colors = scales::col_numeric(
                   palette = as.character(paletteer::paletteer_d(palette = "ggsci::red_material")),
                   domain = NULL)
               )

2 Likes

Got it! Thank you for your help.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.