我正在开发一个读取 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/