How do I suppress row names when using DT::renderDataTable in R shiny?
Please be very careful to read the help pages of functions to know which argument belongs to which function. In your case, the rownames
argument belongs to the datatable()
function, but you actually put it inside the options
argument, and that is certainly wrong. DT::renderDataTable()
accepts either a data object or a table widget as its first argument (again, please read its help page), so either of the following expressions should work:
DT::renderDataTable(datatable(
subsetTable(), filter = 'top', server = FALSE,
options = list(pageLength = 5, autoWidth = TRUE),
rownames= FALSE
))
DT::renderDataTable(
subsetTable(), filter = 'top', server = FALSE,
options = list(pageLength = 5, autoWidth = TRUE),
rownames= FALSE
)
In the latter case, rownames = FALSE
is passed to datatable()
internally, per documentation of the ...
argument of the help page.
Can't hide columns AND set rownames = FALSE in Shiny DT
As rownames are also a column, when you set them to false, yo need to reindex the columns you want to hide. So, in your particular case, column 5 no longer exist. Now it is number 4, and the 4th is the 3rd, so your code should look like:
server <- function(input, output) {
output$table <- DT::renderDataTable({
# Can't use both visible = FALSE and rownames = FALSE
datatable(x, rownames=F,
options = list(
columnDefs = list(list(targets = c(3, 4), visible = FALSE) # THIS
)
)) %>% # OR THIS
formatStyle(
columns = c('Calls', 'Emails'),
valueColumns = c('Calls_goal', 'Emails_goal'),
color = styleEqual(c(1, 0), c("red", "black"))
)
})
}
Supress rownames in data table on download only
The column of row names is the 0-th column. You can assign a class to this column with the columnDefs
option, e.g. rownames
, and use the exportOptions
option to select the columns which do not have this class, with the :not
selector:
library(DT)
datatable(
iris[1:5,],
extensions = 'Buttons',
rownames= TRUE,
options = list(
dom = 'Bfrtip',
columnDefs = list(
list(
targets = 0, className = "rownames"
)
),
buttons = list(
list(
extend = 'excel',
filename = "XLSXFILE",
exportOptions = list(
columns = ":not(.rownames)"
)
)
)
)
)
Turn on row names in renderDataTable
Try this, it works for me, although I can't fully reproduce your dataset.
output$tab <- DT::renderDataTable({
datatable( tabplot(), rownames = TRUE )
})
I've forced the use of the DT package, and also enclosed the table creation in a "datatable" call, which includes the option to enable rownames. I think you'd also be able to enable that option in the creation of "tablplot()", but it should definitely work here either way.
Remove Observation Index in Data.Table R -Shiny Dashboard
The renderTable page says that rownames
is an optional logical variable, so adding rownames = TRUE
to your renderTable
function should be sufficient
R Shiny DT: How to hide columns or format table?
We can use
options= list(columnDefs = list(list(visible = FALSE, targets = target)))
to control which columns are visible, and
target <- which(names(mtcars) %in% c("gear", "carb")) - 1
to get the position of the cols. The - 1 is because js uses 0 index instead of 1 like R.
App:
library(tidyverse)
library(shiny)
library(dplyr)
library(ggplot2)
library(tidyr)
library(shinycssloaders)
library(shinythemes)
library(ggforce)
library(DT)
library(shinyWidgets)
library(shinyjs)
mtcars
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
useShinyjs(),
div(
id = "form",
fluidRow(
# Button to select gear
column(
6,
pickerInput(
inputId = "gear_button", label = "Gear:", choices = c("All", unique(as.character(mtcars$gear))), options = list(`actions-box` = TRUE), multiple = FALSE
),
),
# Button to select carb ranges
column(
6,
pickerInput(inputId = "carb_button", label = "Carb:", choices = c("All", unique(as.character(mtcars$carb))), options = list(`actions-box` = TRUE), multiple = FALSE),
),
)
),
actionButton("resetAll", "Reset Filters")
),
mainPanel(
DT::dataTableOutput("table")
)
),
)
server <- function(input, output, session) {
# Explore tab - table
data <- mtcars
table <- reactive({
if (input$gear_button != "All") {
data <- data[data$gear == input$gear_button, ]
}
if (input$carb_button != "All") {
data <- data[data$carb == input$carb_button, ]
}
data
})
output$table <- DT::renderDataTable({
target <- which(names(table()) %in% c("gear", "carb")) - 1
datatable(table(),
class = "display nowrap compact",
filter = list(position = "top"),
rownames = FALSE,
options = list(
dom = "t",
columnDefs = list(list(visible = FALSE, targets = target)),
scrollX = TRUE
)
)
})
observeEvent(input$resetAll, {
reset("form")
})
}
shinyApp(ui, server)
how to formaStyle a row by index /rowname with datatable in shiny?
We may specify the index for rows as 0
in formatStyle
, and use styleEqual
to match and replace the 'cols1' created
server <- function(input, output) {
v1 <- row.names(data_example)
cols1 <- ifelse(v1 =='Sum','orange','')
output$table <- renderDataTable(datatable(data_example)%>%
formatStyle(0, target = "row",
backgroundColor = styleEqual(v1, cols1)))
}
shinyApp(ui = ui, server = server)
-output
Related Topics
Automatic Documentation of Datasets
Creating a Facet_Wrap Plot with Ggplot2 with Different Annotations in Each Plot
Texture in Barplot for 7 Bars in R
Using a Loop to Create Multiple Data Frames in R
Plotting a Large Number of Custom Functions in Ggplot in R Using Stat_Function()
Sum of Antidiagonal of a Matrix
Arithmetic Mean on a Multidimensional Array on R and Matlab: Drastic Difference of Performances
Order and Color of Bars in Ggplot2 Barplot
Ctree() - How to Get the List of Splitting Conditions for Each Terminal Node
Fast Replacing Values in Dataframe in R
How to Extract Elements from a List with Mixed Elements
R Fails After Installing Gtk and Rgtk2
R Programming: How to Get Euler's Number
Error with Ggplot2 Mapping Variable to Y and Using Stat="Bin"
How to Convert a String in a Function into an Object
Passing Large Matrices to Rcpparmadillo Function Without Creating Copy (Advanced Constructors)