【问题标题】:How to get dataset formatted properly in Shiny app?如何在 Shiny 应用程序中正确格式化数据集?
【发布时间】:2021-09-19 10:21:32
【问题描述】:

到目前为止,我已经能够得到它,以便用户可以将数据上传到闪亮的应用程序(动态上传)。我怎样才能得到它,以便上传的数据框格式正确(见下面的链接)。我相信我想使用的库是 DT,但我对其他选择持开放态度。代码如下

## Only run examples in interactive R sessions
if (interactive()) {

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      fileInput("file1", "Choose CSV File",
        accept = c(
          "text/csv",
          "text/comma-separated-values,text/plain",
          ".csv")
        ),
      tags$hr(),
      checkboxInput("header", "Header", TRUE)
    ),
    mainPanel(
      tableOutput("contents")
    )
  )
)

server <- function(input, output) {
  output$contents <- renderTable({
    # input$file1 will be NULL initially. After the user selects
    # and uploads a file, it will be a data frame with 'name',
    # 'size', 'type', and 'datapath' columns. The 'datapath'
    # column will contain the local filenames where the data can
    # be found.
    inFile <- input$file1

    if (is.null(inFile))
      return(NULL)

    read.csv(inFile$datapath, header = input$header)
  })
}

shinyApp(ui, server)
}

This is what I want the dataset that is uploaded to appear like for the user

【问题讨论】:

    标签: r shiny shinydashboard shinyapps


    【解决方案1】:

    您可以使用renderDataTabledataTableOutput

    library(shiny)
    
    ui <- fluidPage(
      sidebarLayout(
        sidebarPanel(
          fileInput("file1", "Choose CSV File",
                    accept = c(
                      "text/csv",
                      "text/comma-separated-values,text/plain",
                      ".csv")
          ),
          tags$hr(),
          checkboxInput("header", "Header", TRUE)
        ),
        mainPanel(
          dataTableOutput("contents")
        )
      )
    )
    
    server <- function(input, output) {
      output$contents <- renderDataTable({
        # input$file1 will be NULL initially. After the user selects
        # and uploads a file, it will be a data frame with 'name',
        # 'size', 'type', and 'datapath' columns. The 'datapath'
        # column will contain the local filenames where the data can
        # be found.
        inFile <- input$file1
        
        if (is.null(inFile))
          return(NULL)
        
        read.csv(inFile$datapath, header = input$header)
      }, options = list(pageLength = 5))
    }
    
    shinyApp(ui, server)
    

    你也可以试试DT 包,它有同名的函数。与DT::renderDataTableDT::dataTableOutput 一起使用。

    【讨论】:

    • 现在很难看到某些数据集,因为表太大了,有没有办法限制表的大小?谢谢!
    • 您可以将pageLength = 5 设置为在一个页面中仅显示5 行。将 5 更改为您想要的任何值。 @茉莉花
    猜你喜欢
    • 2021-09-10
    • 2021-04-14
    • 2019-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-10
    • 2011-06-21
    相关资源
    最近更新 更多