Скачать выбранные строки в DT - PullRequest
0 голосов
/ 22 октября 2018

Есть ли способ загрузить выбранные строки с расширением buttons?Если нет, есть ли способ добавить пользовательские кнопки в таблицу вверху слева?Я знаю, как загрузить выбранные строки.Я обнаружил, что мы можем добавить пользовательскую кнопку для выбора столбцов (https://github.com/rstudio/DT/issues/397)

Я добавил 2 пользовательских кнопки (упомянутых в коде ниже).

library(shinydashboard)

header <- dashboardHeader(title = 'title')
sidebar <- dashboardSidebar(
  sidebarMenu(
    menuItem('dashboard', tabName = 'dashboard', icon = icon('dashboard'))
  )
)
body <- dashboardBody(
 fluidPage(fluidRow(
  column(2,   
      actionButton("downloadData", "Download Selected Rows", icon = icon("download"), 
      style="color: #333; background-color: #FFF; border-color: #333")),
      useShinyalert(),
      column(2,  
      actionButton(inputId = "run", label = "Write Selected Rows to SQL", icon = icon("paper-plane"),
                       style="color: #333; background-color: #FFF; border-color: #333")),
     useShinyalert()
  )),

           box(
             title = 'box', width = NULL, status = 'primary',
             DT::dataTableOutput('table2')  
  )
)

ui<-dashboardPage(header, sidebar, body)

server = function(input, output) {
  output$table2 = DT::renderDataTable(
    iris, options = list(lengthChange = FALSE)
  )
}

shinyApp(ui, server)

1 Ответ

0 голосов
/ 22 октября 2018

Не уверен, в каком формате вы хотите загрузить выбранные строки, но вот пример, хранящий CSV-файл, основанный на выборе (вам нужна кнопка download):

library(shiny)
library(shinyalert)
library(shinydashboard)

header <- dashboardHeader(title = 'title')
sidebar <- dashboardSidebar(
  sidebarMenu(
    menuItem('dashboard', tabName = 'dashboard', icon = icon('dashboard'))
  )
)
body <- dashboardBody(
  fluidPage(fluidRow(
    column(2,   
           downloadButton("downloadData", "Download Selected Rows", icon = icon("download"), 
                          style="color: #333; background-color: #FFF; border-color: #333")),
    useShinyalert(),
    column(2,  
           actionButton(inputId = "run", label = "Write Selected Rows to SQL", icon = icon("paper-plane"),
                        style="color: #333; background-color: #FFF; border-color: #333")),
    useShinyalert()
  )),
  p(),
  box(
    title = 'box', width = NULL, status = 'primary',
    DT::dataTableOutput('table2')  
  )
)

ui<-dashboardPage(header, sidebar, body)

server = function(input, output) {
  output$table2 = DT::renderDataTable(
    iris, options = list(lengthChange = FALSE)
  )

  output$downloadData <- downloadHandler(
    filename = function() {
      paste0(gsub(" ","_", gsub(":",".", Sys.time())),".csv")
    },
    content = function(file) {
      write.table(iris[input$table2_rows_selected,], file, row.names = FALSE)
    }
  )

}

shinyApp(ui, server)
...