0

Context

I'm building an app whith Rshiny. I created a dynamic navbar. Now, i would like to insert a datatable at each navbar's item (tabPanel). But i don't want to create a table per item in navbar. So i would like to know if it's possible to create only 1 table which update itself with the navbar's selected item ??

Code to create dynamical navbar

runApp(list(
  ui = fluidPage(
    navbarPage(theme=shinytheme("paper"),title="test",
      tabPanel(uiOutput('mytabs'))
    )
  ),
  server = function(input, output, session){
    output$mytabs = renderUI({
      nTabs = length(unique(iris$Species))
      myTabs = lapply(unique(iris$Species)[1:nTabs], tabPanel)
      do.call(tabsetPanel, myTabs)
    })
  }
))

Now i woulk like to display datatable(subset(iris,Species=="value of navbar")) at each navbar's item

Can someone explain me how to do ?

Orhan Yazar
  • 909
  • 7
  • 19

1 Answers1

1

Using question here, you can define a function and call it as below. Note that csv button will only work in a browser.

library(shiny)
library(shinythemes)

runApp(list(
   ui = fluidPage(
        navbarPage(theme=shinytheme("paper"),title="test",
           tabPanel(uiOutput('mytabs'))
     )
  ),
server = function(input, output, session){

createTabs <- function(species_r){
  tabPanel(title = paste("Data", species_r, sep=" "), 
           datatable(subset(iris,Species==species_r),rownames = FALSE,
                     extensions = 'Buttons', options = list(dom = 'Bfrtip', buttons = c('copy', 'csv',I('colvis'))))
           )
}

output$mytabs = renderUI({
  nTabs = length(unique(iris$Species))
  myTabs = lapply(unique(iris$Species)[1:nTabs], createTabs)
  do.call(tabsetPanel, myTabs)
  })
 }
))
A. Suliman
  • 12,923
  • 5
  • 24
  • 37
  • I didn't get where do i have to insert it. an you please tell me ? – Orhan Yazar May 18 '18 at 07:39
  • Do you know how can i insert an observeevent to choose columns i want to display ? – Orhan Yazar May 22 '18 at 09:00
  • observeEvent is [Event handler](http://shiny.rstudio.com/reference/shiny/1.0.2/observeEvent.html), I think you need [Checkbox Group Input Control](https://shiny.rstudio.com/reference/shiny/1.0.2/checkboxGroupInput.html). If I am correct let me know and I will update the answer. – A. Suliman May 22 '18 at 09:19
  • Yes, when i display my tables, there are too many columns so the display is ugly so i would like the user to be able to chose columns he wants to display. – Orhan Yazar May 22 '18 at 09:31
  • 1
    In that case, can I suggest [4. ColVis](https://rstudio.github.io/DT/extensions.html) from DT package. – A. Suliman May 22 '18 at 09:39
  • Thank you very much, everything is ok now !! – Orhan Yazar May 22 '18 at 09:50
  • 1
    you are most welcome. I update the answer with csv and copy for future reader. – A. Suliman May 22 '18 at 09:54
  • Thank you very much. I don't know why but when i click on copy or csv, nothing happens. It works when i open the app in browser. – Orhan Yazar May 22 '18 at 10:00
  • 1
    I think because it uses JS library or may be a compatibility issue I don't know frankly. So yes **csv** only work in a browser. **copy** work like Ctrl+C then you have to open a new Excel sheet or word and Press Ctrl+V or Paste for example. – A. Suliman May 22 '18 at 10:03