gpt4 book ai didi

python - Rpy2,将R返回(List Vector)转换为Python中的Json对象

转载 作者:太空宇宙 更新时间:2023-11-03 15:29:10 25 4
gpt4 key购买 nike

我有一个 Web 应用程序,其中的模块使用工具 rpy2,其目的是在 Python 代码中调用和执行 R 脚本(因为我想增加应用程序模块化)。

我的应用程序有一个客户端-服务器架构,它们之间的通信是通过 HTTP 请求进行的。我必须实现这一目标的工具是Flask

现在,我有一个 R 脚本,其任务是执行聚类过程并返回以 JSON 格式转换的数据帧,为此我在 R 中使用了 jsonlite。我的问题从这里开始。问题是Python代码中这个R脚本的返回是一个ListVector。

我不知道,但在这种格式下 python 会得到 R 脚本结果。所以我真的不知道如何从 python 将此结果(ListVector)转换为 JSON 对象,因为最后我需要在我想要公开的服务中返回 JSON 对象。我尝试理解 rpy2 文档,但我不清楚 rpy2 documentation .

R 脚本

require(kohonen)
require(jsonlite)

group = read.csv2("/home/python_user/Documents/FlaskApp/FlaskApp/resources/data2.csv", header = T, sep = ",", fill = F)
group$X <- NULL

data_train <- group[, -c(3:8)]

###
data_train <- data_train[data_train$NumeroAdultos != 12094,]

data_train$lat <- as.numeric(as.character(data_train$lat))
data_train$lon <- as.numeric(as.character(data_train$lon))
str(data_train)

#### Write JSON K-means #######

cl <- kmeans(data_train, 5)
cl$cluster
result <- data_train[,1:2]
result <- cbind(result, colorCluster = cl$cluster)
result$colorCluster[result$colorCluster == 1] <- "red"
result$colorCluster[result$colorCluster == 2] <- "blue"
result$colorCluster[result$colorCluster == 3] <- "green"
result$colorCluster[result$colorCluster == 4] <- "orange"
result$colorCluster[result$colorCluster == 5] <- "yellow"

toJSON(result, pretty = TRUE)

主程序.py

from flask import Flask, send_file, jsonify, send_from_directory, render_template, Blueprint, request
import json
import rpy2.robjects as robjects

@map_module.route("/serviceData")
def service_data():
r = robjects.r
result = r.source(map_module.root_path + "/R/clustering.R")
return result

控制台结果

File "/home/python_user/Documentos/flaskEnv/local/lib/python3.4/site-packages/flask/app.py", line 1982, in wsgi_app
response = self.full_dispatch_request()
File "/home/python_user/Documentos/flaskEnv/local/lib/python3.4/site-packages/flask/app.py", line 1615, in full_dispatch_request
return self.finalize_request(rv)
File "/home/python_user/Documentos/flaskEnv/local/lib/python3.4/site-packages/flask/app.py", line 1630, in finalize_request
response = self.make_response(rv)
File "/home/python_user/Documentos/flaskEnv/local/lib/python3.4/site-packages/flask/app.py", line 1740, in make_response
rv = self.response_class.force_type(rv, request.environ)
File "/home/python_user/Documentos/flaskEnv/local/lib/python3.4/site-packages/werkzeug/wrappers.py", line 885, in force_type
response = BaseResponse(*_run_wsgi_app(response, environ))
File "/home/python_user/Documentos/flaskEnv/local/lib/python3.4/site-packages/werkzeug/wrappers.py", line 57, in _run_wsgi_app
return _run_wsgi_app(*args)
File "/home/python_user/Documentos/flaskEnv/local/lib/python3.4/site-packages/werkzeug/test.py", line 884, in run_wsgi_app
app_rv = app(environ, start_response)
TypeError: 'ListVector' object is not callable
127.0.0.1 - - [24/Mar/2017 17:32:36] "GET /serviceData HTTP/1.1" 500 -

