闪亮的服务器对浏览器URL有反应,但没有从HTTR中获得
我正在尝试编写一个将接受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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
闪亮的应用程序确实设计为通过WebSocket与客户通信。对于处理http请求,您可能需要考虑一种其他方法,例如a
plumber
API。这是一个量身定制的例子:
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: