library(shiny)library(DT)# ratio 选择行 ***shinyApp( ui = fluidPage( h1('select rable row'), fluidRow( column(9,DT::dataTableOutput('tb1')), column(3, verbatimTextOutput('x1')) ) ), server = function(input, output) { #定义table df_iris = iris[1:6,] #定义产生button的函数段 shinyInput <- function(FUN, len, id, ...) { inputs <- character(len) for (i in seq_len(len)) { inputs[i] <- as.character(FUN(paste0(id, i), ...)) } inputs } #对表格进行包装,增加button addRadioButtons <- paste0('<input type="radio" name="row', '" value="', 1:nrow(df_iris), '">',"") df_iris_buttons = cbind(Pick=addRadioButtons,df_iris) #输出表格设置 output$tb1 = DT::renderDataTable( df_iris_buttons , server = FALSE, escape = FALSE, selection = 'single' ) #输出文字设置 output$x1 = renderPrint({ s = input$tb1_row_last_clicked if (length(s)) { cat('These rows were selected:\n\n') cat(s, sep = ', ') } }) } )
library(shiny)library(DT)shinyApp( ui = fluidPage( title = 'Radio buttons in a table', DT::dataTableOutput('foo'), verbatimTextOutput('sel') ), server = function(input, output, session) { m = matrix( as.character(1:5), nrow = 12, ncol = 5, byrow = TRUE, dimnames = list(month.abb, LETTERS[1:5]) ) for (i in seq_len(nrow(m))) { m[i, ] = sprintf( '<input type="radio" name="%s" value="%s"/>', month.abb[i], m[i, ] ) } rownames(m)[1] = '1st' # I've changed here !!! m 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]])) }) })Hello,