1

@XiongbingJin's example on Stack Overflow allows the user first to display a full dataset and to second to change the columns to display with a checkboxGroupInput .

I would like some help to make something different :

What I want :

  1. datatable displaying begins with an arbitrary list of column (ex :carb, wt, drat of mtars datset ) and not the full dataset.
  2. The user can complete the list to display with the checkboxGroupInput. (ex: add vs).

enter image description here

@XiongbingJin example :

 library(shiny)
 runApp(list(
 ui = basicPage(
 selectInput("select", "Select columns to display", names(mtcars), multiple = 
 TRUE),
 h2('The mtcars data'),
  dataTableOutput('mytable')
),
 server = function(input, output) {
 output$mytable = renderDataTable({
  columns = names(mtcars)
  if (!is.null(input$select)) {
    columns = input$select
  }
  mtcars[,columns,drop=FALSE]
 })
 }
))
Wilcar
  • 2,349
  • 2
  • 21
  • 48
  • I believe that in the example that you show, all the columns are shown since the default items in the input are `names(mtcars)`. Changing that to a vector containing only the desired columns should do. – Marc P Jun 12 '18 at 14:41

1 Answers1

1

As suggested by @Marc P, you can just focus on a subset of names(mtcars) by providing it to the selected argument. This has also the advantage to get rid of the case where input$select is null.

library(shiny)

ui = basicPage(
  selectInput("select", "Select columns to display", 
              names(mtcars),
              selected = names(mtcars)[c(1, 3)], # display 1st and 3rd variables 
              multiple = TRUE),
  h2('The mtcars data'),
  dataTableOutput('mytable')
)

server = function(input, output) {
  output$mytable = renderDataTable({
    mtcars[, input$select, drop=FALSE]
  })
}

shinyApp(ui, server)
kluu
  • 2,848
  • 3
  • 15
  • 35
  • Just an issue : I managed to implement your answer in my real data context and have met an error when input$select is null (index = null with 0 columns). – Wilcar Jun 13 '18 at 06:36
  • Are you loading your data through the shinyapp, e.g using `fileInput`? If not, in what part of the code do you load it? – kluu Jun 13 '18 at 06:44
  • I work in leaflet context : the datable is displayed after a click on shape. – Wilcar Jun 13 '18 at 06:48
  • When the table it will returns me the index null error – Wilcar Jun 13 '18 at 06:49
  • Do you have any repo where you host this part of the code? Because it's a little bit hard for me to infer the origin of the problem – kluu Jun 13 '18 at 06:51
  • Does that have something to do with the line `exped <- inner_join(exped2, geocode, by =c("place" = "place"))`? Only `places_geocode` is defined. I was able to run the app on my computer (it looks great). – kluu Jun 13 '18 at 08:20
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/173047/discussion-between-kluu-and-wilcar). – kluu Jun 13 '18 at 08:50