运行闪亮的应用程序时出错&无法打开连接 [英] error running shiny app "can not open connection"
本文介绍了运行闪亮的应用程序时出错&无法打开连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从我的计算机上的.RData文件加载数据,并且我正在尝试为此运行一个闪亮的应用程序。我的代码如下,但当我运行它时,我得到错误"Can‘t Open Connection"..为什么会出现此错误?
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")
推荐答案
正如维什什所说,我认为你可能需要使用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)
如果您在fileInput
中选择CSV或RDS文件,则它将自动呈现表格。如果它以.rda
或.rdata
(不区分大小写)结尾,那么它将创建一个选择器来选择RDA文件中的哪个对象(因为它们实际上存储具有命名对象的环境,而不是单个对象)。
演示:使用CSV或RDS:
使用RDA文件(其中只有一个对象,mtcars
:
代码中的其他一些更改:
- 而不是使用
if (is.null(...))
,我使用的是更shiny
-esquereq(...)
方法;当事情不像您(开发人员)希望的那样进行时,它会更方便一些;
我特意做了一些可能不会被隔离的事情,但我想要一个清晰的反应路径;如果A依赖于B,C依赖于A和B,那么当A更新时,C也会更新,然后B也会更新,导致C再次更新...也许令人眼花缭乱,但这是多条依赖关系路径的结果。
- 因为它接受两种类型的存储(一个对象和多个),所以我需要两个步骤来检索数据:
rawdat()
可能是环境(RDA)或实际对象;dat()
将始终是对象或NULL
(如果未选择RDA和对象名称)。 - 我不需要
output$rda_objname
中的else return(NULL)
,在这个示例中,我只是为了清晰和明确的代码而使用它;我很可能不会在我的产品代码中使用它。 - 我在这里也经常使用
return
;从技术上讲,这些用途中的任何一个都不是必需的,同样,我只是为了这个例子而明确说明。
这篇关于运行闪亮的应用程序时出错&无法打开连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文