使用Shiny DataTables选择所有行

时间:2017-02-06 15:54:11

标签: r shiny dt

我正在尝试创建一个操作按钮来选择表格的所有行(table_contents),但我无法让它正常运行。我的server.R文件中的代码如下。

渲染数据表以查看加载的CSV文件

output$table_contents = renderDataTable({
inFile = input$file1
if (is.null(inFile))
  return(NULL)
inFile = read.csv(inFile$datapath)
inFile = inFile[1:68]

datatable(inFile,
          rownames = F, extensions = 'Buttons', options = list(
            dom = 'Bfrtip',
            buttons = c('copy', 'csv', 'excel', 'pdf', 'print'),
            scrollX = TRUE,
            fixedColumns = TRUE
          )
)
})

proxy = dataTableProxy('table_contents')


selectedData = reactive({
inFile = input$file1
if (is.null(inFile))
  return(NULL)
inFile = read.csv(inFile$datapath,na = '' )
inFile = inFile[1:68]

rows_selected = as.numeric(input$table_contents_rows_selected)
selected_table = inFile[rows_selected,]
return(selected_table)
})

示例应用

 library(shiny)
 shinyApp(
 ui = fluidPage(
 sidebarLayout(
  sidebarPanel(width=3,
               fileInput('file1', 'Choose CSV File',
                         accept=c('text/csv', 
                                  'text/comma-separated-values,text/plain', 
                                  '.csv')),
               tags$hr(),
               checkboxInput('header', 'Header', TRUE),
               radioButtons('sep', 'Separator',
                            c(Comma=',',
                              Semicolon=';',
                              Tab='\t'),
                            ','),
               radioButtons('quote', 'Quote',
                            c(None='',
                              'Double Quote'='"',
                              'Single Quote'="'"),
                            '"'),
               tags$hr(),
               actionButton(inputId = 'submit4', label = 'Save the completed Assessment to the database'),
               bsTooltip("submit4", "Save the information to the database", placement = "bottom",trigger = "hover")

  ),
  mainPanel(
    dataTableOutput('table_contents'),
    tags$hr(),
    dataTableOutput('selected')
  )
)
),
server = function(input, output) {

  output$table_contents = renderDataTable({
    inFile = input$file1
    if (is.null(inFile))
      return(NULL)
    inFile = read.csv(inFile$datapath)
    inFile = inFile[1:68]

    datatable(inFile,
              rownames = F, extensions = 'Buttons', options = list(
                dom = 'Bfrtip',
                buttons = c('copy', 'csv', 'excel', 'pdf', 'print'),
                scrollX = TRUE,
                fixedColumns = TRUE
              )
    )
  })

  proxy = dataTableProxy('table_contents')

  selectedData = reactive({
    inFile = input$file1
    if (is.null(inFile))
      return(NULL)
    inFile = read.csv(inFile$datapath,na = '' )
    inFile = inFile[1:68]

    rows_selected = as.numeric(input$table_contents_rows_selected)
    selected_table = inFile[rows_selected,]
    #selected_table[is.na(selected_table)] = ''
    return(selected_table)
  })


  output$selected = renderDataTable({
    datatable(selectedData(), extensions = 'Buttons', options = list(
      dom = 'Bfrtip',
      buttons = c('copy', 'csv', 'excel', 'pdf', 'print'),
      scrollX = TRUE,
      fixedColumns = TRUE
    )
    )

  })
}
)

shinyApp(ui = ui, server = server)

0 个答案:

没有答案