【问题标题】:413 request error while using Shiny使用 Shiny 时出现 413 请求错误
【发布时间】:2018-04-05 07:48:24
【问题描述】:

我正在通过 Shiny 开发一个 Web 应用程序。

我正在尝试使用闪亮的 fileinput() 上传我的文件。

我的代码正在运行。我最初尝试上传一个 1.38KB 的文件并且工作正常。

后来当我尝试上传大约 1.58MB 的文件时,它会抛出一个错误,

>   >      <html>  <head><title>413 Request Entity Too Large</title></head>  <body bgcolor="white">  <center><h1>413 Request
> Entity Too Large</h1></center>  <hr><center>nginx/1.12.1</center> 
> </body>

谁能帮助我,我怎样才能避免这个错误?

下面是我正在使用的 Ui 和 Server 的代码。

ui <- fluidPage(

  # App title ----
  titlePanel("Uploading Files"),

  # Sidebar layout with input and output definitions ----
  sidebarLayout(

    # Sidebar panel for inputs ----
    sidebarPanel(

      # Input: Select a file ----
      fileInput("file1", "Choose CSV File",
                multiple = TRUE,
                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")

    )

  )
)

服务器代码

shiny.maxRequestSize=30*1024^2
server <- function(input, output) {

  output$contents <- renderTable({

    # 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)

    df <- read.csv(input$file1$datapath,
                   header = input$header,
                   sep = input$sep,
                   quote = input$quote)

    if(input$disp == "head") {
      return(head(df))
    }
    else {
      return(df)
    }

  })

}

从网上,我也发现我可以增加容量,并在我的服务器功能上方添加了一段代码。

编辑:添加有错误的控制台

【问题讨论】:

    标签: r shiny shinydashboard


    【解决方案1】:

    我正在使用 AWS 环境,但没有在我的帖子中提及。

    这是我的错误的原因。参数client_max_body_size 最初是 1MB,限制上传我的文件。

    当我将它增加到 50MB 时,我可以上传更大的文件。

    添加,如果您要终止您的实例,此配置将失败。

    【讨论】:

    • 祝福你的心。对于以后发现这个的人:client_max_body_size 是 NGINX 使用的参数。因此,如果您使用的是 NGINX 代理,您可能需要更新 client_max_body_size 以与 options(shiny.maxRequestSize=30*1024^2) 保持一致
    • @Carl 我在哪里可以更改此参数?我正在部署到 shinyappsio,所以不要认为可以直接访问 nginx 配置
    • 我遇到了同样的问题。如果有人能告诉我在哪里可以找到这个参数,我会很感激的。我可以上传 413 Request Entity Too Large
      ”(我正在使用开源闪亮服务器)
    【解决方案2】:

    更改您的 maxrequestsize 并放置:

    options(shiny.maxRequestSize=30*1024^2)

    希望对你有帮助

    编辑:

    30 表示 mb,因此如果您要上传超过 5 mb,则需要使用它,但如果您的文件小于 5,请不要输入,因为闪亮的默认上传大小为 5mb。

    编辑 2:

    尝试使用 DT 包(交互式表格): install.packages("DT")

    然后在服务器上更改您的代码并将 library(DT) 放在 shinyServer 之前:

    `output$tb = DT::renderDataTable({
          req(input$file1)
    
          df <- read.csv(input$file1$datapath,
                   header = input$header,
                   sep = input$sep,
                   quote = input$quote)
            if(input$disp == "head") {
               return(head(df))
            }
            else {
               return(df)
            }
        })`
    

    【讨论】:

    • 删除你的 maxrequestsize 和 html : > 413 请求,然后重启 R 试试
    • 然后检查分隔符,我不知道更多,因为我用我的 70mb csv 文件尝试过,它可以工作
    • 增加限制是否解决了您的问题?我面临同样的问题,虽然这解决了应用程序在本地部署时的问题,但它在 shinyapp.io 上失败
    猜你喜欢
    • 2021-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-14
    • 2016-08-25
    • 2013-09-27
    • 2018-04-22
    • 2015-08-18
    相关资源
    最近更新 更多