【发布时间】:2019-09-28 14:07:20
【问题描述】:
我正在构建一个闪亮的应用程序,我注意到当我将代码提交到控制台时,一切都正确加载并按预期运行;但是,当我使用 Run App 按钮呈现应用程序时,我遇到了错误。
具体来说,当我使用“运行应用程序”按钮时,我在应用程序中收到以下错误:“错误:无法打开连接。”此外,我在控制台中收到此错误:'错误:无法打开连接',而控制台显示:'gzfile(文件,“rb”)中的警告:无法打开压缩文件'DATA//grm_mod.rds' , 可能的原因'没有这样的文件或目录''
该应用程序很简单:用户上传一个数据文件,而在后端加载一个 R 模型对象,从模型中估算分数,并将结果显示在用户可以下载的表格中。
此错误的可能原因是什么?请注意,错误的可能来源在服务器逻辑中的代码注释“转换步骤”下。
谢谢。
# load packages
if(!require("pacman"))install.packages("pacman")
p_load(dplyr, shiny, shinythemes, mirt)
# Define UI for data upload app ----
ui <- fluidPage(
# Set theme ----
theme = shinytheme("superhero"),
# App title ----
titlePanel("Raw Score to MAP Score Conversion"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Select a file ----
fileInput("file1", "Choose CSV File",
multiple = FALSE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")),
# Horizontal line ----
tags$hr(),
# Input: Checkbox if file has header ----
checkboxInput("header", "Header", TRUE),
# Input: Select separator ----
radioButtons("sep", "Separator",
choices = c(Comma = ",",
Semicolon = ";",
Tab = "\t"),
selected = ","),
# Input: Select quotes ----
radioButtons("quote", "Quote",
choices = c(None = "",
"Double Quote" = '"',
"Single Quote" = "'"),
selected = '"'),
# Horizontal line ----
tags$hr(),
# Input: Select number of rows to display ----
radioButtons("disp", "Display",
choices = c(Head = "head",
All = "all"),
selected = "head")
),
# Main panel for displaying outputs ----
mainPanel(
# Output: Data file ----
tableOutput("contents"),
# Download button
downloadButton('downloadData', 'Download')
)
)
)
# Define server logic to read selected file ----
server <- function(input, output) {
output$contents <- renderTable(striped = TRUE,
{
# input$file1 will be NULL initially. After the user selects
# and uploads a file, head of that data file by default,
# or all rows if selected, will be shown.
req(input$file1)
# when reading semicolon separated files,
# having a comma separator causes `read.csv` to error
tryCatch(
{
df <- read.csv(input$file1$datapath,
header = input$header,
sep = input$sep,
quote = input$quote)
# Conversion steps ----
# import the model object
mod <- readRDS('DATA//grm_mod.rds')
# generate scores
df <- data.frame(fscores(obj = mod, type = 'MAP', response.pattern = df))
# transform scores
x10_50 <- function(x) {
10 * x + 50
}
df <-
df %>%
mutate_at(vars(matches("^F.$")), .funs = list(T = ~x10_50(.)))
# add download handler
output$downloadData <- downloadHandler(
filename = function() { paste(input$file1, '.csv', sep='') },
content = function(file) {
write.csv(df, file, row.names = FALSE)
}
)
},
error = function(e) {
# return a safeError if a parsing error occurs
stop(safeError(e))
}
)
if(input$disp == "head") {
return(head(df))
}
else {
return(df)
}
# download
output$downloadData <- downloadHandler(
filename = function() {
paste('data-', Sys.Date(), '.csv', sep='')
},
content = function(file) {
write.csv(data, file)
}
)
})
}
# Create Shiny app ----
shinyApp(ui, server)
【问题讨论】:
-
文件路径是相对于 Shiny App 的,而不是你的工作目录,所以当你使用
runApp并调用readRDS('DATA//grm_mod.rds')时,它需要一个目录DATA,它是该目录的子目录存储了包含您的应用程序的.R文件。DATA//grm_mod.rds与包含您闪亮应用的文件的关系在哪里? -
@Wil:DATA 不是子目录。这回答了这个问题。我很乐意接受。
-
好的,我将其添加为答案,但它如何回答您的问题?这样我就可以为未来的读者提供彻底的答案。您是否只需要删除路径的 DATA// 部分?
-
@Wil:是的,我只是将 DATA 文件夹移动到与 app.r 相同的目录,并且按钮按预期工作。
标签: r shiny shiny-reactivity shinyapps