2016-11-17 1 views
0

this exampleからのコピー貼り付けがかなりあります(データの使用を除いて、私は他の答えの一部に優先します)。行列の代わりにテーブルを使用します。私はなぜそれが動作していないか把握することができません。Data.frame/data.tableを持つShiny Datatableのラジオボタン

library(shiny) 
library(DT) 
shinyApp(
    ui = fluidPage(
    title = 'Radio buttons in a table', 
    DT::dataTableOutput('foo'), 
    verbatimTextOutput('sel') 
), 
    server = function(input, output, session) { 

    m = data.table(
     month1 = month.abb, 
     A = '1', 
     B = '2', 
     C = '3', 
     QWE = runif(12) 
    ) 
     m[, A := sprintf(
     '<input type="radio" name="%s" value="%s"/>', 
     month1, m[, A] 
    )] 
     m[, B := sprintf(
     '<input type="radio" name="%s" value="%s"/>', 
     month1, m[, B] 
    )] 
     m[, C := sprintf(
     '<input type="radio" name="%s" value="%s"/>', 
     month1, m[, C] 
    )] 

    output$foo = DT::renderDataTable(
     m, escape = FALSE, selection = 'none', server = FALSE, 
     options = list(dom = 't', paging = FALSE, ordering = FALSE), 
     callback = JS("table.rows().every(function(i, tab, row) { 
      var $this = $(this.node()); 
      $this.attr('id', this.data()[0]); 
      $this.addClass('shiny-input-radiogroup'); 
     }); 
     Shiny.unbindAll(table.table().node()); 
     Shiny.bindAll(table.table().node());") 
    ) 
    output$sel = renderPrint({ 
     str(sapply(month.abb, function(i) input[[i]])) 
    }) 
    } 
) 
+0

奇妙 'それが動作します。 – Carl

答えて

3

問題はrownamesです。あなたはそれに追加された光沢のある属性をすべて得るrownamesの余分な列を持っていますが、ラジオボタンではないので、壊れてしまいます(エラーが出るはずです)。ここで

が作業バージョンです: `DT :: renderDataTable`の内側にあなたが` as.matrix(m)を使用している場合、

library(shiny) 
library(DT) 
shinyApp(
    ui = fluidPage(
    title = 'Radio buttons in a table', 
    DT::dataTableOutput('foo'), 
    verbatimTextOutput('sel') 
), 
    server = function(input, output, session) { 

    m = data.table(
     month1 = month.abb, 
     A = '1', 
     B = '2', 
     C = '3', 
     QWE = runif(12) 
    ) 
    m[, A := sprintf(
     '<input type="radio" name="%s" value="%s"/>', 
     month1, m[, A] 
    )] 
    m[, B := sprintf(
     '<input type="radio" name="%s" value="%s"/>', 
     month1, m[, B] 
    )] 
    m[, C := sprintf(
     '<input type="radio" name="%s" value="%s"/>', 
     month1, m[, C] 
    )] 

    output$foo = DT::renderDataTable(
     m, escape = FALSE, selection = 'none', server = FALSE, rownames=FALSE, 
     options = list(dom = 't', paging = FALSE, ordering = FALSE), 
     callback = JS("table.rows().every(function(i, tab, row) { 
        var $this = $(this.node()); 
        $this.attr('id', this.data()[0]); 
        $this.addClass('shiny-input-radiogroup'); 
    }); 
        Shiny.unbindAll(table.table().node()); 
        Shiny.bindAll(table.table().node());") 
    ) 
    output$sel = renderPrint({ 
     str(sapply(month.abb, function(i) input[[i]])) 
    }) 
    } 
) 
+0

ありがとう!それは単に 'rownames = FALSE'ですか、それは違うのでしょうか、それとも何か他にもありますか? – TheComeOnMan

+0

ちょうど 'rownames = FALSE' – Carl

+0

1列から1ヶ月しか選択できませんか? –

関連する問題