使用 Shiny 时出现 413 请求错误

Mik*_*ikz 3 r shiny shinydashboard

我正在通过闪亮开发 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>
Run Code Online (Sandbox Code Playgroud)

谁能帮我,我怎么能避免这个错误?

下面是我正在使用的 UI 和服务器的代码。

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

    )

  )
)
Run Code Online (Sandbox Code Playgroud)

服务器代码

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

  })

}
Run Code Online (Sandbox Code Playgroud)

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

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

当我上传我的文件时,这是显示的错误

Mik*_*ikz 5

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

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

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

另外,如果您终止实例,此配置将失败。

  • 祝福你的心。对于那些将来发现这一点的人:`client_max_body_size` 是 NGINX 使用的参数。因此,如果您使用的是 NGINX 代理,您可能需要更新 `client_max_body_size` 以与 `options(shiny.maxRequestSize=30*1024^2)`​​ 保持一致 (3认同)

J0k*_*0ki 3

更改您的 maxrequestsize 并输入:

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

希望能帮助到你

编辑:

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

编辑2:

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

然后更改服务器上的代码并将库(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)
        }
    })`
Run Code Online (Sandbox Code Playgroud)