闪亮的服务器对浏览器URL有反应,但没有从HTTR中获得

发布于 2025-01-24 14:22:58 字数 1529 浏览 6 评论 0原文

我正在尝试编写一个将接受URL,在一个文件中读取并根据URL中的参数写入另一个文件的Rshiny应用程序。将我的方法基于 r SHINY REST API通信这个答案。

以下是生成应用程序的最小功能代码 - 当我从浏览器等浏览器运行时(例如,刚输入地址 http://127.0.0.1/?outdest = test2 ),它将创建一个文件。但是,如果我尝试使用httr的get呼叫来调用它,则会生成错误

找不到函数“ data_read”

有意义的函数“ data_read”,因为该功能至少是我的理解。

因此,我认为我想做的是将UI读入并解析GET查询 - 然后在服务器端触发一个ReactiveEvent以获取该数据,但是我无法得到它来生成我想要的输出。

有没有办法来处理此操作?还是另一个包裹 - 我应该看水管工吗?

非常感谢 奥达

library(shiny)
library(rjson)
library(callr)
library(httr)


data_read <- function(x) {
    
    json = fromJSON(file = x)
    dat_df <- bind_rows(lapply(json,as.data.frame))
    
}


    
shiny_UI <- function(req) {
  # The `req` object is a Rook environment
  # See https://github.com/jeffreyhorner/Rook#the-environment
  if (identical(req$REQUEST_METHOD, "GET")) {   
    x = data_read("C:/Users/Z0049Y2S/Documents/test.json")
    query_params <- parseQueryString(req$QUERY_STRING)
    #print(query_params)
    if(length(query_params$outDest) ){
        output_Destination = query_params$outDest
        print(output_Destination)
        write.csv("Hello",paste0(output_Destination,".csv"))
    }
    fluidPage(
      h1("Accepting POST requests from Shiny")
    )
  } 
}

shiny_Server <- function(input, output, session) {
    
}

I'm trying to write an RShiny application that will accept a URL, read in one file and write to another file based on parameters in the URL. Basing my approach on R Shiny REST API communication this answer.

Below is a minimal functioning code to generate the app - and when I run it from a browser like Chrome (e.g. just entering the address http://127.0.0.1/?outDest=test2), it will create a file. But if I try to call it using a GET call from httr, it generates an error

could not find function "data_read"

Which makes sense, because that function is available to the server and not the ui, is my understanding at least.

So what I want to do, I think, is have the UI read in and parse the GET query - then trigger a reactiveEvent on the server side to take that data, but I can't get that to generate the output I want.

Is there a way for RShiny to handle this? Or another package - should I be looking at plumber?

Many thanks,
Aodhán

library(shiny)
library(rjson)
library(callr)
library(httr)


data_read <- function(x) {
    
    json = fromJSON(file = x)
    dat_df <- bind_rows(lapply(json,as.data.frame))
    
}


    
shiny_UI <- function(req) {
  # The `req` object is a Rook environment
  # See https://github.com/jeffreyhorner/Rook#the-environment
  if (identical(req$REQUEST_METHOD, "GET")) {   
    x = data_read("C:/Users/Z0049Y2S/Documents/test.json")
    query_params <- parseQueryString(req$QUERY_STRING)
    #print(query_params)
    if(length(query_params$outDest) ){
        output_Destination = query_params$outDest
        print(output_Destination)
        write.csv("Hello",paste0(output_Destination,".csv"))
    }
    fluidPage(
      h1("Accepting POST requests from Shiny")
    )
  } 
}

shiny_Server <- function(input, output, session) {
    
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你げ笑在眉眼 2025-01-31 14:22:58

闪亮的应用程序确实设计为通过WebSocket与客户通信。对于处理http请求,您可能需要考虑一种其他方法,例如a plumber API。

这是一个量身定制的例子:

p <- callr::r_bg(
  function() {
    library(plumber)

    pr() |>
      pr_handle("GET", "/", function(outFile) {
        write("Hello", outFile)
      }) |>
      pr_run(port = 9850)
  }
)

httr::GET("http://127.0.0.1:9850?outFile=hello.txt")
readLines("hello.txt")

p$kill()

Shiny apps are really designed to communicate with a client via websockets. For handling HTTP requests, you might want to consider a different approach, e.g. a plumber API.

Here's a tailored example of that:

p <- callr::r_bg(
  function() {
    library(plumber)

    pr() |>
      pr_handle("GET", "/", function(outFile) {
        write("Hello", outFile)
      }) |>
      pr_run(port = 9850)
  }
)

httr::GET("http://127.0.0.1:9850?outFile=hello.txt")
readLines("hello.txt")

p$kill()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文