Rscript prompt 'invalid mul tibyte character in parser at line 1' - r

When I run Rscript d:/s.R, it prompt:
invalid multibyte character in parser at line 1 Execution halted
the contens of s.R is:
paste("到达第","天",sep="")
and the encoding of s.R is UTF-8.
How to solve this issue?
Many thanks.

Related

Rendering many Rmds from command line using GNU parallel

To knit an Rmd from the command line, you can do the following and it creates an HTML
Rscript -e "rmarkdown::knit('test.Rmd')"
I want to do this for many Rmds using GNU parallel, I've tried this and various versions of it where I move the quotes around
find -name "*.Rmd" | parallel Rscript -e "rmarkdown::render('{}')"
But I keep getting errors.
/bin/bash: -c: line 0: syntax error near unexpected token `('
/bin/bash: -c: line 0: `Rscript -e rmarkdown::render('./test.Rmd')'
I think this is something to do with where the quotation marks are because I get different errors depending on where I put them. What is the problem? Is it doing something funny like only trying to parallelize Rscript and not what comes after it?
From man parallel:
If you get errors like:
sh: -c: line 0: syntax error near unexpected token
sh: Syntax error: Unterminated quoted string
sh: -c: line 0: unexpected EOF while looking for matching `''
sh: -c: line 1: syntax error: unexpected end of file
zsh:1: no matches found:
then you might try using -q.
So:
find -name "*.Rmd" | parallel -q Rscript -e "rmarkdown::render('{}')"

R error: '\U' used without hex digits in character string starting "'C:\U"

Upon start up, when I run Rstudio I get the following error message:
"Error: '\U' used without hex digits in character string starting "'C:\U"
This is not an issue when trying to run scripts within Rstudio. However, this error prevents me from running any R script as a local job or outside of RStudio.
I unistalled and reinstalled R 4.0.0 and I am still seeing the same error. When I restart R this is how it appears in the console..
Restarting R session...
Error: '\U' used without hex digits in character string starting "'C:\U"
I do not have any paths in my Rscript or Rprofile. I tried setting my working directory in my Rprofile and it did not remove the error. Any ideas?
I had the same issue and after I deleted .Rprofile and .radian_history from my user directory, the error message disappeared.

Is there a way for R to run a script and on error return non-0 to the shell?

Given a file script.R containing 3 lines :
print('hello')
stop('user error')
print('world')
we can run it :
$ R -f script.R
> print('hello')
[1] "hello"
> stop('user error')
Error: user error
> print('world')
[1] "world"
but that continues after the error. I want it to halt the script on error. This does that :
$ R -e "source('script.R')"
> source('script.R')
[1] "hello"
Error in eval(expr, envir, enclos) : user error
Calls: source -> withVisible -> eval -> eval
Good. It halted running the script on the error. But the return value to the shell is 0 (success) :
$ echo $?
0
Is there anyway to run the script, halt on error, and return non-zero to the shell? I searched and I couldn't find an answer.
I could grep the output file for the text "Error" but that has some risk that needs managing; e.g. greping the wrong output file somehow and two or more runs writing to the same file. Those issues can be managed but returning non-zero to the shell would be simpler and more robust. Adding a line to the end of the script is also a workaround since I'd need to add that line to all the scripts.
Thanks to #Pascal in comments, it turned out that in my .Rprofile I had :
options(error=quote(dump.frames()))
When I run with --vanilla as well to prevent my .Rprofile from being loaded :
$ R --vanilla -f script.R
> print('hello')
[1] "hello"
> stop('user error')
Error: user error
Execution halted
$ echo $?
1
Which is exactly what I wanted and solves the problem. Thanks #Pascal!

Write errors to file

Is there a way to write my R errors to a file? I run R on bash via:
R --vanilla < myprogram > myprogram.out &
When my program encounters an error (not a syntax error...like an illegal replacement or something) it stops but the error line isn't written to the output file and I don't know what the program was and a lot of the time I log out from the server while it runs.
Thanks,
Josh
Use the R CMD BATCH <infile> <outfile> syntax instead.

Syntax error only when command is run from cron

This command:
/usr/bin/mysqldump --add-drop-table -u myuser -pmypass mydb > "/home/myuser/dbBackups/"`date +%Y%m%d`".sql"
works fine from the command line but whenb cron runs it I get
/bin/sh: -c: line 0: unexpected EOF while looking for matching ``'
/bin/sh: -c: line 1: syntax error: unexpected end of file
The command is all on one line in the crontab as well so I'm confused by the line 0 and line 1 references...
Can anyone advise me as to what I am doing wrong there?
It's the obvious dumb question, but do you have the matching backquote in your crontab (crontab -l)?
The line one, line zero stuff isn't referring to the lines in the crontab, only to the 'lines' in the one-line script.
Updated:
Ah, I think I've got it. This is from crontab(5):
Percent-signs (%) in the command, unless escaped with backslash (\),
will be changed into newline characters, and all data after the
first % will be sent to the command as standard input.
So the percent characters in your date spec are being interpreted as newlines, which means the backquote isn't terminated before the newline, which would produce your error message.
So escape the percent characters. I'd forgotten that about crontab....
The easiest fix is probably to put the whole command in a shell script and just have that be run. So make a scriptName.sh file that contains the command you listed and have crontab call that script. That gets around all these odd problems.
Commands executed from cron do not have access to the environment variables from your login shell, including the path. So try the following (adding fully qualified path to date):
/usr/bin/mysqldump --add-drop-table -u myuser -pmypass mydb > "/home/myuser/dbBackups/"`/usr/bin/date +%Y%m%d`".sql"
Of course, verify if your date command is located elsewhere by running which date then adjust the path if necessary.

Resources