-2

I am trying to create a grid which has its rows and column names as variables in a particular data set so that I can map individual rows for regression in r on backend (where row names will be dependent variable and column names will be independent variables). Can anybody help on how to approach towards this problem.

I am trying to get something like this on my app. when a user selects lets say app and alerts and when he presses the submit button it does the regression on the backend i.e website ~ app + alerts

I dont have any code right now because i am very new to r shiny and dont know how to create this grid with checkboxes. If anyone could just guide me on how to approach that would be very helpful.

Thanks

1 Answers1

1

You can create table with checkboxes (or other active elements) using DT package. Please see the code below (based on "Showing as Checkbox" anwser:

library(shiny)
library(DT)

# create data.frame of row/column names of futre datable
my_df <- data.frame(nam = c("Favorite1", "Favorite2"))

runApp(
  list(
    ui = fluidRow(
      sidebarLayout(
      h2("Checkboxes Datatable"),
      DT::dataTableOutput("mytable", width = "1%")),
      mainPanel(h2("Selected"),
      tableOutput("checked"))
    ),
    server = function(input, output) {

    # helper function for making checkbox
    shinyInput <- function(FUN, len, id, ...) { 
      inputs <- character(len) 
      for (i in seq_len(len)) { 
        inputs[i] <- as.character(FUN(paste0(id, i), label = NULL, ...)) 
      } 
      inputs 
    } 
    # datatable with checkbox
    output$mytable <- DT::renderDataTable( 
      expr = {
        df <- data.frame(
        #  my_df,
          my_df,
          Favorite1 = shinyInput(checkboxInput, nrow(my_df), "cbox1"), 
          Favorite2 = shinyInput(checkboxInput, nrow(my_df), "cbox2")
        )
        names(df)[1] <- " "
        df
      }, 
      rownames = FALSE,
      server = FALSE, 
      escape = FALSE, 
      options = list(
        ordering = FALSE,
        searching = FALSE,
        paging = FALSE,
        info = FALSE,
        preDrawCallback = JS("function() { 
          Shiny.unbindAll(this.api().table().node()); }"
        ), 
        drawCallback = JS("function() { 
          Shiny.bindAll(this.api().table().node()); } "
        ) 
      )
    )

    # helper function for reading checkbox
    shinyValue <- function(id, len) { 
      unlist(
        x = lapply(
          X = seq_len(len), 
          FUN = function(i) { 
            value = input[[paste0(id, i)]] 
            if (is.null(value)) {
              NA
            } else {
              value
            }  
          }
        )
      ) 
    } 
    # output read checkboxes
    output$checked <- renderTable({
      data.frame(
        Favorite1 = shinyValue("cbox1", nrow(my_df)),
        Favorite2 = shinyValue("cbox2", nrow(my_df))
      )
    }
  )
 }
  )
)

Output (checkboxes clicked in the upper table show their status at lower one): Checkboxes

Artem
  • 3,304
  • 3
  • 18
  • 41