Error running ImageMagick from R: Invalid parameter - r

I am trying to make a GIF-animation in R. I have an array of matrices which i wish to convert into a GIF animation. My strategy is inspired from this example:
http://ryouready.wordpress.com/2010/11/21/animate-gif-images-in-r-imagemagick/
where the following code produces 11 PNG-Pictures with the "png"-function in R. Next it calls for the external ImageMagick-program "convert" to compile the GIF animation.
dir.create("examples")
setwd("examples")
# Animated countdown from 10 to "GO!".
png(file="example%02d.png", width=200, height=200)
for (i in c(10:1, "G0!")){
plot.new()
text(.5, .5, i, cex = 6)
}
dev.off()
# convert the .png files to one .gif file using ImageMagick.
system("convert -delay 80 *.png example_1.gif")
#shell("convert -delay 80 *.png example_1.gif")
The problem is that R doesn't seem to finde the exe-file "convert" which is a part of ImageMagick and installed on the C-drive (C:\Program Files\ImageMagick-6.8.5-Q16). In the comments to the website i am linking to earlier, it is suggested for Windows users to use "shell" instead of "system" to run external programs but none of the two work. The error message is
Invalid parameter - 80
Warning message:
running command 'convert -delay 80 *.png example_1.gif' had status 4
I've tried to change the Windows PATH enviroment variable in the systems properties, as suggested in this answer, but the PATH-variable was allready corectlly defined on my system. I also tried specifying the whole string of the convert.exe file, but also without luck...
How can i get ImageMagick to run through R?
Specs:
Windows 7 Servicepack 1,
R 3.0.0
Thanks in advance...

On Windows, there are several convert.exe commands, all of which are in the PATH. So you must specify the path to the right convert.exe executable. In my case, I had it in the LyX folder (however, you will find it in the ImageMagick installation too).
Be careful with the quotes, the backslashes and the spacing if you are pasting. E.g. from within R:
system('"C:\\Program Files (x86)\\LyX 2.0\\imagemagick\\convert.exe" -delay 20 -loop 0 files_*.png animation.gif')

