微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 Shiny 应用程序中正确格式化数据集?

如何解决如何在 Shiny 应用程序中正确格式化数据集?

到目前为止,我已经能够获得它,以便用户可以将数据上传到闪亮的应用程序(动态上传)。我怎样才能得到它以便上传的数据框格式很好(见下面的链接)。我相信我想使用的库是 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

解决方法

您可以使用 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 一起使用。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。