使用参数从Python调用R脚本

时间:2019-03-27 13:16:08

标签: python r

我有一个带代码的r脚本:

args = commandArgs(trailingOnly=TRUE)
myData <- read.csv(file=args[0])

我想使用GUI来运行它,并使用此python代码提供一个选定的csv文件

from tkinter import filedialog
from tkinter import *
import subprocess

window = Tk()
window.geometry('500x200')
window.title("Wordcloud Creator")
lbl = Label(window, text="1. Please prepare a CSV (-Trennzeichen) file with the columns untgscod, berpos, SpezX3")
lbl.grid(column=0, row=0)
def runScript():
    filename = filedialog.askopenfilename(initialdir = "/",title = "Select file",filetypes = (("csv files","*.csv"),("all files","*.*")))
    subprocess.call(['Rscript', 'C:/Users/Name/Desktop/R-GUI/test.r', filename])
btn = Button(window, text="Select a file and start Cloud creation", command=runScript())
btn.grid(column=0, row=1)
window.mainloop()

但是不幸的是,这不起作用。我收到此错误,但不知道出了什么问题。

  File "c:\Users\name\.vscode\extensions\ms-python.python-2019.2.5558\pythonFiles\lib\python\ptvsd\_vendored\pydevd\_pydev_bundle\pydev_monkey.py", line 444, in new_CreateProcess
    return getattr(_subprocess, original_name)(app_name, patch_arg_str_win(cmd_line), *args)
FileNotFoundError: [WinError 2] The system cannot find the file specified

我看不到为什么找不到该文件。

2 个答案:

答案 0 :(得分:0)

根据评论中的建议,检查

  • 您的路径正确无误,不包含空格或奇怪的字符
  • 文件确实存在于正确的位置

