kng229 kng229 - 1 month ago 26
R Question

R Shiny RenderDataTable display options

Shiny Output]

So I have this table that I'm producing using the ShinyDash package. Given the small number of records, I'd like to remove the page numbers and just have Previous/Next appear. I'd also like to remove the top-left box to select the # of records per page, and have it default at 5.

Here's my current code, although the last line with options should really be all that matters:

output$values <- renderDataTable({
}
df <- data.frame(
Period = c("SP15", "FA14", "SU14", "SP14", "FA13", "SP13"),
StartedApps = c(full$Cum[full$Day == as.numeric(elapsed) & full$Group == "SP15"],
full$Cum[full$Day == as.numeric(elapsed) & full$Group == "FA14"],
full$Cum[full$Day == as.numeric(elapsed) & full$Group == "SU14"],
full$Cum[full$Day == as.numeric(elapsed) & full$Group == "SP14"],
full$Cum[full$Day == as.numeric(elapsed) & full$Group == "FA13"],
full$Cum[full$Day == as.numeric(elapsed) & full$Group == "SP13"]),
Pct = c("",
percent(full$Cum[full$Day == as.numeric(elapsed) & full$Group == "SP15"]/full$Cum[full$Day == as.numeric(elapsed) & full$Group == "FA14"] - 1),
percent(full$Cum[full$Day == as.numeric(elapsed) & full$Group == "SP15"]/full$Cum[full$Day == as.numeric(elapsed) & full$Group == "SU14"] - 1),
percent(full$Cum[full$Day == as.numeric(elapsed) & full$Group == "SP15"]/full$Cum[full$Day == as.numeric(elapsed) & full$Group == "SP14"] - 1),
percent(full$Cum[full$Day == as.numeric(elapsed) & full$Group == "SP15"]/full$Cum[full$Day == as.numeric(elapsed) & full$Group == "FA13"] - 1),
percent(full$Cum[full$Day == as.numeric(elapsed) & full$Group == "SP15"]/full$Cum[full$Day == as.numeric(elapsed) & full$Group == "SP13"] - 1)),
CompletedApps = c(fullcomp$Cum[fullcomp$Day == as.numeric(elapsed) & fullcomp$Group == "SP15"],
fullcomp$Cum[fullcomp$Day == as.numeric(elapsed) & fullcomp$Group == "FA14"],
fullcomp$Cum[fullcomp$Day == as.numeric(elapsed) & fullcomp$Group == "SU14"],
fullcomp$Cum[fullcomp$Day == as.numeric(elapsed) & fullcomp$Group == "SP14"],
fullcomp$Cum[fullcomp$Day == as.numeric(elapsed) & fullcomp$Group == "FA13"],
fullcomp$Cum[fullcomp$Day == as.numeric(elapsed) & fullcomp$Group == "SP13"]),
Pct2 = c("",
percent(fullcomp$Cum[fullcomp$Day == as.numeric(elapsed) & fullcomp$Group == "SP15"]/fullcomp$Cum[fullcomp$Day == as.numeric(elapsed) & fullcomp$Group == "FA14"] - 1),
percent(fullcomp$Cum[fullcomp$Day == as.numeric(elapsed) & fullcomp$Group == "SP15"]/fullcomp$Cum[fullcomp$Day == as.numeric(elapsed) & fullcomp$Group == "SU14"] - 1),
percent(fullcomp$Cum[fullcomp$Day == as.numeric(elapsed) & fullcomp$Group == "SP15"]/fullcomp$Cum[fullcomp$Day == as.numeric(elapsed) & fullcomp$Group == "SP14"] - 1),
percent(fullcomp$Cum[fullcomp$Day == as.numeric(elapsed) & fullcomp$Group == "SP15"]/fullcomp$Cum[fullcomp$Day == as.numeric(elapsed) & fullcomp$Group == "FA13"] - 1),
percent(fullcomp$Cum[fullcomp$Day == as.numeric(elapsed) & fullcomp$Group == "SP15"]/fullcomp$Cum[fullcomp$Day == as.numeric(elapsed) & fullcomp$Group == "SP13"] - 1)))

df$Pct <- recode(df$Pct, "'Inf%' = 'NA'")
df$Pct2 <- recode(df$Pct2, "'Inf%' = 'NA'")
df$StartedApps <- as.integer(df$StartedApps)
df$CompletedApps <- as.integer(df$CompletedApps)
names(df) <- c("Period", "Starts", "Pct Change", "Submits", "Pct Change")
df
}, options = list(pagingType = "simple", bFilter = FALSE, aLengthMenu = 5, iDisplayLength = 5))


The app builds on the DataTables JavaScript library (http://datatables.net/reference/option/) but I don't know JavaScript enough to figure it out :( Any ideas?

Answer

You can use the sPaginationType option. In latter versions of data.table (1.10) there is more flexibility with pagingType http://datatables.net/reference/option/pagingType :

library(shiny)
runApp(list(
  server = function(input, output, session) {
  output$dt <- renderDataTable({
    data = data.frame(setNames(replicate(15, rnorm(100), simplify = FALSE), letters[1:15]))
    return(data)
  },
  options = 
    list(sPaginationType = "two_button")
  )
}
, ui = navbarPage("Foo", id="page", collapsable=TRUE, inverse=FALSE,
                   tabPanel("Bar",
                            dataTableOutput("dt")
                   )
)
)
)