Formatear datos en una tabla como porcentaje
Tengo un marco de datos que se parece a esto:

Aquí está el código para crear este DF:
structure(list(ethnicity = structure(c(1L, 2L, 3L, 5L), .Label = c("AS",
"BL", "HI", "Others", "WH", "Total"), class = "factor"), `Strongly agree` = c(30.7,
26.2, 37.4, 31.6), Agree = c(43.9, 34.5, 41, 45.4), `Neither agree nor disagree` = c(9.4,
14.3, 8.6, 8.7), Disagree = c(10, 15.5, 9.9, 9.7), `Strongly disagree` = c(6,
9.5, 3.2, 4.6)), row.names = c(NA, -4L), class = "data.frame")
Quiero agregar barras de datos y hacer estos números como porcentajes. Intenté usar la biblioteca formateable para hacer eso (vea mi código a continuación).
formattable(df,align=c("l","l","l","l","l","l"),
list(`ethnicity` = formatter("span", style = ~ style(color = "grey", font.weight = "bold"))
,area(col = 2:6) ~ function(x) percent(x / 100, digits = 0)
,area(col = 2:6) ~ color_bar("#DeF7E9")))
Estoy enfrentando 2 problemas:
- Los números no aparecen como un porcentaje en la salida de la tabla.
- La alineación parece apagada en la última columna, es decir.
¿Realmente agradecería si alguien pudiera ayudarme a entender qué me estoy perdiendo aquí?

Respuestas
Aquí hay una solución, pero requiere mucha escritura, supongo que es posible usarla, mutate_at()
pero no puedo averiguar cómo pasar los nombres de las columnas en la percent()
parte. El uso de .
produce un error.
Esto funciona con mucha escritura:
library(dplyr)
library(formattable)
df %>%
mutate(`Strongly agree` = color_bar("#DeF7E9")(formattable::percent(`Strongly agree`/100))) %>%
mutate(`Agree` = color_bar("#DeF7E9")(formattable::percent(`Agree`/100))) %>%
mutate(`Disagree` = color_bar("#DeF7E9")(formattable::percent(`Disagree`/100))) %>%
mutate(`Neither agree nor disagree` = color_bar("#DeF7E9")(formattable::percent(`Neither agree nor disagree`/100))) %>%
mutate(`Strongly disagree` = color_bar("#DeF7E9")(formattable::percent(`Strongly disagree`/100))) %>%
formattable(.,
align=c("l","l","l","l","l","l"),
`ethnicity` = formatter("span", style = ~ style(color = "grey", font.weight = "bold")))
Esto no funciona, pero podría mejorarse:
df %>%
mutate_at(.vars = 2:6, .funs = color_bar("#DeF7E9")(formattable::percent(./100))) %>%
formattable(...)
Más información sobre esta estructura "extraña" var = color_bar(...)(var)