...,如果没有帮助,则可以尝试使用v=0 o=- 7452034467634633423 2 IN IP4 127.0.0.1 s=- t=0 0 a=group:BUNDLE 0 1 a=msid-semantic: WMS 90 BOLNAOPqVHAtxWW2PZJyA5RG9IkH2MntC2EP m=audio 9 UDP/TLS/RTP/SAVPF 111 103 9 0 8 105 13 110 113 126 c=IN IP4 0.0.0.0 a=rtcp:9 IN IP4 0.0.0.0 a=ice-ufrag:KwHG a=ice-pwd:YhH0S06F5dJrCns9jFbscFMA a=ice-options:trickle renomination a=fingerprint:sha-256 B1:75:DB:D5:20:8C:86:F1:CC:54:4A:1C:C6:C9:AD:D3:79:C4:1E:45:57:CD:9B:FC:CC:1D:01:C5:C1:C5:BF:93 a=setup:active a=mid:0 a=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level a=sendrecv a=rtcp-mux a=rtpmap:111 opus/48000/2 a=rtcp-fb:111 transport-cc a=fmtp:111 minptime=10;useinbandfec=1 a=rtpmap:103 ISAC/16000 a=rtpmap:9 G722/8000 a=rtpmap:0 PCMU/8000 a=rtpmap:8 PCMA/8000 a=rtpmap:105 CN/16000 a=rtpmap:13 CN/8000 a=rtpmap:110 telephone-event/48000 a=rtpmap:113 telephone-event/16000 a=rtpmap:126 telephone-event/8000 a=ssrc:2787512374 cname:dF4uE3SebLpSsKGh a=ssrc:2787512374 msid:90 101 a=ssrc:2787512374 mslabel:90 a=ssrc:2787512374 label:101 a=ssrc:4286803808 cname:dF4uE3SebLpSsKGh a=ssrc:4286803808 msid:BOLNAOPqVHAtxWW2PZJyA5RG9IkH2MntC2EP cf5d6c28-239b-4c64-8051-33dbbb81edf4 a=ssrc:4286803808 mslabel:BOLNAOPqVHAtxWW2PZJyA5RG9IkH2MntC2EP a=ssrc:4286803808 label:cf5d6c28-239b-4c64-8051-33dbbb81edf4 m=video 9 UDP/TLS/RTP/SAVPF 96 97 98 99 108 109 124 c=IN IP4 0.0.0.0 a=rtcp:9 IN IP4 0.0.0.0 a=ice-ufrag:KwHG a=ice-pwd:YhH0S06F5dJrCns9jFbscFMA a=ice-options:trickle renomination a=fingerprint:sha-256 B1:75:DB:D5:20:8C:86:F1:CC:54:4A:1C:C6:C9:AD:D3:79:C4:1E:45:57:CD:9B:FC:CC:1D:01:C5:C1:C5:BF:93 a=setup:active a=mid:1 a=extmap:2 urn:ietf:params:rtp-hdrext:toffset a=extmap:3 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time a=extmap:4 urn:3gpp:video-orientation a=extmap:5 http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01 a=extmap:6 http://www.webrtc.org/experiments/rtp-hdrext/playout-delay a=extmap:7 http://www.webrtc.org/experiments/rtp-hdrext/video-content-type a=extmap:8 http://www.webrtc.org/experiments/rtp-hdrext/video-timing a=extmap:10 http://tools.ietf.org/html/draft-ietf-avtext-framemarking-07 a=sendrecv a=rtcp-mux a=rtcp-rsize a=rtpmap:96 VP8/90000 a=rtcp-fb:96 goog-remb a=rtcp-fb:96 transport-cc a=rtcp-fb:96 ccm fir a=rtcp-fb:96 nack a=rtcp-fb:96 nack pli a=rtpmap:97 rtx/90000 a=fmtp:97 apt=96 a=rtpmap:98 VP9/90000 a=rtcp-fb:98 goog-remb a=rtcp-fb:98 transport-cc a=rtcp-fb:98 ccm fir a=rtcp-fb:98 nack a=rtcp-fb:98 nack pli a=rtpmap:99 rtx/90000 a=fmtp:99 apt=98 a=rtpmap:108 red/90000 a=rtpmap:109 rtx/90000 a=fmtp:109 apt=108 a=rtpmap:124 ulpfec/90000 a=ssrc-group:FID 1166964128 3959308542 a=ssrc:1166964128 cname:dF4uE3SebLpSsKGh a=ssrc:1166964128 msid:90 100 a=ssrc:1166964128 mslabel:90 a=ssrc:1166964128 label:100 a=ssrc:3959308542 cname:dF4uE3SebLpSsKGh a=ssrc:3959308542 msid:90 100 a=ssrc:3959308542 mslabel:90 a=ssrc:3959308542 label:100 a=ssrc-group:FID 1617372799 1848430645 a=ssrc:1617372799 cname:dF4uE3SebLpSsKGh a=ssrc:1617372799 msid:BOLNAOPqVHAtxWW2PZJyA5RG9IkH2MntC2EP b4a261f3-065b-47bd-a759-207401cb9a6e a=ssrc:1617372799 mslabel:BOLNAOPqVHAtxWW2PZJyA5RG9IkH2MntC2EP a=ssrc:1617372799 label:b4a261f3-065b-47bd-a759-207401cb9a6e a=ssrc:1848430645 cname:dF4uE3SebLpSsKGh a=ssrc:1848430645 msid:BOLNAOPqVHAtxWW2PZJyA5RG9IkH2MntC2EP b4a261f3-065b-47bd-a759-207401cb9a6e a=ssrc:1848430645 mslabel:BOLNAOPqVHAtxWW2PZJyA5RG9IkH2MntC2EP a=ssrc:1848430645 label:b4a261f3-065b-47bd-a759-207401cb9a6e 而不是subprocess.run

答案 1 :(得分:0)

我对python一无所知,所以在那里我无济于事,但是您的Rscript正在调用参数的第零个元素,这只是一个空字符。

R从1开始索引。

如果我的脚本是:

args <- commandArgs(trailingOnly = TRUE)
print(args[0])

它将返回:

[1] character(0) # this is R telling you that the atomic is a character, but it has zero length

您的RScript应该是:

args <- commandArgs(trailingOnly = TRUE)
MyData <- read.csv(file = args[1])

此外,如果这是您的整个Rscript,则RScript关闭后,“ MyData”将消失。如果要在R中创建文件,则需要使用:

write.table(<whatever>)

带有适合您数据的参数。