How to adjust formatRound of the datatable function

Could you help me adjust the formatRound of the datatable function. I would like to leave it like this: formatRound(c(3:last_col()), ie until the last column, but it doesn't work, how can I adjust this, because I don't want to specify a value, for example, c(3:4).

library(DT)

Test <- structure(list(date2 = structure(c(18808, 18808, 18809, 18810
), class = "Date"), Category = c("FDE", "ABC", "FDE", "ABC"), 
coef = c(4, 1, 6, 1)), row.names = c(NA, 4L), class = "data.frame")

Test<-Test %>% mutate(date2 = format(ymd(date2), "%d/%m/%Y"))%>%
  mutate(SUM = rowSums(across(3:last_col()), na.rm = TRUE)) %>% 
  mutate(across(everything(), ~ replace_na(as.character(.), '-')))


z<-datatable (Test, options = list(columnDefs = list(list(className = 'dt-center', targets = "_all")),
                                   paging =TRUE,searching = FALSE, pageLength =  10,dom = 'tip',scrollx=T),rownames = FALSE)%>%
  formatRound(c(3:last_col()), digits=0)

if you want to know what number the last column of a data.frame is base R has you covered.
the function ncol() will tell you the number of the last column of Test is 4.

ncol(Test)

so

formatRound(c(3:ncol(Test)), digits=0)

Thanks @nirgrahamuk ! =)

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.