r - 如何阅读 .xpt 文件?

标签 r shiny

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

下面的代码读取一个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)
}

最佳答案

您可以使用 haven 包中的 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)

关于r - 如何阅读 .xpt 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70459147/

相关文章:

R - 在循环中按属性缓冲 SpatialPolygons

r - 并行包在 R 3.3.1 (Ubuntu 14.04) 中不可用

r - 如何在 Shiny 的应用程序中仅显示五行数据

r - 计算基准年和相对百分比变化的指数

r - R 中的手动感知器示例 - 结果可以接受吗?

r - 在 R 中一次循环 10 列

javascript - Shiny 数据表中的单选按钮,用于一列中的 "subselection"行/分组

r - 如何删除数据表列中的填充

r - 使 ggvis 图具有 Shiny 的 react 性

javascript - 使用 html 标签和 bootstrap 时,为什么文本出现在我的 Shiny 应用程序中