交互式闪亮表格下载和页面长度问题

问题描述 投票:0回答:1

下面的代码改编自https://gist.github.com/wch/4211337,完美地说明了我的挑战。我有两个主要问题:

  1. 我无法下载交互式表格;和
  2. 我不知道如何使表格打印为页面长度,例如显示 25 行并允许您切换到下一页。

这是代码:

服务器.r

data_sets <- c("mtcars", "morley", "rock")

shinyServer(function(input, output) {

  # Drop-down selection box for which data set
  output$choose_dataset <- renderUI({
    selectInput("dataset", "Data set", as.list(data_sets))
  })

  # Check boxes
  output$choose_columns <- renderUI({
    # If missing input, return to avoid error later in function
    if(is.null(input$dataset))
      return()

    # Get the data set with the appropriate name
    dat <- get(input$dataset)
    colnames <- names(dat)

    # Create the checkboxes and select them all by default
    checkboxGroupInput("columns", "Choose columns", 
                        choices  = colnames,
                        selected = colnames)
  })


  # Output the data
  output$data_table <- renderTable({
    # If missing input, return to avoid error later in function
    if(is.null(input$dataset))
      return()

    # Get the data set
    dat <- get(input$dataset)

    # Make sure columns are correct for data set (when data set changes, the
    # columns will initially be for the previous data set)
    if (is.null(input$columns) || !(input$columns %in% names(dat)))
      return()

    # Keep the selected columns
    dat <- dat[, input$columns, drop = FALSE]

    # Return
    dat
  })

 output$downloadData <- downloadHandler(
      filename = function() {
        ('test.csv')
      }, 
      content = function(con) {
        write.table(dat, row.names = FALSE, col.names=T, sep=",",con)
      },
      contentType="csv"
    )
})

ui.r

shinyUI(pageWithSidebar(

  headerPanel(""),

  sidebarPanel(
    uiOutput("choose_dataset"),

    uiOutput("choose_columns"),
downloadButton("downloadData", style = "color: white;background-color: #303030")
  ),


  mainPanel(
    tableOutput("data_table")
  )
))

我在 downloadHandler 中收到错误代码,表示它无法识别数据。我尝试过将元素包装在reactive({})中,但这也不起作用。

我已经尝试了几种方法来让表格显示页面长度,但我所做的一切都不起作用,所以我没有此处提供的任何代码。

r shiny
1个回答
0
投票

只需在外面进行数据整理即可。 试试这个

server <- shinyServer(function(input, output) {
  
  # Drop-down selection box for which data set
  output$choose_dataset <- renderUI({
    selectInput("dataset", "Data set", as.list(data_sets))
  })
  
  # Check boxes
  output$choose_columns <- renderUI({
    # If missing input, return to avoid error later in function
    if(is.null(input$dataset))
      return()
    
    # Get the data set with the appropriate name
    dat <- get(input$dataset)
    colnames <- names(dat)
    
    # Create the checkboxes and select them all by default
    checkboxGroupInput("columns", "Choose columns", 
                       choices  = colnames,
                       selected = colnames)
  })
  
  dat <- reactive({
    # If missing input, return to avoid error later in function
    if(is.null(input$dataset))
      return()
    
    # Get the data set
    dat <- get(input$dataset)
    
    # Make sure columns are correct for data set (when data set changes, the
    # columns will initially be for the previous data set)
    if (is.null(input$columns) || !(input$columns %in% names(dat)))
      return()
    
    # Keep the selected columns
    dat <- dat[, input$columns, drop = FALSE]
    
    # Return
    dat
  })
  
  # Output the data
  output$data_table <- renderTable({
    dat()
  })
  
  output$downloadData <- downloadHandler(
    filename = function() {
      ('test.csv')
    }, 
    content = function(con) {
      write.table(dat(), row.names = FALSE, col.names=T, sep=",",con)
    },
    contentType="csv"
  )
})
© www.soinside.com 2019 - 2024. All rights reserved.