Running R script via shell script. syntax error near unexpected token `('












1














I am currently trying to run an R script via a shell script.



Here the R script:



test = rnorm(1:100, 1000)
write.csv(test, 'test.csv')


And here the bash script which calls the R one:



#!/bin/bash -l
#SBATCH --partition=compute
#SBATCH --job-name=test
#SBATCH --mail-type=ALL
#SBATCH --mail-user=myemail@blabla.com
#SBATCH --time=00:10:00
#SBATCH --nodes=1
#SBATCH --tasks-per-node=12
#SBATCH --account=myaccount
module purge
module load R
${HOME}/test.R


I think I did everything correctly, but the output returns the following error:



/mydirectory/test.R: line 3: syntax error near unexpected token `('
/mydirectory/test.R: line 3: `test = rnorm(1:100, 1000)'


Why I did I get this error?










share|improve this question
























  • What is the output of ls -l ${HOME}/test.R?
    – Hauke Laging
    Dec 2 '17 at 13:20










  • here's the output: -rwxr--r-- 1 username Dec 2 11:37 /mydirectory/test.R
    – aaaaa
    Dec 2 '17 at 13:32










  • p.s. if I run test.R without the shell script it works fine.
    – aaaaa
    Dec 2 '17 at 13:37






  • 1




    @aaaaa: Can you show the complete command, on how you tested the R script to be working?
    – Inian
    Dec 2 '17 at 13:39






  • 2




    @aaaaa: Remember, logging into R and running the commands, and trying it out in the shell script is not the same, R shell is different from the bash shell. Figure out a way to run the commands without logging into R from the command line directly and use that same approach in the shell script
    – Inian
    Dec 2 '17 at 13:56
















1














I am currently trying to run an R script via a shell script.



Here the R script:



test = rnorm(1:100, 1000)
write.csv(test, 'test.csv')


And here the bash script which calls the R one:



#!/bin/bash -l
#SBATCH --partition=compute
#SBATCH --job-name=test
#SBATCH --mail-type=ALL
#SBATCH --mail-user=myemail@blabla.com
#SBATCH --time=00:10:00
#SBATCH --nodes=1
#SBATCH --tasks-per-node=12
#SBATCH --account=myaccount
module purge
module load R
${HOME}/test.R


I think I did everything correctly, but the output returns the following error:



/mydirectory/test.R: line 3: syntax error near unexpected token `('
/mydirectory/test.R: line 3: `test = rnorm(1:100, 1000)'


Why I did I get this error?










share|improve this question
























  • What is the output of ls -l ${HOME}/test.R?
    – Hauke Laging
    Dec 2 '17 at 13:20










  • here's the output: -rwxr--r-- 1 username Dec 2 11:37 /mydirectory/test.R
    – aaaaa
    Dec 2 '17 at 13:32










  • p.s. if I run test.R without the shell script it works fine.
    – aaaaa
    Dec 2 '17 at 13:37






  • 1




    @aaaaa: Can you show the complete command, on how you tested the R script to be working?
    – Inian
    Dec 2 '17 at 13:39






  • 2




    @aaaaa: Remember, logging into R and running the commands, and trying it out in the shell script is not the same, R shell is different from the bash shell. Figure out a way to run the commands without logging into R from the command line directly and use that same approach in the shell script
    – Inian
    Dec 2 '17 at 13:56














1












1








1







I am currently trying to run an R script via a shell script.



Here the R script:



test = rnorm(1:100, 1000)
write.csv(test, 'test.csv')


And here the bash script which calls the R one:



#!/bin/bash -l
#SBATCH --partition=compute
#SBATCH --job-name=test
#SBATCH --mail-type=ALL
#SBATCH --mail-user=myemail@blabla.com
#SBATCH --time=00:10:00
#SBATCH --nodes=1
#SBATCH --tasks-per-node=12
#SBATCH --account=myaccount
module purge
module load R
${HOME}/test.R


I think I did everything correctly, but the output returns the following error:



/mydirectory/test.R: line 3: syntax error near unexpected token `('
/mydirectory/test.R: line 3: `test = rnorm(1:100, 1000)'


Why I did I get this error?










share|improve this question















I am currently trying to run an R script via a shell script.



Here the R script:



test = rnorm(1:100, 1000)
write.csv(test, 'test.csv')


And here the bash script which calls the R one:



#!/bin/bash -l
#SBATCH --partition=compute
#SBATCH --job-name=test
#SBATCH --mail-type=ALL
#SBATCH --mail-user=myemail@blabla.com
#SBATCH --time=00:10:00
#SBATCH --nodes=1
#SBATCH --tasks-per-node=12
#SBATCH --account=myaccount
module purge
module load R
${HOME}/test.R


I think I did everything correctly, but the output returns the following error:



/mydirectory/test.R: line 3: syntax error near unexpected token `('
/mydirectory/test.R: line 3: `test = rnorm(1:100, 1000)'


Why I did I get this error?







bash shell-script error-handling r syntax






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago









Rui F Ribeiro

39.2k1479130




39.2k1479130










asked Dec 2 '17 at 10:47









aaaaa

11016




11016












  • What is the output of ls -l ${HOME}/test.R?
    – Hauke Laging
    Dec 2 '17 at 13:20










  • here's the output: -rwxr--r-- 1 username Dec 2 11:37 /mydirectory/test.R
    – aaaaa
    Dec 2 '17 at 13:32










  • p.s. if I run test.R without the shell script it works fine.
    – aaaaa
    Dec 2 '17 at 13:37






  • 1




    @aaaaa: Can you show the complete command, on how you tested the R script to be working?
    – Inian
    Dec 2 '17 at 13:39






  • 2




    @aaaaa: Remember, logging into R and running the commands, and trying it out in the shell script is not the same, R shell is different from the bash shell. Figure out a way to run the commands without logging into R from the command line directly and use that same approach in the shell script
    – Inian
    Dec 2 '17 at 13:56


















  • What is the output of ls -l ${HOME}/test.R?
    – Hauke Laging
    Dec 2 '17 at 13:20










  • here's the output: -rwxr--r-- 1 username Dec 2 11:37 /mydirectory/test.R
    – aaaaa
    Dec 2 '17 at 13:32










  • p.s. if I run test.R without the shell script it works fine.
    – aaaaa
    Dec 2 '17 at 13:37






  • 1




    @aaaaa: Can you show the complete command, on how you tested the R script to be working?
    – Inian
    Dec 2 '17 at 13:39






  • 2




    @aaaaa: Remember, logging into R and running the commands, and trying it out in the shell script is not the same, R shell is different from the bash shell. Figure out a way to run the commands without logging into R from the command line directly and use that same approach in the shell script
    – Inian
    Dec 2 '17 at 13:56
















What is the output of ls -l ${HOME}/test.R?
– Hauke Laging
Dec 2 '17 at 13:20




What is the output of ls -l ${HOME}/test.R?
– Hauke Laging
Dec 2 '17 at 13:20












here's the output: -rwxr--r-- 1 username Dec 2 11:37 /mydirectory/test.R
– aaaaa
Dec 2 '17 at 13:32




here's the output: -rwxr--r-- 1 username Dec 2 11:37 /mydirectory/test.R
– aaaaa
Dec 2 '17 at 13:32












p.s. if I run test.R without the shell script it works fine.
– aaaaa
Dec 2 '17 at 13:37




p.s. if I run test.R without the shell script it works fine.
– aaaaa
Dec 2 '17 at 13:37




1




1




@aaaaa: Can you show the complete command, on how you tested the R script to be working?
– Inian
Dec 2 '17 at 13:39




@aaaaa: Can you show the complete command, on how you tested the R script to be working?
– Inian
Dec 2 '17 at 13:39




2




2




@aaaaa: Remember, logging into R and running the commands, and trying it out in the shell script is not the same, R shell is different from the bash shell. Figure out a way to run the commands without logging into R from the command line directly and use that same approach in the shell script
– Inian
Dec 2 '17 at 13:56




@aaaaa: Remember, logging into R and running the commands, and trying it out in the shell script is not the same, R shell is different from the bash shell. Figure out a way to run the commands without logging into R from the command line directly and use that same approach in the shell script
– Inian
Dec 2 '17 at 13:56










1 Answer
1






active

oldest

votes


















5














The problem is the shell is trying to the run your ${HOME}/test.R with a bash interpreter for which it is not trying to understand the syntax from the line number 3. Use the R interpreter explicitly from which you want the test.R to run from.



Set the interpreter for your Rscript in test.R as



#!/usr/bin/env Rscript

module purge
module load R
test = rnorm(1:100, 1000)
write.csv(test, 'test.csv')


This way with the interpreter set, you can now run it from the shell script as



Rscript ${HOME}/test.R




Remember, logging into R shell and running the commands on it, and trying it out in the shell script are not the same, R shell is different from the bash shell. You need to use the way to run the commands without logging into R from the command line directly and use that same approach in the shell script.






share|improve this answer























  • I'm quite sure the module entries are actually bash functions: modules.sourceforge.net
    – liori
    Dec 2 '17 at 14:53












  • After adding the shebang, you can just call the script as ${HOME}/test.R, you don't need to prepend it with the interpreter anymore
    – Ferrybig
    Dec 2 '17 at 20:11











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f408355%2frunning-r-script-via-shell-script-syntax-error-near-unexpected-token%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









5














The problem is the shell is trying to the run your ${HOME}/test.R with a bash interpreter for which it is not trying to understand the syntax from the line number 3. Use the R interpreter explicitly from which you want the test.R to run from.



Set the interpreter for your Rscript in test.R as



#!/usr/bin/env Rscript

module purge
module load R
test = rnorm(1:100, 1000)
write.csv(test, 'test.csv')


This way with the interpreter set, you can now run it from the shell script as



Rscript ${HOME}/test.R




Remember, logging into R shell and running the commands on it, and trying it out in the shell script are not the same, R shell is different from the bash shell. You need to use the way to run the commands without logging into R from the command line directly and use that same approach in the shell script.






share|improve this answer























  • I'm quite sure the module entries are actually bash functions: modules.sourceforge.net
    – liori
    Dec 2 '17 at 14:53












  • After adding the shebang, you can just call the script as ${HOME}/test.R, you don't need to prepend it with the interpreter anymore
    – Ferrybig
    Dec 2 '17 at 20:11
















5














The problem is the shell is trying to the run your ${HOME}/test.R with a bash interpreter for which it is not trying to understand the syntax from the line number 3. Use the R interpreter explicitly from which you want the test.R to run from.



Set the interpreter for your Rscript in test.R as



#!/usr/bin/env Rscript

module purge
module load R
test = rnorm(1:100, 1000)
write.csv(test, 'test.csv')


This way with the interpreter set, you can now run it from the shell script as



Rscript ${HOME}/test.R




Remember, logging into R shell and running the commands on it, and trying it out in the shell script are not the same, R shell is different from the bash shell. You need to use the way to run the commands without logging into R from the command line directly and use that same approach in the shell script.






share|improve this answer























  • I'm quite sure the module entries are actually bash functions: modules.sourceforge.net
    – liori
    Dec 2 '17 at 14:53












  • After adding the shebang, you can just call the script as ${HOME}/test.R, you don't need to prepend it with the interpreter anymore
    – Ferrybig
    Dec 2 '17 at 20:11














5












5








5






The problem is the shell is trying to the run your ${HOME}/test.R with a bash interpreter for which it is not trying to understand the syntax from the line number 3. Use the R interpreter explicitly from which you want the test.R to run from.



Set the interpreter for your Rscript in test.R as



#!/usr/bin/env Rscript

module purge
module load R
test = rnorm(1:100, 1000)
write.csv(test, 'test.csv')


This way with the interpreter set, you can now run it from the shell script as



Rscript ${HOME}/test.R




Remember, logging into R shell and running the commands on it, and trying it out in the shell script are not the same, R shell is different from the bash shell. You need to use the way to run the commands without logging into R from the command line directly and use that same approach in the shell script.






share|improve this answer














The problem is the shell is trying to the run your ${HOME}/test.R with a bash interpreter for which it is not trying to understand the syntax from the line number 3. Use the R interpreter explicitly from which you want the test.R to run from.



Set the interpreter for your Rscript in test.R as



#!/usr/bin/env Rscript

module purge
module load R
test = rnorm(1:100, 1000)
write.csv(test, 'test.csv')


This way with the interpreter set, you can now run it from the shell script as



Rscript ${HOME}/test.R




Remember, logging into R shell and running the commands on it, and trying it out in the shell script are not the same, R shell is different from the bash shell. You need to use the way to run the commands without logging into R from the command line directly and use that same approach in the shell script.







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 2 '17 at 14:27

























answered Dec 2 '17 at 10:55









Inian

3,860824




3,860824












  • I'm quite sure the module entries are actually bash functions: modules.sourceforge.net
    – liori
    Dec 2 '17 at 14:53












  • After adding the shebang, you can just call the script as ${HOME}/test.R, you don't need to prepend it with the interpreter anymore
    – Ferrybig
    Dec 2 '17 at 20:11


















  • I'm quite sure the module entries are actually bash functions: modules.sourceforge.net
    – liori
    Dec 2 '17 at 14:53












  • After adding the shebang, you can just call the script as ${HOME}/test.R, you don't need to prepend it with the interpreter anymore
    – Ferrybig
    Dec 2 '17 at 20:11
















I'm quite sure the module entries are actually bash functions: modules.sourceforge.net
– liori
Dec 2 '17 at 14:53






I'm quite sure the module entries are actually bash functions: modules.sourceforge.net
– liori
Dec 2 '17 at 14:53














After adding the shebang, you can just call the script as ${HOME}/test.R, you don't need to prepend it with the interpreter anymore
– Ferrybig
Dec 2 '17 at 20:11




After adding the shebang, you can just call the script as ${HOME}/test.R, you don't need to prepend it with the interpreter anymore
– Ferrybig
Dec 2 '17 at 20:11


















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f408355%2frunning-r-script-via-shell-script-syntax-error-near-unexpected-token%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

CARDNET

Boot-repair Failure: Unable to locate package grub-common:i386

濃尾地震