[英]Integrating R script with Python - Error in loading packages
(很抱歉,有一点点格式不正确的问题)
我正在尝试为我的情感分析程序在Python中创建一个GUI,该GUI已用R编码。它看起来如下。
#!/usr/bin/python
from os import *
from subprocess import *
from tkinter import *
class Application(Frame):
#A GUI Application
def __init__(self,master):
#Initialise the frame
Frame.__init__(self,master)
self.grid()
self.create_widgets()
def create_widgets(self):
#Create the Sentiment Analysis GUI
#required to display whatever grid we have prepared?
self.label=Label(self,text="Welcome to Sentiment Analysis GUI!")
self.label.grid()
#Get Tweets
self.stream=Button(self,text="Stream the tweets",command=self.runprogram)
self.search=Button(self,text="Search the tweets",command=self.runr)
self.stream.grid()
self.search.grid()
#Manually classify the tweets
self.label_tweets=Button(self,text="Label the tweets")
self.label_tweets.grid()
#try and put select how many to label
#save these labels along with topic
#train button
self.train=Button(self,text="Train_the_system")
self.train.grid()
#"classify the rest" button
self.classify=Button(self,text="Classify the rest of tweets")
self.classify.grid()
#Demographic Analysis
self.demographic=Button(self,text="Do the Demographic Analysis")
self.demographic.grid()
def runprogram(self):
#system("python try.py")
chmod(r'E:\OneDrive\Interns\project\twitter',0o777)
system(r"Rscript E:\OneDrive\Interns\project\twitter\products_vape.R")
#this is not required
def runr(self):
command=Rscript
path2script='E:\OneDrive\Interns\project\twitter\products_vape.R'
cmd=[command,path2script]
#call(cmd,shell=True)
x=check_output(cmd,universal_newlines=True,shell=True)
#print('abc',x)
root=Tk()
root.title("Sentiment Analysis")
root.geometry("500x200")
app=Application(root)
root.mainloop()
在这段代码中,我为各种操作(如“训练系统”,“标记推文”)创建了按钮,这些按钮都是不言而喻的。
我尝试了两个版本的尝试在self.stream和self.search中运行我的rcode。 运行程序根本无法正常工作-抛出错误,“未定义名称Rscript”。 虽然runprogram有效,但无法加载任何库。 products_vape.R的rcode如下:
library(RMySQL)
library(DBI)
library(RJSONIO)
library(streamR)
library(stringr)
library(rjson)
load("C:\\Users\\Invincibles\\Documents\\cred.Rdata")
tweets<-filterStream( file.name="vape and ecigs.json",language="en",track=c("Vape","Vaping","e-cigarette","e-cig","ecigarette","ecig","e cig","e cigarette"), oauth=cred,verbose = TRUE)
vapes <- parseTweets("vapes and ecigs.json", simplify = FALSE)
tweets.filter <- tweets.df[ which(tweets.df$country_code=="IN"), ]
单击“流式推文”按钮时出现的错误是“库中存在错误(RMySQL):没有名为'RMySQL'的软件包”
仅出于记录目的,当我在命令窗口或RStudio中使用Rscript运行该代码时,它们可以完美运行。
我尝试过更改库以进行更改,但是存在类似的错误。
在单独但相关的注释上,是否可以通过将每个按钮与R文件集成在一起来做到这一点?
举例来说,在“人口统计分析”按钮中,我需要输出带有热图等的RMap的地图作为弹出窗口?
对于各种环境,从Python调用带有专用包的R脚本时,库路径可能无法在Python端完全识别。
要解决此问题,请尝试将.libPaths()放在R中的代码顶部以显式定义位置:
.libPaths("dir/to/package")
library(RMySQL)
library(DBI)
library(RJSONIO)
library(streamR)
library(stringr)
library(rjson)
...
或者,使用lib.loc
参数,默认情况下为NULL
并使用.libPaths()
设置的库树:
library(RMySQL, lib.loc="dir/to/package")
library(DBI, lib.loc="dir/to/package")
library(RJSONIO, lib.loc="dir/to/package")
library(streamR, lib.loc="dir/to/package")
library(stringr, lib.loc="dir/to/package")
library(rjson, lib.loc="dir/to/package")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.