R代码中的命令行

2 投票
3 回答
2352 浏览
提问于 2025-04-16 01:31

让我先说一下,我是编程新手。

我希望能在R脚本中通过命令行运行一个Python脚本。我现在用的是Windows XP,但也有一台运行Windows 7的电脑。我可以在命令提示符下顺利运行以下代码,没有错误。

cd C:\Documents and Settings\USER\workspace\UGA - Website
python test1.py

我在R中尝试了各种方法,使用了?system,但希望有人能告诉我我哪里做错了。比如,这里有一个尝试(有人建议使用绝对路径)

cmd.1 <- shQuote("C:Python26/python.exe C:/Documents and Settings/USER/Desktop/UGA New Website", type="cmd")
system(cmd.1)

任何指导都会非常感谢。

3 个回答

1

C: 后面加一个 /,这样看起来就像这样:

cmd.1 <- shQuote("C:Python26/python.exe C:/Documents and Settings/USER/Desktop/UGA New Website", type="cmd")
system(cmd.1)
1

没有测试过,但可以试试这个:

cmd.1 <- shQuote('C:\\Python26\\python.exe "C:\\Documents and Settings\\USER\\Desktop\\UGA New Website"', type="cmd")
system(cmd.1)

如果这个不行,可以尝试不同的写法,比如 \\\/,还有你放引号的位置。

你也可以试试用系统的 cd 命令来切换目录,这样就不需要写绝对路径了。

2

谢谢大家的帮助。我的问题是由几个因素造成的,但这段代码解决了我的问题。

shell(paste("python", shQuote("C:\\Documents and Settings\\USER\\Desktop\\UGA New Website\\metrics_get.py")))

非常感谢

撰写回答