简体   繁体   English

将R脚本与Python集成-加载软件包时出错

[英]Integrating R script with Python - Error in loading packages

(Sorry about bits and pieces of poor formatting in question) (很抱歉,有一点点格式不正确的问题)

I am trying to create a GUI in Python for my Sentiment Analysis Program which has been coded in R. It looks like as follows. 我正在尝试为我的情感分析程序在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()

In this code, I have created buttons for various operations like "Train the System", "Label the tweets" - which is all self explanatory. 在这段代码中,我为各种操作(如“训练系统”,“标记推文”)创建了按钮,这些按钮都是不言而喻的。

I tried two versions of trying to run my rcode in self.stream and self.search. 我尝试了两个版本的尝试在self.stream和self.search中运行我的rcode。 The runr is not at all working - "name Rscript is not defined" is the error which gets thrown. 运行程序根本无法正常工作-抛出错误,“未定义名称Rscript”。 While runprogram works but is unable to load any libraries. 虽然runprogram有效,但无法加载任何库。 The rcode for products_vape.R is as follows: 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"), ]

The error on clicking the Stream the tweets button is "Error in library(RMySQL): there is no package called 'RMySQL'" 单击“流式推文”按钮时出现的错误是“库中存在错误(RMySQL):没有名为'RMySQL'的软件包”

Just for the record this code runs perfectly when I run with Rscript in Command Window or in RStudio. 仅出于记录目的,当我在命令窗口或RStudio中使用Rscript运行该代码时,它们可以完美运行。

I have tried changing library to require but there is a similar error. 我尝试过更改库以进行更改,但是存在类似的错误。

On a separate but related note, Will it be possible to do this by integrating each buttons with R files? 在单独但相关的注释上,是否可以通过将每个按钮与R文件集成在一起来做到这一点?

For instance in I would be requiring to output the Maps of RMaps with Heat Maps etc as a pop out in "Do the Demographic Analysis" Button? 举例来说,在“人口统计分析”按钮中,我需要输出带有热图等的RMap的地图作为弹出窗口?

For various environments, when calling R scripts with specialized packages from Python, the library paths may not be fully recognized on the Python end. 对于各种环境,从Python调用带有专用包的R脚本时,库路径可能无法在Python端完全识别。

To resolve, try placing .libPaths() at top of code in R to explicitly define locations: 要解决此问题,请尝试将.libPaths()放在R中的代码顶部以显式定义位置:

.libPaths("dir/to/package")

library(RMySQL)
library(DBI)
library(RJSONIO)
library(streamR)
library(stringr)
library(rjson)
...

Alternatively, use the lib.loc argument which by default is NULL and uses the library trees set in .libPaths() : 或者,使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM