I'm building a shiny app and I'm noticing that when I submit the code to console everything loads correctly and runs as expected; however when I render the app with the Run App button I'm getting errors.
Specifically, when I use the Run App button I get the following error in the application:'Error: cannot open the connection.' Additionally, I'm getting this error in the console: 'Error: cannot open the connection,' while the console reads: 'Warning in gzfile(file, "rb") :cannot open compressed file 'DATA//grm_mod.rds', probable reason 'No such file or directory''
The application is straightforward: A user uploads a data file, while on the back end an R model object is loaded, scores are estimated from the model, and results display in a table that the user can download.
What is the likely cause of this error? Note, the likely source of the error is under the code comment "Conversion steps" in the server logic.
# 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)
The filepath is relative to the Shiny App, not your working directory, so when you use runApp()
and call readRDS('DATA//grm_mod.rds')
it expects a directory DATA
that is a subdirectory of the directory in which the .R file that contains your app is stored. If you move DATA
to the same directory as app.r
it should work.