firmo23
firmo23

Reputation: 8404

Specify a different name for a specific value to be used in the search bar of a datatable

I have a basic shiny app below with a datatable. Ι was wondering if it is possible to search in the search bar of the datatable for the species "setosa" but with another name which has to be set earlier. For example typing "sts" will give all the "setosa" species.

library(shiny)
library(DT)
shinyApp(
  ui = fluidPage(DTOutput('tbl')),
  server = function(input, output) {
    output$tbl = renderDT(
      iris, options = list(lengthChange = FALSE)
    )
  }
)

Upvotes: 2

Views: 59

Answers (1)

ismirsehregal
ismirsehregal

Reputation: 33397

You can define an abbreviation column and hide it (searching still works):

iris$Abbreviation <- "sts"
iris$Abbreviation[iris$Species == "versicolor"] <- "vrs"
iris$Abbreviation[iris$Species == "virginica"] <- "vrg"

hideCols <- which(names(iris) %in% c("Abbreviation"))

library(shiny)
library(DT)
shinyApp(
  ui = fluidPage(DTOutput('tbl')),
  server = function(input, output) {
    output$tbl = renderDT(
      iris, options = list(lengthChange = FALSE, columnDefs = list(list(visible=FALSE, targets=hideCols)))
    )
  }
)

Upvotes: 2

Related Questions