I am trying to change the footer of a Datatable in a Shiny App. I can replicate the error I get in the App with the following code:
dt_test <- tibble(cntry = c("A","A","B"),
city = c("X","Y","Z"),
sales = c(1000,1500,500),
score = c(1.1234,5.1234,2.1234))
footer <- sapply(dt_test, function(x) ifelse((is.numeric(x)), sum(x), ""))
sketch <- htmltools::withTags(table(
tableHeader(dt_test),
tableFooter(footer)
))
sketch
R shows this:
Error in writeImpl(text) :
Text to be written must be a length-one character vector
But if I take the definition of the footer directly as the argument it works:
sketch <- htmltools::withTags(table(
tableHeader(dt_test),
tableFooter(sapply(dt_test, function(x) ifelse( (is.numeric(x)),sum(x), "" ))
)))
Unfortunately I can't use this approach as the aggregation has a lot of business logic included and is performed in a separate function. What am I doing wrong here?
The issue is that footer
is the name of a tag, i.e. the <footer>
tag. Hence, when using footer
inside htmltools::withTags
you are actually passing tags$footer
(which is a function) to tableFooter
and not the content stored in your vector footer. That's why you get the error message and that's why you code works when you pass the defintion directly.
This said there are two options to make your code work:
Option 1: Use htmltools::tags$...
instead of htmltools::withTags
library(tibble)
library(DT)
sketch <- htmltools::tags$table(
tableHeader(dt_test),
tableFooter(footer)
)
Option 2: Rename your variable
footer1 <- footer
sketch <- htmltools::withTags(table(
tableHeader(dt_test),
tableFooter(footer1)
))