【问题标题】:How to read .xpt files?如何读取 .xpt 文件?
【发布时间】:2022-01-24 06:34:18
【问题描述】:

我正在开发一个读取 xpt 文件的 R 闪亮应用程序。

下面的代码读取一个csv文件并显示一个表格;但是,我正在寻找一种使用函数sasxport.get. 从 .xpt 文件中查看/显示相同内容的方法,有人可以帮助我如何在 R Shiny 中执行此操作吗?

app.R(目前它读取 csv)

## 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)
}

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您可以使用来自have包的read_xpt。试试这个

    library(haven)
    
    ui <- fluidPage(
      sidebarLayout(
        sidebarPanel(
          fileInput("file1", "Choose CSV File",
                    accept = c(
                      "text/csv",
                      "text/comma-separated-values,text/plain",
                      ".csv", ".xpt")
          ),
          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)
      # })
      
      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_xpt(inFile$datapath) 
      })
      
    }
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 2019-12-30
      • 2017-05-30
      • 2012-12-25
      相关资源
      最近更新 更多