Search code examples
rdynamicshinyformattable

Using formattable in R with dynamic column headers


Formattable allows one make formatted data output in R and Rshiny. A good explanation is here: https://www.r-bloggers.com/formatting-table-output-in-r/

example code

In the "formatter" function above you can see that the column headers are referenced. I would like to know if this code can be written in a way as to work with dynamic column headers that change based on a users input to an rshiny app. I was thinking that there may be a way to reference the column number instead of the specific name but I have been unsuccessful at doing this thus far. Any help is appreciated.

Thanks.


Solution

  • It appears that the function is looking for a named list to match up to the names in the data.frame. I recommend that you create the list with your desired formats, then set the name(s) of the format arguments "dynamically" to match what you are displaying.

    e.g.:

    myFormats <- list( formatter("span") )
    names(myFormats) <- input$userEnteredChoiceHere
    
    formattable(DF, myFormats)
    

    Without actually reproducible code though, it is hard to help you more than that. A picture of code is not easy to work from.