我正在尝试从我的计算机上的 .RData 文件加载数据,我正在尝试为此运行一个闪亮的应用程序。我的代码在下面,但是当我运行它时出现错误“无法打开连接”..为什么会出现这个错误?
library(shiny)
ui <- fluidPage(
tableOutput("table")
)
server <- function(input, output, session) {
dataset <- reactive({
if (inFile == "")
return(NULL)
get(inFile$file1, load("E:/RProjects/Dashboard/gender1.RData"))
})
output$table <- renderTable({
if (is.null(dataset()))
return(NULL)
head(dataset(), 10)
})
}
shinyApp(ui, server)
样本数据:
structure(list(Gender = c("Male", "Male", "Male", "Male", "Male",
"Male", "Male", "Male", "Male", "Male"), Height = c(73.847017017515,
68.7819040458903, 74.1101053917849, 71.7309784033377, 69.8817958611153,
67.2530156878065, 68.7850812516616, 68.3485155115879, 67.018949662883,
63.4564939783664), Weight = c(241.893563180437, 162.3104725213,
212.7408555565, 220.042470303077, 206.349800623871, 152.212155757083,
183.927888604031, 167.971110489509, 175.92944039571, 156.399676387112
), BMI = c(0.0443566151469252, 0.0343082174614673, 0.0387343292394288,
0.0427654457094595, 0.0422547891767963, 0.033653156898047, 0.0388739862001733,
0.0359564180086832, 0.039169072415755, 0.0388404008602306), probability = c(5.77831234737499e-06,
0.605952546493327, 2.62595199514618e-05, 0.000362873417265588,
0.00461190097404834, 0.911068673692331, 0.0496119303175197, 0.352335117615303,
0.139124546478089, 0.343426515632885)), row.names = c(NA, 10L
), class = "data.frame")
正如 Vishesh 所说,我认为您可能需要使用readRDS
而不是load
,但这里有一个shiny
应用程序允许所有三个:csv、rds 或 rda。
首先,快速调试设置,以便我们测试三种类型的文件:
write.csv(mtcars, file="mt.csv")
saveRDS(mtcars, file="mt.rds")
save(mtcars, file="mt.rda")
(对于生产应用程序当然不需要。)
现在的应用程序:
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("file1", "Choose CSV, rda, or rds File"),
tags$hr(),
checkboxInput("header", "Header (if CSV)", TRUE),
uiOutput("rda_objname")
),
mainPanel(
tableOutput("contents")
)
)
)
server <- function(input, output) {
file1_ <- reactive({
req(input$file1)
# might also work with input$file1$type, which is something like
# 'application/vnd.ms-excel', though for me in testing this was
# blank for RDS/RDA ...
a <- input$file1
a$ext <- tolower(tools::file_ext(input$file1$name))
# ... though length==1 since we did not do multiple = TRUE
a$ext <- ifelse(a$ext == "rdata", "rda", a$ext)
a
})
rawdat <- reactive({
req(file1_())
inFile <- file1_()
# if we ever do fileInput(..., multiple = TRUE), this will need to
# be on a vector of length > 1
if ("csv" == inFile$ext) {
return( read.csv(inFile$datapath, header = input$header) )
} else if ("rds" == inFile$ext) {
return( readRDS(inFile$datapath) )
} else if (inFile$ext == "rda") {
e <- new.env(parent = emptyenv())
load(inFile$datapath, envir = e)
return( e )
} else return( NULL )
})
output$rda_objname <- renderUI({
# this only displays a select-input if the input file is valid and
# an Rdata-looking file, otherwise the select-input is absent
req(file1_())
inFile <- file1_()
if (inFile$ext == "rda") {
obj <- isolate(ls(envir = rawdat()))
selectInput("objname", "RDA object name",
choices = c("Select object name ...", obj))
} else return( NULL )
})
dat <- reactive({
req(rawdat())
inFile <- isolate(file1_())
if (inFile$ext == "rda") {
req(input$objname, input$objname %in% ls(envir = rawdat()))
return( get(input$objname, envir = rawdat()) )
} else return( rawdat() )
})
output$contents <- renderTable({
req(dat())
dat()
})
}
shinyApp(ui, server)
如果您在 中选择 CSV 或 RDS 文件fileInput
,则它会自动呈现表格。如果它以.rda
or结尾.rdata
(不区分大小写),那么它将创建一个选择器来选择 rda 文件中的哪个对象(因为它们确实存储了具有命名对象的环境,而不是单个对象)。
演示:使用 CSV 或 RDS:
使用 RDA 文件(其中有一个对象,mtcars
:
您的代码中的一些其他更改:
if (is.null(...))
我没有使用,而是使用了更shiny
风格化的req(...)
方法;当事情不像您(开发人员)预期的那样进行时,它会更加用户友好;isolate
一些可能不会被孤立的东西,但我想要一条清晰的反应路径;如果 A 依赖于 B 并且 C 依赖于 A 和 B,则有可能,当 A 更新时,C 将更新,然后 B 将更新,导致 C 再次更新......可能令人眼花缭乱,但这是多个依赖路径的结果.rawdat()
可能是环境 (RDA) 或实际对象;dat()
将始终是对象或NULL
(如果未选择 RDA 和对象名称)。else return(NULL)
in output$rda_objname
,我只是为了在这个例子中清晰和明确的代码而把它放在那里;我可能不会在我的生产代码中使用它。return
这里也用了很多;从技术上讲,在任何这些用途中都不需要它,同样我只是在这个例子中明确说明。本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句