setwd()导致终端语法错误

时间:2021-12-17 00:59:36

Why is it that the RScript works fine on RStudio but results in an error when running the crontab job on Terminal?

为什么RScript在RStudio上正常工作但在终端上运行crontab作业时会导致错误?

I'm trying to run a crontab job on an RScript to collect data every minute (* * * * *). The RScript works fine when run on RStudio however when running the crontab, I get the following error:

我正在尝试在RScript上运行crontab作业以每分钟收集数据(* * * * *)。在RStudio上运行时,RScript工作正常但是在运行crontab时,我收到以下错误:

/Users/xx/Capstone_2/Get_Data.R: line 3: syntax error near unexpected token `'/Users/xx/Capstone_2/Data''
/Users/xx/Capstone_2/Get_Data.R: line 3: `setwd('/Users/xx/Capstone_2/Data')'

Here is what I run on crontab -e:

这是我在crontab -e上运行的内容:

* * * * * /Users/xx/Capstone_2/Get_Data.R

* * * * * /Users/xx/Capstone_2/Get_Data.R

Here is the RScript:

这是RScript:

setwd('/Users/xx/Capstone_2/Data')
library(twitteR)

setup_twitter_oauth('consumer-key', 'consumer-secret', 
'access-token','access-secret')

iphonex <- searchTwitter("iphonex", n=1000)
iphonex <- twListToDF(iphonex)

write.csv(iphonex, '/Users/xx/Capstone_2/Data/iphonex.csv')

2 个解决方案

#1


0  

If you have shown us all of the /Users/xx/Capstone_2/Get_Data_Test.R file, then the problem is that the shell doesn't know you want to run it through R. You need to add

如果你向我们展示了所有/Users/xx/Capstone_2/Get_Data_Test.R文件,那么问题是shell不知道你想通过R运行它。你需要添加

#!/usr/bin/Rscript

as the first line.

作为第一线。

#2


0  

Step-1:

Check (in terminal) where Rscript is installed using:

使用以下命令检查(在终端中)安装Rscript的位置:

which Rscript

Step-2:

Adapt the shebang line in your R script, according to path of step-1 (e.g.):

根据步骤1的路径(例如)调整R脚本中的shebang行:

#!/usr/bin/Rscript

Step-3:

Let crontab handle the change to the directory where you have your R script.

让crontab处理对R脚本所在目录的更改。

* * * * * cd /Users/xx/Capstone_2/ && /usr/bin/Rscript Get_Data.R

#1


0  

If you have shown us all of the /Users/xx/Capstone_2/Get_Data_Test.R file, then the problem is that the shell doesn't know you want to run it through R. You need to add

如果你向我们展示了所有/Users/xx/Capstone_2/Get_Data_Test.R文件,那么问题是shell不知道你想通过R运行它。你需要添加

#!/usr/bin/Rscript

as the first line.

作为第一线。

#2


0  

Step-1:

Check (in terminal) where Rscript is installed using:

使用以下命令检查(在终端中)安装Rscript的位置:

which Rscript

Step-2:

Adapt the shebang line in your R script, according to path of step-1 (e.g.):

根据步骤1的路径(例如)调整R脚本中的shebang行:

#!/usr/bin/Rscript

Step-3:

Let crontab handle the change to the directory where you have your R script.

让crontab处理对R脚本所在目录的更改。

* * * * * cd /Users/xx/Capstone_2/ && /usr/bin/Rscript Get_Data.R