I am trying to create a function if DT table where in , based on the parameter, the table should respond
dt_table <- function(data, selected_col , colhidden, colcenter, for_per) {
data <- data %>% select(selected_col)
datatable(data,
options = list(
columnDefs = list(
list(visible=FALSE, targets=match(colhidden, colnames(data))),
list(className = 'dt-center', targets = match(colcenter, colnames(data))))
)
) %>%
if(is.null(for_per)){
NULL
} else {
formatPercentage(for_per, 2)
}
}
But the format percentage is conditional , only when it not NULL, it should consider or not else
But my code is not working
dt_table(iris,
selected_col = c("Petal.Width", "Sepal.Length", "Sepal.Width", "Petal.Length","Species"),
colhidden = c("Species"),
colcenter = c("Sepal.Width"),
for_per = NULL)