首页 > 解决方案 > 来自 url、web、cloud 的自定义 R 闪亮文件输入

问题描述

我有一个闪亮的应用程序,它需要能够从 web / s3 / 预签名的下载 url 获取文件。我有一个自定义 javascript 小部件,我可以从公共 s3 路径或公共 s3 路径中获取预签名的 url。我正在构建这个问题/答案在 R 闪亮的应用程序中接受 HTTP 请求

我目前的理解是我需要获取这个预签名的 url 并将其发布到闪亮的服务器。

我曾尝试制作自己的输入绑定,但我的理解是 fileInput 与其他自定义输入有点不同,并且在 ui 和服务器中有自己的代码代码路径。

我想我需要在这里访问底层服务器端代码https://github.com/rstudio/shiny/blob/master/R/shiny.R#L1674及以下(handleRequest、saveFileUrl 等)但我不确定正确的方法来做到这一点。

我的最终目标是该文件将作为“输入”提供。

这是我当前的代码(基于链接的示例)我可以看到文件发布到服务器(我认为..)并获得 200 的回报——但我不确定下一步该做什么。

服务器.R

library(shiny)

shinyServer(function(input, output, session) {
  api_url <- session$registerDataObj( 
    name   = 'api', # an arbitrary but unique name for the data object
    data   = list(), # you can bind some data here, which is the data argument for the
                     # filter function below.
    filter = function(data, req) {
      print(ls(req))  # you can inspect what variables are encapsulated in this req
                      # environment
      if (req$REQUEST_METHOD == "GET") {
        # handle GET requests
        query <- parseQueryString(req$QUERY_STRING)
        # say:
        # name <- query$name
        # etc...
      } 

      if (req$REQUEST_METHOD == "POST") {
        # handle POST requests here

        reqInput <- req$rook.input

        # read a chuck of size 2^16 bytes, should suffice for our test
        buf <- reqInput$read(2^16)

        # simply dump the HTTP request (input) stream back to client
        shiny:::httpResponse(
          200, 'text/plain', buf
        )
      }          
    }
  )

  # because the API entry is UNIQUE, we need to send it to the client
  # we can create a custom pipeline to convey this message
  session$sendCustomMessage("api_url", list(url=api_url))

})


用户界面

library(shiny)

shinyUI(fluidPage(
  singleton(tags$head(HTML(
    '
    <script type="text/javascript">
      $(document).ready(function() {
        // creates a handler for our special message type
        Shiny.addCustomMessageHandler("api_url", function(message) {
          // set up the the submit URL of the form
          $("#form1").attr("action", "/" + message.url);
          $("#submitbtn").click(function() { $("#form1").submit(); });
        });
      })
    </script>
  '
  ))),
  tabsetPanel(
    tabPanel('POST request example',
             # create a raw HTML form
             HTML('
                  <form enctype="multipart/form-data" method="post" action="" id="form1">
                      <span>Name:</span>
                      <input name="file" type="file" /> <br />
                  </form>
            ')
    )
  )
))

标签: javascriptrshinyshiny-servershinyapps

解决方案


推荐阅读