Reputation: 480
Below application will give you the secondary DT table when the particular row is selected on Main DT table. Can we alter this functionality. Like instead of this, can we have reactive tabs below the main DT table. So when particualar row is selected on main DT table (say Mazda RX4), tab naming "Mazda RX4" should open having details of it. When another row is selected(say valiant), another tab should open with this name and its contents. Can we achieve this
---
title: "MRE"
author: ""
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: scroll
runtime: shiny
---
Page {data-orientation=columns}
=======================================================================
Column {data-width=650}
-----------------------------------------------------------------------
### .
```{r}
require(DT)
library(dplyr)
library(tibble)
sliderInput("filter", label = "Filter by cyl", min = 4, max = 8, step = 2, value = 6)
filteredTable_data <- reactive({
mtcars %>% rownames_to_column() %>% ##dplyr's awkward way to preserve rownames
filter(., cyl == input$filter) %>% column_to_rownames()
})
##explicit assignment to output ID
DT::dataTableOutput("filteredTable")
output$filteredTable <- DT::renderDataTable({
datatable(
filteredTable_data(),
selection = list(mode = "multiple"),
caption = "Filtered Table (based on cyl)"
)
})
filteredTable_selected <- reactive({
ids <- input$filteredTable_rows_selected
filteredTable_data()[sort(ids),] ##sort index to ensure orig df sorting
})
##anonymous
DT::renderDataTable({
datatable(
filteredTable_selected(),
selection = list(mode = "none"),
caption = "Table that gets data from unfiltered original data"
)
})
```
Upvotes: 1
Views: 164
Reputation: 7106
In the app below, each time you select a specific row, a new tab with the rowname as title appears selected. When two rows are chosen, the tab with the latest choice will appear selected. Please, let me know any doubts.
library(shiny)
library(DT)
library(tidyverse)
ui <- fluidPage(
sliderInput("filter", label = "Filter by cyl", min = 4, max = 8, step = 2, value = 6),
dataTableOutput("filteredTable"),
uiOutput('selected_with_tabs')
)
server <- function(input, output, session) {
filteredTable_data <- reactive({
mtcars %>%
rownames_to_column() %>% ##dplyr's awkward way to preserve rownames
filter(., cyl == input$filter) %>%
column_to_rownames()
})
output$filteredTable <- DT::renderDataTable({
datatable(
filteredTable_data(),
selection = list(mode = "multiple"),
caption = "Filtered Table (based on cyl)"
)
})
filteredTable_selected <- eventReactive(input$filteredTable_rows_selected, {
ids <- input$filteredTable_rows_selected
rownames(filteredTable_data()[ids, ])
})
#dynamic tabs
output$selected_with_tabs <- renderUI({
tabs <- filteredTable_selected() %>%
map2(1:length(.), ~ tabPanel(title = .x, {
dataTableOutput(str_c('selected_', .y)) #contents of each tab
}))
tabsetPanel_wselection <- partial(tabsetPanel, selected = filteredTable_selected()[length(filteredTable_selected())])
tagList(exec(tabsetPanel_wselection, !!!tabs))
})
#server side of tab content.
observeEvent(input$filteredTable_rows_selected, {
walk2(1:length(filteredTable_selected()), filteredTable_selected(), ~ {
output[[str_c('selected_', .x)]] <- renderDataTable(mtcars[.y, ]) })
})
}
shinyApp(ui, server)
Upvotes: 2