我正在尝试从计算机上的.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")
答案 0 :(得分:3)
正如Vishesh所说,我认为您可能需要使用readRDS
而不是load
,但这是一个shiny
应用程序,它允许所有三个:csv,rds或rda。 / p>
首先,进行快速调试设置,以便我们测试三种类型的文件:
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)
如果在fileInput
中选择CSV或RDS文件,则它将自动呈现表格。如果它以.rda
或.rdata
结尾(不区分大小写),则它将创建一个选择器来选择rda文件中的哪个对象(因为它们实际上存储具有命名对象而不是单个对象的环境)
演示:带有CSV或RDS:
使用RDA文件(其中只有一个对象,mtcars
:
您的代码有一些其他更改:
if (is.null(...))
风格的shiny
方法,而不是使用req(...)
;当事情没有按照您(开发人员)的预期进行时,它更加人性化; isolate
提出了一些可能无法隔离的东西,但我希望有一个明确的反应路径;如果A依赖于B而C同时依赖于A和B,则当A更新时,C将更新,然后B将更新,从而导致C重新更新……可能使人头晕目眩,但这可能是多个依赖路径的结果。rawdat()
可能是环境(RDA)或实际的objecvt; dat()
始终是对象或NULL
(如果未选择RDA和对象名称)。else return(NULL)
中的output$rda_objname
,在此示例中我只是为了清楚和显式代码而已;我可能不会在我的生产代码中使用它。return
;从技术上讲,在所有这些用途中都不需要它,我只是在此示例中是明确的。答案 1 :(得分:1)
我建议使用readRDS
来读取RData文件。另外,您需要指定fileInput
UI元素,用户可使用该UI元素浏览到数据文件。
library(shiny)
ui <- fluidPage(
fileInput("file", label = "Rdata"),
tableOutput("table")
)
server <- function(input, output, session) {
dataset <- reactive({
req(input$file)
inFile <- input$file
readRDS(inFile$datapath)
})
output$table <- renderTable({
if (is.null(dataset()))
return(NULL)
head(dataset(), 10)
})
}
shinyApp(ui, server)
您在评论中提到的链接说明了req
的用法,当应用加载且用户尚未选择数据源时,它可以防止您的应用出错。