Creating a function of Dt table in shiny

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)

This isn't a shiny problem, or a DT problem, its a question of how %>% works.
%>% is a way to re-write b(a) as a%>% b, so a %>% NULL is equivalent to NULL(a) and NULL is not a function, so this is a syntax error.
A drop in replacement would be a function that returns its input unchanged. base::identity() which has a simple definition function(x)x could be used.

This topic was automatically closed 21 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.