Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

413 request error while using Shiny

I am developing a web application through shiny.

I am trying to upload my files using the fileinput() in shiny.

I have my code working. I intially tried to upload a file of 1.38KB and worked fine.

later when I tried to upload a file approximately 1.58MB, it throws me an error,

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

Could anyone help me, how i could avoid this error ?

Below is the code for Ui and Server I am using.

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

    )

  )
)

Server Code

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

  })

}

From web, I also found that I could increase the capacity , and have added a piece of code above my server function.

EDIT: Adding the console with error

when I upload my file, this is the error shown

like image 362
Mikz Avatar asked Jan 28 '23 08:01

Mikz


2 Answers

I am working with AWS environment and failed to mention in my post.

Here is the reason for my error. The parameter client_max_body_size was initially 1MB, that restricted to upload my files.

When I increased it to 50MB, i am able to upload larger files.

To add, this configuration will fail, if you will terminate your instance.

like image 135
Mikz Avatar answered Feb 06 '23 15:02

Mikz


change your maxrequestsize and put:

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

hope it helps

Edit:

The 30 means the mb so if you will upload more than 5 mb you need to use it but if your files will be less than 5, don't put it because shiny default upload size is 5mb.

Edit 2:

Try to use DT package (interactive table) using: install.packages("DT")

Then change your code on server and put library(DT) before 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)
        }
    })`
like image 30
J0ki Avatar answered Feb 06 '23 15:02

J0ki