闪亮的应用程序在控制台中提交代码时运行,但在选择 运行 应用程序按钮时不运行

Shiny app runs when code submitted in console but not when Run App button is selected

我正在构建一个闪亮的应用程序,我注意到当我提交代码到控制台时,所有内容都正确加载并按预期运行;但是,当我使用 运行 应用程序按钮呈现应用程序时,出现错误。

具体来说,当我使用 运行 应用程序按钮时,我在应用程序中收到以下错误:'Error: cannot open the connection.' 此外,我在控制台中收到此错误:'Error: cannot open the connection,' 而控制台显示:'gzfile(file, "rb") 中的警告:无法打开压缩文件 'DATA//grm_mod.rds',可能的原因 'No such file or directory''

应用程序很简单:用户上传数据文件,同时在后端加载 R 模型对象,根据模型估计分数,结果显示在 table 中,用户可以下载。

此错误的可能原因是什么?请注意,错误的可能来源在服务器逻辑中的代码注释 "Conversion steps" 下。

谢谢。

# 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 移动到与 app.r 相同的目录,它应该可以工作。