3

I have this reproducible code below using the Iris dataset. I am wondering how I can create an additional table below the current table or in a better scenario how I can create a tabset tab to include an additional table.

Code and Screenshot of Output below:

library(shiny)
library(DT)    

shinyApp(
  ui = fluidPage(
    textInput('search2', "Search 2"),
    DTOutput('dt')
  ),
  server = function(input, output, session) {
    
    DTproxy <- dataTableProxy("dt")
    output$dt = renderDT(iris)
    
    observeEvent(input$search2, {
      updateSearch(DTproxy, keywords = list(global = input$search2, columns = NULL))
    })
    
  })

enter image description here

ismirsehregal
  • 30,045
  • 5
  • 31
  • 78
Jaskeil
  • 1,044
  • 12
  • 33

1 Answers1

3

I guess this is a follow-up on this?

Here is a simple example with 3 tables and 3 tabs all depending on a global search field. We could reduce the code via lapply or modules but I think this is more understandable at first:

library(shiny)
library(DT)

ui <- fluidPage(
  titlePanel("Tabsets"),
  sidebarLayout(
    sidebarPanel(
      textInput('search', "Search"),
    ),
    mainPanel(
      tabsetPanel(id = "tabsetPanelID",
                  type = "tabs",
                  tabPanel("Tab1", DTOutput('DT1')),
                  tabPanel("Tab2", DTOutput('DT2')),
                  tabPanel("Tab3", DTOutput('DT3'))
      )
    )
  )
)

server <- function(input, output, session) {
  output$DT1 = renderDT(iris)
  DTProxy1 <- dataTableProxy("DT1")
  
  output$DT2 = renderDT(iris)
  DTProxy2 <- dataTableProxy("DT2")
  
  output$DT3 = renderDT(iris)
  DTProxy3 <- dataTableProxy("DT3")
  
  observeEvent(c(input$search, input$tabsetPanelID), {
    updateSearch(DTProxy1, keywords = list(global = input$search, columns = NULL))
    updateSearch(DTProxy2, keywords = list(global = input$search, columns = NULL))
    updateSearch(DTProxy3, keywords = list(global = input$search, columns = NULL))
  })
}

shinyApp(ui, server)
ismirsehregal
  • 30,045
  • 5
  • 31
  • 78
  • Correct! I have been wrangling with this the past few days, I may update my code with data relevant to my problem. How does the search work? Are all the tables connected/joined making the filter apply across sheets or does the search just look for ketwords within the tables? – Jaskeil Feb 03 '22 at 20:57
  • 1
    The tables are not connected. `updateSearch` is applied to each table separately looking for the keyword in all columns - with the `columns` parameter you could make the search column specific. – ismirsehregal Feb 03 '22 at 21:01
  • Thanks, so I was able to recreate what you posted, however, is it hosted locally just on my computer? Is that something I can share with other users? – Jaskeil Feb 03 '22 at 21:12
  • 1
    I guess you are developing in RStudio? Then yes it's hosted on your local PC. You can share it with others in the same local network as described [here](https://stackoverflow.com/a/26800937/9841389) or you deploy it on [shinyapps.io](https://www.shinyapps.io/) (free account needed) - it's a web app not a standalone html file. – ismirsehregal Feb 03 '22 at 21:18
  • Gotcha makes sense, is it possible I can create that same "Search 2" filter in a markdown document? Where the search is applicable across tables similar to how you did it with the iris dataset – Jaskeil Feb 03 '22 at 21:20
  • 1
    Under the hood `updateSearch` uses a JS function. You might be able to create some custom JS code using the same function and attach it to the datatable widget via `htmlwidgets::onRender` - but I fear this won't be as straightforward as the `shiny` implementation. – ismirsehregal Feb 03 '22 at 21:25
  • 1
    Okay, good to know. I will go with the shiny approach – Jaskeil Feb 03 '22 at 21:26
  • Also, how do I add in the download excel or csv buttons for the code above? – Jaskeil Feb 03 '22 at 21:28
  • 1
    Check [this answer](https://stackoverflow.com/a/50040562/9841389). – ismirsehregal Feb 03 '22 at 21:30