Executing R files from inside Python using PypeR - python

My current work project is on writing a Python program that must at various points rely on R. Since I don't know R so well, and the person helping me doesn't know Python so well, the actual R code is not in my program. Instead, he opened Notepad, put the R code in there, and saved it as (name).r. When executed, the output is written into a txt file, which Python can then read.
All I have to do is ask Python to ask R to run (name).r
I've tried using subprocess.run. That worked for awhile, and then for some unknown reason stopped working and now does nothing. Then I tried using rpy2, which also worked for awhile; but now it looks like the installation is broken and I'm having trouble getting it reinstalled.
I'd like to give a 3rd option a try now: PypeR. I used pip install pyper. Looked like it was successful.
To keep things simple, I opened Notepad and typed in the following, and saved it as hello.r:
message <- 'goodbye'
write.table(message,'C:/Users/(my name)/Desktop/(folder)/goodbye.txt',row.names=FALSE,col.names=FALSE)
Manually opening R and copy-pasting the lines in one-at-a-time does indeed work. But I'm having trouble getting it to work from Python. Here are some things I've tried (I always put import pyper at the top):
pyper.runR("source('C:/Users/(muy name)/Desktop/(folder)/hello.r')")
This gives NameError: name 'dump_stdout' is not defined
pyper.R("source('C:/Users/(my name)/Desktop/(folder)/hello.r')")
This gives FileNotFoundError: [WinError 2] The system cannot find the file specified
r=pyper.R("C:/Program Files/R/R-3.4.1/bin/i386/Rgui.exe")
r("source('C:/Users/(my name)/Desktop/(folder)/hello.r')")
This causes RGui to open up with a blank R Console. And then nothing happens. When I click back to Python, the console shows Python is busy until I click the halt button, whereupon I get "OSError: [Errno 22] Invalid argument
What is the correct way to execute hello.r?
Thank you

Looks like I got it. This works:
r=pyper.R(RCMD="C:/Program Files/R/R-3.4.1/bin/R")
r.run("source('C:/Users/(my name)/Desktop/(folder)/hello.r')")

Related

Syntax error when trying to open a file through Python 3 Command Shell

So I am learning Python through Udemy tutorials and now I need to open a file through CMD(CMD is opened on folder I need) and when I am typing function for opening file it says syntax error, but I have made everything good what a guy on tutorials says, I really don't know what what should I do, I checked all of the forums and still cant find the answer.
Here are some screenshots:
Couple of issues:
1.Your text file is called "example.txt.txt" instead of "example.txt"
2.The "example.txt","r" should be surrounded with brackets () instead of <>. These symbols look similar in cmd and are easy to confuse.
#instead of
file = open<"example.txt","r">
#use
file = open("example.txt","r")
This should fix your problem; let me know if it does.
You have to use parenthesss () not <>
file = open("example.txt","r")
check https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files

ValueError: need more than 0 values to unpack (Python 2)

I am trying to replicate another researcher's findings by using the Python file that he added as a supplement to his paper. It is the first time I am diving into Python, so the error might be extremely simple to fix, yet after two days I haven't still. For context, in the Readme file there's the following instruction:
"To run the script, make sure Python2 is installed. Put all files into one folder designated as “cf_dir”.
In the script I get an error at the following lines:
if __name__ == '__main__':
cf_dir, cf_file, cf_phys_file = sys.argv[1:4]
os.chdir(cf_dir)
cf = pd.read_csv(cf_file)
cf_phys = pd.read_csv(cf_phys_file)
ValueError: need more than 0 values to unpack
The "cf_file" and "cf_phys_file" are two major components of all files that are in the one folder named "cf_dir". The "cf_phys_file" relates only to two survey question's (Q22 and Q23), and the "cf_file" includes all other questions 1-21. Now it seems that the code is meant to retrieve those two files from the directory? Only for the "cf_phys_file" the columns 1:4 are needed. The current working directory is already set at the right location.
The path where I located "cf_dir" is as follows:
C:\Users\Marc-Marijn Ossel\Documents\RSM\Thesis\Data\Suitable for ML\Data en Artikelen\Per task Suitability for Machine Learning score readme\cf_dir
Alternative option in readme file,
In the readme file there's this option, but also here I cannot understand how to direct the path to the right location:
"Run the following command in an open terminal (substituting for file names
below): python cfProcessor_AEAPnP.py cf_dir cf_file cf_phys_file task_file jobTaskRatingFile
jobDataFile OESfile
This should generate the data and plots as necessary."
When I run that in "Command Prompt", I get the following error, and I am not sure how to set the working directory correctly.
- python: can't open file 'cfProcessor_AEAPnP.py': [Errno 2] No such file or directory
Thanks for the reading, and I hope there's someone who could help me!
Best regards & stay safe out there during Corona!!
Marc
cf_dir, cf_file, cf_phys_file = sys.argv[1:4]
means, the python file expects few arguments when called.
In order to run
python cfProcessor_AEAPnP.py cf_dir cf_file cf_phys_file task_file jobTaskRatingFile jobDataFile OESfile
the command prompt should be in that folder.
So, open command prompt and type
cd path_to_the_folder_where_ur_python_file_is_located
Now, you would have reached the path of the python file.
Also, make sure you give full path in double quotes for the arguments.

