0
votes

I have a data table within my shiny app. I would like to hide the first column. Below is part of my code. This part "datatable(population_, options=list(columnDefs = list(list(visible=FALSE, targets=c(1)))))" is assumed to do the job but the column appears in the data table.

Thanks for your help.

Nader

#print data in data tab
     bardata_ <- reactive ({
    out <- population_ %>%
      filter (County %in% input$county,
              Year %in% input$years,
              Sex %in% input$sex)
    return(out)
  })
  output$data <- DT::renderDataTable ({
    datatable(population_, options=list(columnDefs = list(list(visible=FALSE, targets=c(1)))))

    (bardata_())
  })
2

2 Answers

1
votes

Your function return not population_, but bardata_()
Use this:

  bardata_ <- reactive ({
    out <- population_ %>%
      filter (County %in% input$county,
              Year %in% input$years,
              Sex %in% input$sex)
    return(out)
  })
  output$data <- DT::renderDataTable ({
    datatable(population_, options=list(columnDefs = list(list(visible=FALSE, targets=c(1)))))

  })

Check here

library(shiny)
ui <- fluidPage(
  DT::dataTableOutput("dt")
)

server <- function(input, output, session) {

  output$dt <- DT::renderDataTable({

   DT::datatable(iris, options=list(columnDefs = list(list(visible=FALSE, targets=c(1)))))
    (iris)
  })
}

shinyApp(ui, server)

AND

ui <- fluidPage(
  DT::dataTableOutput("dt")
)

server <- function(input, output, session) {

  output$dt <- DT::renderDataTable({

   DT::datatable(iris, options=list(columnDefs = list(list(visible=FALSE, targets=c(1)))))

  })
}

shinyApp(ui, server)
0
votes

I think you can simply do a select statement on your data that you feed to datatable(). Using a "-" in front of columns will remove them.

datatable(population_ %>% select(-First_column_name),...)

With base R:

population_[,2:ncol(population_)]