2016-06-17 3 views
0

Ich habe ein Problem mit R glänzend Code, was muss ich tun, wenn ich den wahren oder falschen Wert in der Checkbox sehen möchte? weil, wenn ich versuche, zu drucken (input $ row1) das Ergebnis ist NULL, und wenn ich check in das Kontrollkästchen gibt es keine Antwort in print-Anweisung. Dies ist der Code:Shiny - True oder False Wert, wenn ein Kontrollkästchen in der Datentabelle

library(shiny) 
library(DT) 
mymtcars = mtcars 
mymtcars$id = 1:nrow(mtcars) 
runApp(
    list(ui = pageWithSidebar(
    headerPanel('Examples of DataTables'), 
    sidebarPanel(

    ), 
    mainPanel(
     uiOutput("mytable") 
    ) 
) 
    , server = function(input, output, session) { 

    output$mytable = renderUI({ 
     addCheckboxButtons <- paste0('<input type="checkbox" id="row', mymtcars$id, '">',"") 
     yyy<- cbind(mymtcars[, names(mymtcars[,2:4]), drop=FALSE],Pick=addCheckboxButtons) 

     #try 
     print(input$row1) #didnt work 
     print(input$row2) #didnt work 

     #Display table with checkbox buttons 
     list(
     renderDataTable(yyy, 
        options = list(orderClasses = TRUE, 
            lengthMenu = c(5, 25, 50), 
            pageLength = 25, 
            callback = JS("function(table) { 
               table.on('change.dt', 'tr td input:checkbox', function() { 
               setTimeout(function() { 
               Shiny.onInputChange('rows', $(this).add('tr td input:checkbox:checked').parent().siblings(':last-child').map(function() { 
               return $(this).text(); 
               }).get()) 
               }, 10); 
               }); 
    }")),escape = FALSE 

        )) 
     } 
) 
    } 
    ) 
        ) 

Antwort

0

Siehe kurzes Beispiel (nur für row1)

1) können Sie verwenden Shiny.bindAll

2) Sie müssen in observe

library(shiny) 
library(DT) 
mymtcars = mtcars 
mymtcars$id = 1:nrow(mtcars) 
runApp(
    list(ui = pageWithSidebar(
    headerPanel('Examples of DataTables'), 
    sidebarPanel(
     checkboxGroupInput('show_vars', 'Columns to show:', names(mymtcars), 
         selected = names(mymtcars)) 
     ,textInput("collection_txt",label="Foo") 
    ), 
    mainPanel(
     DT::dataTableOutput("mytable") 
    ) 
) 
    , server = function(input, output, session) { 

    observe({ 
     print(input$row1) 
    }) 

    output$mytable = DT::renderDataTable({ 
     #Display table with checkbox buttons 
     DT::datatable(cbind(Pick=paste0('<input type="checkbox" id="row', mymtcars$id, '" value="', mymtcars$id, '">',""), mymtcars[, input$show_vars, drop=FALSE]), 
        options = list(orderClasses = TRUE, 
            lengthMenu = c(5, 25, 50), 
            pageLength = 25 , 

            drawCallback= JS(
            'function(settings) { 
             Shiny.bindAll(this.api().table().node());}') 
        ),selection='none',escape=F) 


    }) 

    }) 
) 
drucken, um zu sehen
Verwandte Themen