geodex
geodex

Reputation: 1249

Checkbox on table or dataframe

How do you select rows of data from a dataframe or table using checkboxes? I've done the following code but it seems the checkbox items are columns and does not really display the results.

Thanks for your help.

server.R

   shinyServer(function(input, output) {
       dataset<-reactive({
         data(cars)
         cars
       })

       output$choose_data <- renderUI({
         checkboxGroupInput("dtab", "Data Table", dataset()) 
    })

       dataset2<-reactive({
         input$dtab         
    })      

       output$data_table <- renderTable({
         data2()                    
       })
    })

ui.R

   shinyUI(pageWithSidebar(
         headerPanel(""),

         sidebarPanel(
         uiOutput("choose_data"),
         br()
         ),

        mainPanel(
        wellPanel("Data", tableOutput("data_table")
    ))))

Upvotes: 9

Views: 4596

Answers (3)

Jiaxiang
Jiaxiang

Reputation: 883

Add some HTML tags and use `DT::datatable(escape = F) to display. The following code is pretty easy to interpret.

  1. <input type="checkbox" id="checkbox1" class="styled"> is a checkbox tag.
  2. DT::datatable(escape = F) builds a html page.

{r} library(tidyverse) tibble( x = '<input type="checkbox" id="checkbox1" class="styled">This is a checkbox' ) %>% DT::datatable(escape = F)

enter image description here

Upvotes: 0

Victorp
Victorp

Reputation: 13856

Hi you can try package ReporteRs, there's a function FlexTable for creating html table (or word table), an example :

library("shiny")
library("ReporteRs")
mymtcars <- head(mtcars)

# ui
ui <- fluidPage(
  tags$h1("Table with checkboxes"),
  tableOutput(outputId = "table"),
  br(),
  verbatimTextOutput(outputId = "out")
)
# server
server <- function(input, output) {
  output$table <- renderFlexTable({
    # Create checkboxes
    mymtcars$Name <- paste0('<label><input type="checkbox" id="car', seq_along(rownames(mymtcars)), '"> <span>', rownames(mymtcars), '</span></label>')
    mymtcars <- mymtcars[c("Name", names(mtcars))] # Put col 'Name' in the first place
    ft <- vanilla.table(mymtcars) # convert to FlexTable objet
    ft[, "Name", to = "header"] <- parLeft() # left align checkboxes
    ft[, "Name"] <- parLeft() # left align header
    return(ft)
  })
  # the inputs created are in input$car1, input$car2, ...
  output$out <- renderPrint({
    # results
    res <- unlist(lapply(1:nrow(mymtcars), function(i) input[[paste0("car", i)]]))
    print(res)
    if (any(res)) {
      print(rownames(mymtcars)[res])
    }
  })
}
# launch app
shinyApp(ui = ui, server = server)

The result looks like :

ft_example

For more informations about FlexTable objects you can look here.

Upvotes: 4

Kevin Ushey
Kevin Ushey

Reputation: 21285

An example. Notice how I set the labels, and use the id specified for the checkboxGroupInput, choose_row, as an input parameter for output$data_table. This can replace your server.R file.

shinyServer(function(input, output) {

  data(cars)
  cars <- cars[1:10,] ## limit ourselves to first 10 rows for example

  ## a UI element that we can use for selecting rows
  output$choose_data <- renderUI({
    labels <- setNames( as.list(1:nrow(cars)), paste("Row", 1:nrow(cars)) )
    checkboxGroupInput("choose_row", "Select Rows", labels)
  })

  ## render the data.frame at the selected rows
  output$data_table <- renderTable({
    cars[input$choose_row, ]
  })
})

Upvotes: 0

Related Questions