如何将python脚本的输出传输到Rscript中?(复制)

时间:2022-06-08 14:03:36

This question already has an answer here:

这个问题已经有了答案:

I am writing a bash script that uses an argument from the command line input to pass into a python script, the result is using python's csv.writer module to produce a .csv file. I then have written an R script which accepts a .csv file on it's own, but I now want to pipe the csv file directly from my python script into my r script.

我正在编写一个bash脚本,该脚本使用来自命令行输入的参数传递到python脚本中,结果使用的是python的csv。生成.csv文件的writer模块。然后我编写了一个R脚本,它自己接受一个.csv文件,但是现在我想把csv文件直接从python脚本传输到R脚本中。

Here is my bash script:

下面是我的bash脚本:

#!/bin/bash

python protparams.py $1 | Rscript frequency.r

and my python script:

和我的python脚本:

from Bio import SeqIO
from Bio.SeqUtils import ProtParam
from Bio.SeqUtils import ProtParamData
import sys
import csv

handle = open(sys.argv[1])
with open('test.csv', 'w') as fp: 
    writer = csv.writer(fp, delimiter=',')
    for record in SeqIO.parse(handle, "fasta"): 
            seq = str(record.seq)
            X = ProtParam.ProteinAnalysis(seq)
            data = [seq,X.get_amino_acids_percent(),X.aromaticity(),X.gravy(),X.isoelectric_point(),X.secondary_structure_fraction(),X.molecular_weight(),X.instability_index()]
            writer.writerow(data)

which all works fine up to here my python script generates the csv file when called via my bash script. great! but when I pipe it into the following R script as in my bash file I get this error:

在这里,我的python脚本可以在我的bash脚本调用时生成csv文件。太棒了!但是当我把它插入到下面的R脚本中,就像在我的bash文件中一样,我得到了这个错误:

Error in file(file, "rt") : cannot open the connection
Calls: read.csv -> read.table -> file
In addition: Warning message:
In file(file, "rt") : cannot open file 'NA': No such file or directory
Execution halted

Here is my R script for reference:

以下是我的R脚本供参考:

args <- commandArgs(trailingOnly = TRUE)
dat <- read.csv(args[1], header=TRUE)
write.csv(dat, file = "out2.csv")

(at the moment my r script is simply testing to see if it can output the .csv file).

(目前,我的r脚本只是测试它是否可以输出.csv文件)。

This message normally occurs when the file doesn't exist, however in this case I think it is appearing because the argument in my r script is expecting a file passed as a command line argument - which just isn't getting picked up in the current way I have written my bash script. Am I wrong in thinking that piping the output of my python program is the same as using the output as a command line argument for my r script?

这个消息通常发生在该文件不存在,然而在这种情况下,我认为这是由于参数出现在我的r脚本期望一个文件作为命令行参数传递——不让拿起在当前我写了我的bash脚本。我是否错误地认为我的python程序的输出与使用输出作为我的r脚本的命令行参数相同?

Thanks very much.

非常感谢。

1 个解决方案

#1


2  

Yes, a little change in frequency.R (reading from stdin) allows to duplicate a .csv:

是的,频率有一点变化。R(从stdin中读取)允许复制.csv:

l@np350v5c:~$ cat foo.sh 
cat gbr_Country_en_csv_v2.csv | Rscript frequency.R

l@np350v5c:~$ cat frequency.R 
f <- file("stdin")
dat <- read.csv(f, header=TRUE)
write.csv(dat, file = "out2.csv")

#1


2  

Yes, a little change in frequency.R (reading from stdin) allows to duplicate a .csv:

是的,频率有一点变化。R(从stdin中读取)允许复制.csv:

l@np350v5c:~$ cat foo.sh 
cat gbr_Country_en_csv_v2.csv | Rscript frequency.R

l@np350v5c:~$ cat frequency.R 
f <- file("stdin")
dat <- read.csv(f, header=TRUE)
write.csv(dat, file = "out2.csv")