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

时间:2019-05-10 16:20:38

标签: r shiny shiny-reactivity shinyapps

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

具体来说,当我使用“运行应用程序”按钮时,在应用程序中出现以下错误:“错误:无法打开连接。”此外,我在控制台中收到此错误:“错误:无法打开连接”,而控制台读取:“警告gzfile(file,“ 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)

1 个答案:

答案 0 :(得分:1)

文件路径是相对于Shiny App的,而不是相对于您的工作目录的,因此,当您使用runApp()并调用readRDS('DATA//grm_mod.rds')时,它需要一个目录DATA,该目录是该目录中的子目录存储包含您的应用程序的.R文件。如果将DATAapp.r移到同一目录,则应该可以。