Checkbox on table or dataframe
Asked Answered
T

3

9

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")
    ))))
Tolentino answered 27/9, 2013 at 3:24 Comment(2)
I have almost exactly the same question -- have you figured it out? I'm rendering an R data.frame using renderTable, and I want to have a column of checkboxes (in the table) that, when clicked, changes the corresponding entry in the column of the R data.frame to either TRUE or FALSE, depending on whether the checkbox is currently checked. Can't find any examples on how this might be accomplished.Emotionalize
not really. havent really figured it out yetTolentino
E
4

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.

Estimate answered 25/4, 2016 at 13:2 Comment(0)
W
0

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, ]
  })
})
Wells answered 27/9, 2013 at 5:51 Comment(1)
you missed the point. I want to display all the data as a table on the sidepanel with a checkbox for each row (not the name Row). The rows selected are displayed as a table on the mainPanel.Tolentino
D
0

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

Devolution answered 10/11, 2018 at 16:53 Comment(2)
Is it possible to have it checked by default?Redd
@Redd Absolutely, just change the syntax to checked, here is the reference, developer.mozilla.org/en-US/docs/Web/HTML/Element/Input/…Devolution

© 2022 - 2024 — McMap. All rights reserved.