I'm a windows 10 user, after defining the working directory I got it working in R using
shell("convert -set delay 80 -loop 0 *.jpg example_shell_test.gif")
within cmd each command means the following
convert = open convert function from ImageMagick
-set delay x = set the delay time between each frame to x (1000 = 1 second)
-loop 0 = loop forever, if set to 1 it will go through the images once
*.[image type]= and file of .[image type]
[name of output gif].gif = save new .gif as
I got it working first within command prompt by navigating to the directory and running the line
convert -set delay 80 -loop 0 *.jpg example_cmd_test.gif
Before this I was using
-delay = 80
rather than
-set delay 80
in cmd and got error:
convert: invalid argument for option '-delay': = # error/convert.c/ConvertImageCommand/1277.
In R using the system() command with the correct "-set delay x" I was getting error:
> system("convert -set delay = 80 -loop 0 *.jpg example_3.gif")
Invalid Parameter - delay
Warning message:
running command 'convert -set delay = 80 -loop 0 *.jpg example_3.gif' had status 4
other errors in shell()
> shell("convert -set delay = 80 -loop 0 *.jpg example_shell_test11.gif")
convert: unable to open image '80': No such file or directory # error/blob.c/OpenBlob/3094.
convert: no decode delegate for this image format `' # error/constitute.c/ReadImage/509.
Warning messages:
1: running command 'C:\WINDOWS\system32\cmd.exe /c convert -set delay = 80 -loop 0 *.jpg example_shell_test11.gif' had status 1
2: In shell("convert -set delay = 80 -loop 0 *.jpg example_shell_test11.gif") :
'convert -set delay = 80 -loop 0 *.jpg example_shell_test11.gif' execution failed with error code 1
I ran it in R with shell() after and it seems to work fine
shell("convert -set delay = 80 -loop 0 *.jpg example_shell_test.gif")
Do have a look at this thread also
ImageMagick - Issue with Windows and convert function

I know someone else already found the solution to your problem but there is an easier way to solve it without having to include the entire pathway in system(). Simply setani.options(convert = 'pathway/convert.exe')
after loading the animation package.

After attempting all of these fixes as well as these and these to no success, I used alternative software to make the conversion, several of which are described here. I am a Windows user and found the simple instructions contained in that site for VirtualDub quickly accomplished this task.

Related

How can I activate a '.js' script from R?

I have a '.js' script that I usually activate from the terminal using the command node script.js. As this is part of a process where I first do some data analysis in R, I want to avoid the manual step of opening the terminal and typing the command by simply having R do it for me. My goal would be something like this:
...R analysis
write.csv(df, "data.csv")
system('node script.js')
However, when I use that specific code, I get the error:
sh: 1: node: not found
Warning message:
In system("node script.js") : error in running command
Of course, the same command runs without problem if I type it directly on the terminal.
About my Software
I am using:
Linux computer with the PopOS!
RStudio 2021.09.1+372 "Ghost Orchid"
R version 4.0.4.
The error message node: not found indicates that it couldn't find the program node. It's likely in PATH in your terminal's shell, but not in system()'s shell (sh).
In your terminal, locate node by executing which node. This will show the full path to the executable. Use that full path in system() instead.
Alternatively, run echo $PATH in your terminal, and run system('echo $PATH') or Sys.getenv('PATH') in R. Add any missing directories to R's path with Sys.setenv(PATH = <your new path string>)
Note that system2() is recommended over system() nowadays - but for reasons unimportant for your case. See ?system and ?system2 for a comparison.
Examples
Terminal
$ which node
/usr/bin/node
R
system('/usr/bin/node script.js')
# alternatively:
system2('/usr/bin/node', c('script.js'))
or adapt your PATH permanently:
Terminal
% echo $PATH
/usr/local/bin:/home/caspar/programs:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin
R
> Sys.getenv('PATH')
[1] "/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/Applications/RStudio.app/Contents/MacOS/postback"
> Sys.setenv(PATH = "/home/caspar/programs:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/Applications/RStudio.app/Contents/MacOS/postback")

Error installing hugo using blogdown::hugo_install()

I am unable to install hugo using the blogdown package. When I run blogdown::hugo_install() I get the following message and see a pop-up window with a message that hugo is downloading. After the pop-up window appears, R / RStudio just hangs though. For reference, I have just updated R and my RStudio version is 1.2.5033. I am running Windows 10.
The latest Hugo version is 0.69.0 trying URL 'https://github.com/gohugoio/hugo/releases/download/v0.69.0/hugo_extended_0.69.0_Windows-64bit.zipContent length 649 bytes
I also tried running blogdown:hugo_install(version = 0.69) . When I run that, I get a different error:
trying URL 'https://github.com/gohugoio/hugo/releases/download/v0.69/hugo_extended_0.69_Windows-64bit.zip'
trying URL 'https://github.com/gohugoio/hugo/releases/download/v0.69/hugo_extended_0.69_Windows-64bit.zip'
trying URL 'https://github.com/gohugoio/hugo/releases/download/v0.69/hugo_extended_0.69_Windows-64bit.zip'
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 9 100 9 0 0 9 0 0:00:01 --:--:-- 0:00:01 44
Error in install_hugo_bin(exec) :
Unable to install Hugo to any of these dirs: C:\Users\dougj\AppData\Roaming/Hugo,
In addition: Warning messages:
1: In download.file(url, output, ..., method = method) :
cannot open URL 'https://github.com/gohugoio/hugo/releases/download/v0.69/hugo_extended_0.69_Windows-64bit.zip': HTTP status was '404 Not Found'
2: In download.file(url, output, ..., method = method) :
cannot open URL 'https://github.com/gohugoio/hugo/releases/download/v0.69/hugo_extended_0.69_Windows-64bit.zip': HTTP status was '404 Not Found'
3: In download.file(url, output, ..., method = method) :
cannot open URL 'https://github.com/gohugoio/hugo/releases/download/v0.69/hugo_extended_0.69_Windows-64bit.zip': HTTP status was '404 Not Found'
4: In utils::unzip(zipfile) : error 1 in extracting from zip file
5: In file.copy(exec, destdir, overwrite = TRUE) :
problem copying .\hugo.exe to C:\Users\dougj\AppData\Roaming\Hugo\hugo.exe: No such file or directory
Lastly, I tried installing the dev version of blogdown, downloading the hugo binary, and then manually installing using blogdown::hugo_install_bin(path_to_unzipped_binary.exe) but I get this error when I try that:
Error: 'install_hugo_bin' is not an exported object from 'namespace:blogdown'
Any suggestions?
The latest Hugo version is 0.69.0
trying URL 'https://github.com/gohugoio/hugo/releases/download/v0.69.0/hugo_extended_0.69.0_Windows-64bit.zip'
Content length 649 bytes
According to the message above, the content length doesn't seem to be correct (should be about 13Mb). I don't know why, but you might be behind a firewall. If that's the case, you could download this zip file in your web browser, and pass the local file path of the zip file to blogdown::install_hugo(), e.g.,
# change the path below to the actual path of the zip file on your computer
blogdown::install_hugo('~/Downloads/hugo_extended_0.69.0_Windows-64bit.zip')
For the rest of your issues:
I also tried running blogdown:hugo_install(version = 0.69) . When I run that, I get a different error
That is because there is no version 0.69. There is only 0.69.0 (you can check it on the Github releases page).
Lastly, I tried installing the dev version of blogdown, downloading the hugo binary, and then manually installing using blogdown::hugo_install_bin(path_to_unzipped_binary.exe)
The function hugo_install_bin() is not exported from blogdown. You don't really need it (use install_hugo() instead).

How do I open a document from R?

I want to open a file from within R.
I can launch the software (graphpad prism) with the following:
system2("C:/Program Files (x86)/GraphPad/Prism 7/prism.exe")
I expected this to open my prism file as if I were double clicking on it or running it from cmd, but it didn't:
system2("H:/Graphs/Shell/Templates/NASH4_Standard.pzfx")
I am receiving the message:
Warning message: running command
'H:/Graphs/Shell/Templates/NASH4_Standard.pzfx' had status 127
I see that this is not an error but just a warning. Am I unintentionally "shelling" the document in the background? How would I make sure it pops up as a window?
Status 127 was addressed here, but for launching the software, not opening the document with it.
In Windows environments, you need to call a command line interpreter like CMD prompt or PowerShell. Also, any file path that has spaces needs to be enclosed in double quotes above the quotes needed in R for string literals (the case for your .exe not specific file).
With system() send entire command in one string:
system('cmd /c "H:/Graphs/Shell/Templates/NASH4_Standard.pzfx"')
# POWER SHELL REQUIRES MORE QUOTE ESCAPING (ONLY ONE PAIR W/O SPACES)
system('powershell & """H:/Graphs/Shell/Templates/NASH4_Standard.pzfx"""')
With system2() use the args parameter:
# FILES
system2('cmd', args=c('/c', '"H:/Graphs/Shell/Templates/NASH4_Standard.pzfx"'))
system2('powershell', args=c(' & """H:/Graphs/Shell/Templates/NASH4_Standard.pzfx"""'))
# EXECUTABLES
system2('cmd', args=c('/c', '"C:/Program Files (x86)/GraphPad/Prism 7/prism.exe"'))
system2('powershell', args=c(' & """C:/Program Files (x86)/GraphPad/Prism 7/prism.exe"""'))
shell.exec("C:/Program Files (x86)/GraphPad/Prism 7/prism.exe")
does it work for you ?
ps. and shell.exec("MyWorkbook.xls") open file with default program

How to crontab a R-script and append the errors

I'm new to Ubuntu and would like to run a daily crontab. I'm running similar job under Windows Command Line but would like to switch to Ubuntu.
My R-Script has an Error-function which prints errors into a separate
folder everytime a job is done and an error has occured ():
# define error function
my.error.fun <- function() {
cat(geterrmessage(), file="/home/network/R/Test.txt", append=TRUE)
}
# output error
options("error"=my.error.fun)
Under crontab -e I have determined the following:
00 16 * * * /usr/bin/Rscript /home/R/network/Test.R 2> /home/R/RErrors/Test.txt
and the crontabhas been saved under /tmp/crontab.2t/crontab
where I append the errros inside the R-Script. In contrast to Windows Command Line that concept seems not to work in Ubuntu That seems not to work however. I'm not sure whether thats the correct syntax. There has been no file in the RErrors folder.
May I as for some suggestions?

Error in untar( ) while using R

I am new to the R programming language and am having basic issues with it. I want to untar a file, but it has not been able to work for me.
Here is the code that I enter:
untar("CD_data.tar", exdir="data")
It then returns the following error message:
/bin/sh: /usr/bin/gnutar: No such file or directory
Warning message:
In untar("CD_data.tar", exdir = "data") :
‘/usr/bin/gnutar -xf 'CD_data.tar' -C 'data'’ returned error code 127
Please help! Thanks!
R on OS X 10.9 (Mavericks) seems to set a wrong TAR environment variable.
You can fix this by adding the following to your .Rprofile (or executing it manually):
Sys.setenv(TAR = '/usr/bin/tar')
Alternatively, you can provide the tar path as an argument when calling untar.
My 2 cents is that you are using a mac and have not installed tar. You are getting value 127 because the command is not found within your $PATH and it's not a built-in command (which is usually the case if you were in unix...
In other words you need to install tar.
Or run it in linux.

Resources