使用R's Plumber-创建GET端点以托管CSV格式的数据而不是JSON [英] Using R's Plumber - create GET endpoint to host CSV formatted data rather than JSON

查看:38
本文介绍了使用R's Plumber-创建GET端点以托管CSV格式的数据而不是JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这通常是R的水管工库的一个很好的快速演示,但主要是我正在努力以csv格式提供数据

我正在使用R的水管工包装来托管我的一些体育数据的API端点.目前,我有一些数据可以帮助我为美国职棒大联盟棒球队获得总冠军.使用水管工,我设置了以下两个脚本:

I am working with R's plumber package to host an API endpoint for some sports data of mine. Currently I have some data that grabs win totals for MLB baseball teams that I'm trying to serve. Using plumber, I have the following 2 scripts set up:

setupAPI.R :使用两个GET端点设置我的API:

setupAPI.R: sets up my API with two GET endpoints:

library(plumber)
library(jsonlite)

# load in some test sports data to host
mydata = structure(list(Team = structure(c(8L, 20L, 7L, 28L, 2L, 30L, 
23L, 1L, 6L, 19L), .Label = c("Angels", "Astros", "Athletics", 
"Blue Jays", "Braves", "Brewers", "Cardinals", "Cubs", "Diamondbacks", 
"Dodgers", "Giants", "Indians", "Mariners", "Marlins", "Mets", 
"Nationals", "Orioles", "Padres", "Phillies", "Pirates", "Rangers", 
"Rays", "Red Sox", "Reds", "Rockies", "Royals", "Tigers", "Twins", 
"White Sox", "Yankees"), class = "factor"), GamesPlayed = c(162L, 
162L, 162L, 162L, 162L, 162L, 162L, 162L, 162L, 162L), CurrentWins = c(92L, 
75L, 83L, 85L, 101L, 91L, 93L, 80L, 86L, 66L)), .Names = c("Team", 
"GamesPlayed", "CurrentWins"), row.names = c(NA, 10L), class = "data.frame")

# create a GET request for shareprices (in JSON format)
#* @get /shareprices_json
getSPs <- function(){ 
  return(toJSON(mydata))
}

# create a GET request for MLB shareprices (in CSV format)
#* @get /shareprices_csv
csvSPs <- function(){
  return(mydata)
}

# run both functions (i think needed for the endpoints to work)   
getSPs()
csvSPs()

RunAPI.R :铅锤的setupAPI.R,获取本地托管的端点

RunAPI.R: plumb's setupAPI.R, gets the endpoints hosted locally

library(plumber)
r <- plumb("setupAPI.R") 
r$run(port=8000)

..

在控制台中运行RunAPI.R代码后,当我转到端点时,我的 http://127.0.0.1:8000/shareprices_csv 端点显然返回了JSON对象,而我的 http://127.0.0.1:8000/shareprices_json 端点似乎奇怪地返回了长度为1的JSON,其中字符串中的JSON作为返回的JSON中的唯一元素.

After I've run the RunAPI.R code in my console, when I go to the endpoints, my http://127.0.0.1:8000/shareprices_csv endpoint is clearly returning a JSON object, and my http://127.0.0.1:8000/shareprices_json endpoint is seemingly oddly returning an JSON of length 1, with a JSON in a string as the sole element in the returned JSON.

简而言之,我现在看到我应该简单地返回数据帧,而不是返回JSON(数据帧),以使端点托管JSON格式的数据,但 我仍然不知道如何以CSV格式提供此数据.水管工有可能吗?setupAPI.R中的函数中的return语句应该是什么样的?任何帮助表示赞赏!

In short, I can see now that I should simply return the dataframe, and not toJSON(the dataframe), to have the endpoint host JSON formatted data, however I still do not know how to serve this data in CSV format. Is this possible in plumber? What should the return statement look like in the functions in setupAPI.R? Any help is appreciated!!

推荐答案

这里需要两个技巧:

  1. 您可以通过直接返回响应对象来绕过端点上的序列化.更多文档此处
  2. 您可以通过更改 res $ body 来指定响应的正文.
  1. You can bypass serialization on an endpoint by returning the response object directly. More docs here
  2. You can specify the body of the response by mutating res$body.

您可以将这两个想法结合起来创建一个端点,例如:

You can combine these two ideas to create an endpoint like:

#' @get /data.csv
function(res) {
  con <- textConnection("val","w")
  write.csv(iris, con)
  close(con)

  res$body <- paste(val, collapse="\n")
  res
}

请注意,水管工免费为您提供了一些不错的功能,例如为JSON响应设置适当的HTTP标头.如果您要自己发送响应,则所有这些都是您自己决定的,因此您需要确保设置适当的标头,以教您的API客户端如何解释该响应.

Note that plumber does some nice things for you for free like setting the appropriate HTTP headers for your JSON responses. If you're sending a response yourself, you're on your own for all that, so you'll need to make sure that you set the appropriate headers to teach your API clients how they should interpret this response.

这篇关于使用R's Plumber-创建GET端点以托管CSV格式的数据而不是JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