Hide Certain Columns in a Responsive Data Table Using Dt Package

Hide certain columns in a responsive data table using DT package

You can hide columns in your table using DT options or extensions.

If you want them to be hidden in advance but have a button to make them visible again, the ColVis extension should work well for you: link

If you just want thme stay hidden, add the following option (can't remember where I've seen its documentation right now..)

options=list(columnDefs = list(list(visible=FALSE, targets=columns2hide)))

How to hide a column using the DT package - columnDefs parameter doesn't work

The targets should be numeric values of the column number

datatable(test, options=list(columnDefs = list(list(visible=FALSE, targets=c(4))))) %>%

How to hide specific columns of a datatable on mobile devices

You have not added the script required for responsive column hiding. Add, https://cdn.datatables.net/colreorder/1.5.2/js/dataTables.colReorder.min.js and DataTable will auto hide columns when screen size is small.

Also add the responsive plugin script https://cdn.datatables.net/responsive/2.2.3/js/dataTables.responsive.min.js

If you want to hide particular columns, then you need to add display class on the column. Check out https://datatables.net/extensions/responsive/examples/display-control/classes.html for more details. For your case, adding below code will hide 2 columns.

    <tr>
<th></th>
<th></th>
<th class="min-phone-l"></th>
<th class="min-phone-l"></th>
<th data-priority="1"></th>
</tr>

For column reordering, check colReorder here : https://datatables.net/extensions/colreorder/

conditional formatting of cells using if else? or hiding columns in DT package in R

Okay so I will answer my own question here, the code needed another list within a list using "visible = FALSE" as @LocoGris pointed. Without adding a list within the list, ellipsis was getting compromised.

datatable(
data_wide,
plugins = "ellipsis",
options = list(
columnDefs = list(list(
targets = c(2,3,4),
render = JS("$.fn.dataTable.render.ellipsis( 17, false )")
),list(visible=FALSE,targets = c(5,6,7)))
)
)%>%
formatStyle(
'book1',"b1",
color = "lightgreen",
border = '2px solid #FFFFFF',
backgroundColor = styleEqual(c(0, 1), c('lightblue', 'lightgreen'))) %>%
formatStyle(
'book2',"b2",
border = '2px solid #FFFFFF',
color = "lightgreen",
backgroundColor = styleEqual(c(0, 1), c('lightblue', 'lightgreen'))) %>%
formatStyle(
'book3',"b3",
border = '2px solid #FFFFFF',
color = "lightgreen",
backgroundColor = styleEqual(c(0, 1), c('lightblue', 'lightgreen')) )

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)

Make DT in-table filters responsive to subsetted data

That's because iris$Species is a factor and the DT filter uses the levels of this factor, which still are setosa, virginica and versicolor after the dplyr filtering, even though some of them have 0 occurence. To drop the levels with 0 occurence, use the droplevels function:

iris2 <- reactive({
droplevels(iris1() %>% filter(Species %in% input$SpeciesPick))
})

Is there a way to hide columns of a tibble from the console output in R?

It depends on exactly what you mean by "hiding". You won't easily be able to have it there but completely inaccessible to the user. However, it's easy to hide it when printing, by adding a new class to it and defining a print method for that class.

For example:

hideHelper <- function (df) {
class(df) <- c("hideHelper", class(df))
df
}

print.hideHelper <- function(x, ...) {
x$helper <- NULL
NextMethod(x, ...)
}

# create some data with a column named "helper":
library(tibble)
data <- tibble(
x = LETTERS[1:10],
y = c(10:1),
z = runif(10),
helper = 1:10
)

# Tell R not to print that column
data <- hideHelper(data)
data
#> # A tibble: 10 × 3
#> x y z
#> <chr> <int> <dbl>
#> 1 A 10 0.844
#> 2 B 9 0.150
#> 3 C 8 0.986
#> 4 D 7 0.581
#> 5 E 6 0.774
#> 6 F 5 0.333
#> 7 G 4 0.787
#> 8 H 3 0.967
#> 9 I 2 0.693
#> 10 J 1 0.0949

# But it's still there:
names(data)
#> [1] "x" "y" "z" "helper"
data$helper
#> [1] 1 2 3 4 5 6 7 8 9 10

Created on 2022-01-30 by the reprex package (v2.0.1.9000)

Originally I said you won't be able to do it at all, but that's not true. R has several opaque object types. For example, you could put the columns you don't want the user to see into objects allocated in compiled C or C++ code and accessed with "external pointers", and the user would only be able to see whatever aspects of them you wrote code to allow them to see.



Related Topics



Leave a reply



Submit