1

I have developed a C# Windows app that has an embedded R Shiny app through an iframe. This R Shiny app has been deployed on shinyapps.io and works fine. The R Shiny app needs to analyze data from the C# Windows app, and this can be received through a POST request using WebView2, which works like wonder.

The problem

The problem arises when the size of the sent file exceeds 50MB (52428800 bytes in binary). While the size of the sent data (measured on the C# Windows app side) stays below 50Mb, it works perfectly. But after that size is exceeded, altought the iframe containing the R Shiny app starts and loads the interface, within 10-15 seconds it throws an error saying the "Maximum upload size [has been] exceeded":

Upload size exceeded - Error as shown in the UI

Here is the code that sends it from the Windows app:

await DataView.EnsureCoreWebView2Async();
var postData = Encoding.UTF8.GetBytes(jsonData);
var postDataStream = new MemoryStream(postData.Length);

postDataStream.Write(postData, 0, postData.Length);
postDataStream.Seek(0, SeekOrigin.Begin);
var request = DataView.CoreWebView2.Environment.CreateWebResourceRequest(DataView.Source.AbsoluteUri,
                            "POST", postDataStream, "Content-Type: application/json");
DataView.CoreWebView2.NavigateWithWebResourceRequest(request);

And here the code that receives it on the R Shiny side. This code is placed inside the UI part of the Shiny app:

app_ui <- function(request) {
if (identical(request$REQUEST_METHOD, "POST")) {
    # Log the reception
    log_safe_info("POST request received")
    
    query_params <- parseQueryString(request$QUERY_STRING)

    # Read from 'request'
    body_bytes <- request$rook.input$read(-1)
    result <- jsonlite::fromJSON(rawToChar(body_bytes))
    
    log_safe_info(paste0("Size of the received object: ", object.size(result[[2]])))
  }
}

There are some notes to mention here:

  1. The logs register that this part of the code is reached.
  2. The code inside the R Shiny app reads the data from the result object, which is an array of 2 positions. The data I need is on the second position, while the first position only contains a timestamp. There is no other data contained inside the object.
  3. There are no other errors, e.g., related to reading the JSON file, etc.

What have I tried and didn't work

Increasing the R Shiny option maxRequestSize (as suggested by Joe Cheng):

options(shiny.maxRequestSize=100*(1024*1024))

Since I am using golem, this line is added inside the app.R file, before the run_app() function, where all the options are set. I have read over and over that this command should be the solution, but it simply doesn't work. Perhaps I am doing it wrong or something.

Questions I have

  1. Is the command wrong? Maybe the placement of the command is wrong? Should it be inside the UI part of the app? Or maybe the server?
  2. Is there any other place within the Shinyapps dashboard where this limit can be changed? Can it be subject to the subscription I have on ShinyApps (Starter Plan)?
  3. Is there a way to send multiple POST requests through WebView2? My [despair-fueled] idea is to split the data in chunks <50Mb each and send them sequentially. Maybe by doing that the upload size is not hit.
ismirsehregal
  • 30,045
  • 5
  • 31
  • 78
  • For future readers: please see my answer [here](https://stackoverflow.com/a/71064046/9841389) for a reproducible example and some further information on the shiny UI function pattern used above. – ismirsehregal Oct 14 '22 at 12:11
  • Shiny's `maxRequestSize` by default is 5MB not 50MB, so I guess this is another limitation, maybe of the underlying [rook](https://github.com/jeffreyhorner/Rook) application. – ismirsehregal Oct 14 '22 at 12:41
  • Are you sure about the 50MB? You can print and check the option while your shiny app is running via `print(getOption('shiny.maxRequestSize'))`. I guess [this](https://github.com/rstudio/shiny/blob/main/R/middleware.R#L325) is being triggered. – ismirsehregal Oct 14 '22 at 13:18
  • I am quite sure about the 50Mb. When I send package of 52,428,790 bytes (10 bytes less than the max.) it goes through, but when it's 52,428,810 bytes (10 bytes above), it doesn't. Both sizes are measured on the C# app's side. I have set the max. in Shiny using `options(shiny.maxRequestSize=1000*(1024*1024))` and when running `print(getOption('shiny.maxRequestSize'))` the value is 1048,576,000. I have tried setting it to 1000Mb, to 500Mb, 100Mb, even 100Gb, and it just gets me to the same error message. – Raul Coroban Oct 17 '22 at 08:40
  • You might want to file an issue [here](https://github.com/rstudio/shiny/issues), link to Joe Cheng's gist [here](https://gist.github.com/jcheng5/2aaff19e67079840350d08361fe7fb20) and ping @jcheng5, as the used pattern is quite exotic. – ismirsehregal Oct 17 '22 at 09:33

0 Answers0