So I have a dashboard in Shiny. It's just simple table that looks like [the image found in this link][1] (apologies that I can't give you something more reproducible. Not sure how I would do that.)
Asked
Active
Viewed 1,178 times
1 Answers
1
A working shiny app:
ui.R
library(shiny)
library(DT)
library(htmltools)
fluidPage(
title = 'DataTables Information',
h1('A client-side table'),
fluidRow(
column(12,
selectInput("speciesSelector",
"Select species",
choices = c("All", levels(iris$Species)),
selected = "All"),
DT::dataTableOutput('iris')
)
)
)
server.R
library(shiny)
library(DT)
library(htmltools)
sketch <- htmltools::withTags(table(
class = "display",
style = "bootstrap",
tableHeader(c("ID", colnames(iris))),
tableFooter(c("ID", colnames(iris)))
))
shinyServer(function(input, output, session) {
data <- reactive({
data <- iris
if (input$speciesSelector != "All") {
data <- data[data$Species == input$speciesSelector,]
}
data
})
# render the table (with row names)
output$iris = DT::renderDataTable(data(),
container = sketch,
server = FALSE,
caption = "Column sum example",
filter = "top", options = list(footerCallback = JS(
"function( tfoot, data, start, end, display ) {",
"var api = this.api(), data;",
"total = api.column( 4, { page: 'current'} ).data().reduce( function ( a, b ) {return a + b;} )",
"total1 = api.column( 4, { search:'applied'} ).data().reduce( function ( a, b ) {return a + b;} )",
"$( api.column( 4 ).footer() ).html(total.toFixed(2) + ' / ' + total1.toFixed(2));",
"}")))
})

Valter Beaković
- 3,140
- 2
- 20
- 30
-
Should this be included in my server.R file or should it entirely replace it? – wizkids121 Aug 04 '16 at 15:33
-
1This should be included in the server.R file in the DT::renderDataTable part, copy the code starting with the parenthesis after the datatable keyword. The table definition (sketch) can be in the server.R file, I have put it outside the shinyServer function. Add the server = FALSE parameter to the DT::renderDataTable call. At the moment I was not able to make it run with the parameter server = TRUE. – Valter Beaković Aug 05 '16 at 08:40
-
Beakovic I tried that, but it produced something that I was not looking for. I put the code above and the resulting page. – wizkids121 Aug 11 '16 at 21:14
-
@wizkids121, ok, I'll put a full example later today – Valter Beaković Aug 12 '16 at 05:54
-
@wizkids121 I have edited my answer to include a working Shiny app using the Iris dataset. Let me know if this helps. – Valter Beaković Aug 12 '16 at 09:36
-
@wizkids121 regarding the dropdown the variable marketing_pro_name should be a factor, I'll take a look at the 1. issue. – Valter Beaković Aug 12 '16 at 16:32
-
The part were you define the table (sketch variable) change the table footer definition to tableFooter(c("ID", colnames(BreakdownByRep))) – Valter Beaković Aug 12 '16 at 16:52
-
@wizkids121 just to be clear, you would like to have totals for each columns? – Valter Beaković Aug 12 '16 at 17:40
-
Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/120831/discussion-between-wizkids121-and-valter-beakovic). – wizkids121 Aug 12 '16 at 18:35