下面一行显示了存储 R 脚本返回结果的“结果”变量,调用 R 脚本时该变量的值是 ListVector,而不是 JSON 对象。

result = r.source(map_module.root_path + "/R/clustering.R")

感谢您阅读我的文章。

最佳答案

我一直在尝试解决这个问题,但我找到了一个对我来说不是最好的解决方案。所以解决方案是,从 R 脚本返回 JSON 对象,使用 python 获取并使用库 JSON 和服务 json.load 作为列表(python 对象)加载。完成此操作后,您将拥有一个 python 对象(作为列表,但具有 JSON 结构),因此最终将此列表转换为 python 中的 JSON 对象。请记住,问题是 R 脚本在 python 中返回一个 ListVector,并且该对象在 python 中不可调用。

最终结果

R-Sript(我纠正了一些东西,但本质或结构是一样的)

# K-means Clustering 
require(jsonlite)

# Read the data
group = read.csv2("/home/python_user/Documents/FlaskApp/FlaskApp/resources/data2.csv", header = T, sep = ",", fill = F)

# Prepare the data
group$X <- NULL
group <- group[, -c(3:8)]
group <- group[group$NumeroAdultos != 12094,]
group$lat <- as.numeric(as.character(group$lat))
group$lon <- as.numeric(as.character(group$lon))

# Excute the K-means algorithm
cl <- kmeans(group[,3:9], 5)

# Bind the data with its number cluster
group <- group[,1:2]
group <- cbind(group, colorCluster = cl$cluster)

# Change conventions
group$colorCluster[group$colorCluster == 1] <- "red"
group$colorCluster[group$colorCluster == 2] <- "blue"
group$colorCluster[group$colorCluster == 3] <- "green"
group$colorCluster[group$colorCluster == 4] <- "orange"
group$colorCluster[group$colorCluster == 5] <- "yellow"

# Convert the data frame to JSON object
toJSON(group, pretty=FALSE)

主程序.py

from flask import Flask, send_file, jsonify, send_from_directory, render_template, Blueprint, request
import json
import rpy2.robjects as robjects

@map_module.route("/getEntomoClustering", methods=['GET'])
def clustering():
r = robjects.r
dataR = r.source(map_module.root_path + "/R/clustering.R")[0][0]
clusters = json.loads(dataR)
return jsonify(clusters)

控制台结果

[
{
"colorCluster": "red",
"lat": 3.8946,
"lon": -76.2884
},
{
"colorCluster": "red",
"lat": 3.8988,
"lon": -76.2904
},
{
"colorCluster": "red",
"lat": 3.9008,
"lon": -76.2904
},
{
"colorCluster": "red",
"lat": 3.9034,
"lon": -76.2916
},
{
"colorCluster": "red",
"lat": 3.8934,
"lon": -76.2916
},
{
"colorCluster": "red",
"lat": 3.9067,
"lon": -76.2941
},
{
"colorCluster": "red",
"lat": 3.8972,
"lon": -76.2947
},
{
"colorCluster": "red",
"lat": 3.8948,
"lon": -76.2964
},... ]

要考虑到,R脚本的结果是一个ListVector,在其中它找到用R转换的数据Json,但在python中不是可调用对象。为此,我访问该数据 JSON,并且我知道它具有 JSON 结构,因此我使用服务 json.load 并获取列表(python 对象),最终使用服务 jsonify 再次转换为 JSON 对象(该对象可以是用Python对象处理,可调用)

dataR = r.source(map_module.root_path + "/R/clustering.R")[0][0] //JSON data from R is not callable
clusters = json.loads(dataR) // load a list, python object
return jsonify(clusters) // convert to JSON object callable

对此解决方案有任何疑问都可以问我。感谢您的阅读。

关于python - Rpy2,将R返回(List Vector)转换为Python中的Json对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43010705/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com