Can't get working command line on prompt to work on subprocess

I need to extract text from a PDF. I tried the PyPDF2, but the textExtract method returned an encrypted text, even though the pdf is not encrypted acoording to the isEncrypted method.
So I moved on to trying accessing a program that does the job from the command prompt, so I could call it from python with the subprocess module. I found this program called textExtract, which did the job I wanted with the following command line on cmd:
"textextract.exe" "download.pdf" /to "download.txt"
However, when I tried running it with subprocess I couldn't get a 0 return code.
Here is the code I tried:
textextract = shlex.split(r'"textextract.exe" "download.pdf" /to "download.txt"')
subprocess.run(textextract)
I already tried it with shell=True, but it didn't work.
Can anyone help me?
I was able to get the following script to work from the command line after installing the PDF2Text Pilot application you're trying to use:
import shlex
import subprocess
args = shlex.split(r'"textextract.exe" "download.pdf" /to "download.txt"')
print('args:', args)
subprocess.run(args)
Sample screen output of running it from a command line session:
> C:\Python3\python run-textextract.py
args: ['textextract.exe', 'download.pdf', '/to', 'download.txt']
Progress:
Text from "download.pdf" has been successfully extracted...
Text extraction has been completed!
The above output was generated using Python 3.7.0.
I don't know if your use of spyder on anaconda affects things or not since I'm not familiar with it/them. If you continue to have problems with this, then, if it's possible, I suggest you see if you can get things working directly—i.e. running the the Python interpreter on the script manually from the command line similar to what's shown above. If that works, but using spyder doesn't, then you'll at least know the cause of the problem.
There's no need to build a string of quoted strings and then parse that back out to a list of strings. Just create a list and pass that:
command=["textextract.exe", "download.pdf", "/to", "download.txt"]
subprocess.run(command)
All that shlex.split is doing is creating a list by removing all of the quotes you had to add when creating the string in the first place. That's an extra step that provides no value over just creating the list yourself.

Creating a new .py file in python on a mac but getting [Errno 1] Operation not permitted

I have tried to write a python program that creates another python program and when I direct it to where I want it saved it says:
`File "/Users/Fredrik/Documents/infinitylock copy.py", line 11, in createFile
f = open(dest,'w')
IOError: [Errno 1] Operation not permitted: '/Users/Fredrik/Documents/python\\'`
My entire code is:
import time as t
from os import path
def createFile(dest):
date = t.localtime(t.time())
name = '%d_%d_%d.txt' % (date[1], date[2], (date[0] %100))
if not (path.isfile(dest + name)):
f = open(dest + name,'w')
f.write('\n'*30)
f.close()
if __name__ == '__main__':
destination = '/Users/Fredrik/Documents/python\\'
createFile(destination)
raw_input("done")
Please answer on how I can get this program to work properly. And resolve the error. Thank you.
Suggestions for 'get this program to work correctly':
What I would do is go through each part of the code, and look up on StackOverflow on how most people use each part. For example, I would use
import os
instead of
from os import path
simply because you might later want to use other functions. Its important to keep your code reusable, especially if it gets rid of errors later on.
Answer to 'resolve the error':
Try using forward slashes - I don't know why you have 'python\'
The program is likely throwing an error because you wrote that, an it is not valid and so therefore not permitted. Use .txt or some extension like that. Finally, try to run it with administrative privileges. http://www.wikihow.com/Open-Applications-With-Root-Privileges-on-a-Mac has a fairly good explaination. Simply open the console, root yourself, then run the program from there.

f.write() function is not working in python since this morning

I am using Python 2.7 and the function, f.write() is not working. A part of my code is given below.
Please suggest if any packages need to be installed.
for item in data1['OperationalTimes']['airportResources']:
with open("airportResources_details.txt",'a') as f: -- code works fine till here when i try to "print data1"
f.write(item['arrivalTerminal']+'\n') -- this line is not getting through
For a start, you can avoid the performance drain of opening and closing the file for every single item, by simply changing the order of things:
with open("airportResources_details.txt",'a') as f:
for item in data1['OperationalTimes']['airportResources']:
f.write(item['arrivalTerminal']+'\n')
Beyond that, you may want to check the usual suspects, like ensuring you're running in the correct directory. For example, use os.system("pwd") for getting the current working directory (on a UNIX-like platform).
Or temporarily changing the file specifier to something like /tmp/xyzzy.txt and seeing if that gets created in the right place.
Or temporarily changing it to use print rather than f.write to see if it comes out on standard output.

Categories