I´m using openxlsx2 so that I can use the function wb_add_pivot_table() but the problem is that the order in which the rows appear, inside the pivot table, isn't in the order I want it to be displayed.
I´ve tried the following:
tbl_1 <- tibble(
var_1 = c("FEBRERO", "SEPTIEMBRE","AGOSTO", "SEPTIEMBRE", "AGOSTO", "FEBRERO", "DICIEMBRE", "DICIEMBRE"),
var_2 = seq_along(var_1)
) |>
mutate(
var_1 = as.factor(var_1) |>
fct_relevel("FEBRERO", "AGOSTO", "SEPTIEMBRE", "DICIEMBRE")
) # I want the order of te rows, inside the pivot table, be like this
tbl_1
wb_1 <- wb_workbook() |>
wb_add_worksheet() |>
wb_add_data(x = tbl_1)
df <- wb_data(wb_1)
wb_1 |>
wb_add_pivot_table(
x = df,
rows = "var_1",
data = "var_2",
fun = "sum"
) |>
wb_save("example_1.xlsx")
My naive approach, as you see, was to convert the class of the var_1 column to factor, but it didn't work. Thanks in advance.

