简体   繁体   中英

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.

#!/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. The runr is not at all working - "name Rscript is not defined" is the error which gets thrown. While runprogram works but is unable to load any libraries. The rcode for products_vape.R is as follows:

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'"

Just for the record this code runs perfectly when I run with Rscript in Command Window or in RStudio.

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?

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?

For various environments, when calling R scripts with specialized packages from Python, the library paths may not be fully recognized on the Python end.

To resolve, try placing .libPaths() at top of code in R to explicitly define locations:

.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() :

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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