0

I am currently working on an application form using Shiny. My main page will consist of a table that will have a click button which will open a new modal window that will display the application form details. Once the save button is clicked the data will get stored in my DB in the backend. I have given a sample example of my app using mtcars.

My question is a two parter.

  1. I have added checkbox to the main page rows and wish to track the click of these check boxes so that I can check the required ones and using their click event save some other value in the DB. How can I track this checkbox click event?

  2. I need a single checkbox click to select about 150 or more check boxes simultaneously. Is there a way to capture all these click events together in an array or something and make use of them later?

Here is my code:

rm(list = ls())
library(DT)
library(shiny)
library(shinyBS)
library(shinyjs)
library(shinydashboard)

shinyInput <- function(FUN, len, id, ...) {inputs <- character(len)
for (i in seq_len(len)) {
  inputs[i] <- as.character(FUN(paste0(id, i), ...))}
inputs
}

ui <- dashboardPage(
  dashboardHeader(title = "Simple App"),
  dashboardSidebar(
    sidebarMenu(id = "tabs",
                menuItem("Menu Item 1", tabName = "one", icon = icon("dashboard"))
    )
  ),
  dashboardBody(
    tabItems(
      tabItem(tabName = "one",h2("Datatable Modal Popup"),
              DT::dataTableOutput('my_table'),uiOutput("popup")
      )
    )
  )
)

server <- function(input, output, session) {
  my_data <- reactive({
    testdata <- mtcars
    as.data.frame(cbind(View = shinyInput(actionButton, nrow(testdata),'button_', label = "View", onclick = 'Shiny.onInputChange(\"select_button\",  this.id)' ),
                        Check = shinyInput(checkboxInput,label = "Check", nrow(testdata),  'box_', value = FALSE),testdata))
  })  
  output$my_table <- DT::renderDataTable(my_data(),selection = 'single',options = list(searching = FALSE,pageLength = 10),server = FALSE, escape = FALSE,rownames= FALSE)

  SelectedRow <- eventReactive(input$select_button,{
    as.numeric(strsplit(input$select_button, "_")[[1]][2])
  })

  observeEvent(input$select_button, {
    toggleModal(session, "modalExample", "open")
  })

  DataRow <- eventReactive(input$select_button,{
    my_data()[SelectedRow(),2:ncol(my_data())]
  })

  output$popup <- renderUI({
    bsModal("modalExample", paste0("Data for Row Number: ",SelectedRow()), "", size = "large",
            column(12,                   
                   DT::renderDataTable(DataRow())

            )
    )
  })

}

shinyApp(ui, server)
Uwe
  • 41,420
  • 11
  • 90
  • 134
  • What have you tried already? Did you try searching on Google for an example on using a Shiny checkbox? – TheComeOnMan Nov 02 '16 at 06:32
  • I did try and I have managed to reproduce the example from here for the check box. http://stackoverflow.com/questions/37886104/shiny-true-or-false-value-when-check-a-checkbox-in-datatable This is working for the checkbox and I am able to track the true/false of the checkbox. Trying to figure out how to configure it for my needs. I don't have any idea about the second part. I did not find any examples on the net. – Sanjay Jayaraman Nov 02 '16 at 07:24
  • But the above link is not helpful. I am able to see the click event only if i mention the row number for that particular checkbox. My goal is to get the row number of the checkbox from the click event itself. So I am still trying to find a solution for part 1. – Sanjay Jayaraman Nov 02 '16 at 10:02
  • You can have a look at this [link](http://stackoverflow.com/questions/37875078/shiny-checkbox-in-table-in-shiny) for solution to the first part. Hope it helps! – SBista Nov 07 '16 at 09:08

0 Answers0