I am trying to run exiftool through R to get metadata from photos using the system() command. When I run this on a mac, it works fine, but from windows I am not linking up with the cmd.exe properly and getting the following error from this code:
exif_datetime <- function(path) {
exif_cmd <- 'exiftool.pl -T -r -DateTimeOriginal '
cmd <- paste(exif_cmd, "'", path, "'" ,sep='')
exif_time <- system(cmd, intern = TRUE)
exif_time
}
photo_time <- exif_datetime('C:/Users/photo.jpg')
photo_time
Error in system(cmd, intern = TRUE) :
'CreateProcess' failed to run 'C:\Windows\exiftool.pl -T -r -DateTimeOriginal 'C:/Users/photo.jpg''
When I run the exiftool command from cmd.exe in Windows, I get the proper result. My exiftool.pl is in the C:Windows folder on my computer. Is there something regarding the PATH that I am missing? Also, I remember something about windows needing a shell, but I have not figured out if that is what I need in my case nor how to create one properly.
Thanks for all your suggestions. I found a solution that works for me, involving the shell() command. I thought that it had to be incorporated with the system() command somehow, but it appears that it will work on its own.
exif_datetime <- function(path) {
exif_call <- 'exiftool.pl'
exif_cmd<-' -r -T -DateTimeOriginal '
exif_timestamp <- shell(paste(exif_call, exif_cmd, path), intern=T)
exif_timestamp
}
photo_time <- exif_datetime('C:/Users/photo.jpg')
photo_time
[1] "2016:02:14 11:50:29"
Related
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")
I've been using the here package to make my projects more portable. It works great apart from when I use cronR to schedule some of my scripts. When I run my_script.R from Rstudio I get a message from library(here):
here() starts at /home/pd/projects/my_proj
When I set script.R to run using cronR I get a different message:
here() starts at /home/pd
Which is where my_schedule.cron is stored. Ideally I want to keep my_schedule.cron where it is. I can see from the logs that my_script.R runs fine apart from when it comes to saving data because the path used by here() is incorrect. Is there anyway to get the here function to detect the project dir when my_script.R is run from cronR or the terminal?
You can modify the command cmd that is usually created with cron_rscript() by adding cd to your project folder followed by the usual part:
cmd <- "cd /home/pd/projects/my_proj && /usr/lib/R/bin/Rscript ./my_script.R >> ./my_script.log 2>&1"
cron_add(command = cmd, frequency = 'daily', at = '18:00')
If the first line of your #rstats script is wd <- here(), I will come
into your lab and SET YOUR COMPUTER ON FIRE.
Learn how to use environment variables
wd <- Sys.getenv("HOME")
wd <- file.path(wd, "projects", "my_proj")
Or use the 'Additional arguments to Rscript' element in the cronR user interface to pass something extra to the Rscript and fetch it with commandArgs().
If you don't use the cronR interface but cron_rscript, use cronR::cron_rscript(..., rscript_args = "/home/pd/projects/my_proj")
args <- commandArgs(trailingOnly = TRUE)
if(length(args) > 0){
wd <- args[1]
}
I fuzz up with issue,
I create 2 files .r and .sh in same home folder, here is my code
abc.sh
#!/bin/bash
NAME="Ali"
Rscript /home/cdsw/abc.R $NAME --save
abc.R
#args <- commandArgs(trailingOnly = TRUE), I used this option also
args <- commandArgs()
message(args)
I unable to get the output,
Here is the output
/usr/local/lib/R/bin/exec/R--sense--no-readline
Please help me out...
I'm new to R and I'm stuck in a very simple task.
I want to run an R script from console, but I want the script to be able to read user inputs.
This is how I'm reading from the script:
library = readLines(n = 1L)
if(library == "1")
{
library = "GUDHI"
}
And this is how I'm running my script from R-Portable with a .bat:
#echo on
cd..
cd..
cd..
cd..
cd..
PATH C:\Users\MyUser\Desktop\App\RFolder\R-Portable\App\R-Portable\bin;%path%
cd C:\Users\MyUser\Desktop\App\RFolder
Rscript Phom.R 1
pause
When I run this .bat it throws an error (Argument is of length zero):
As if the console didn't wait for user input.
The problem is not the .bat code. If I remove the readLines functions from my script and hardcode the input, it works perfectly. I also tried the readline function with no success.
Thanks.
Solution for Interactive R script from Windows CMD:
cat("Prompt Message: ")
library = readLines(con = "stdin", 1)
I'm not sure if the prompt MUST end with ": ", but I had trouble when I removed that piece of string.
This worked for me, I hope this helps somebody.
I found out that it is possible to open the windows explorer with a predefined path via the cmd.exe program using the following command:
explorer PATH
Back in R using the following command opens up the windows explorer:
system("explorer", intern=TRUE)
However, when I specify a path R returns the following warning message and does not open the explorer at the specified path:
> system("explorer C:\\Users\\xxx", intern=TRUE)
character(0)
attr(,"status")
[1] 1
Warning message:
running command 'explorer C:\Users\xxx' had status 1
I quoted the \ otherwise R complained about not recognising \U from \Users.
However, when the command is executed as we would expect the double \ are replaced by only one .
When I copy paste the explorer C:\Users\xxx bit from the R warning message into the cmd.exe program the explorer is opened up at the specified path.
Does anybody have any idea why that fails?
try
shell("explorer C:\\Users\\xxx", intern=TRUE)
I've created this simple function... I hope it'll be useful!
wopen <- function(x){
y <- getwd()
y <- gsub("/", "\\\\", y)
shell(paste0("explorer ", y), intern = TRUE)
}
Briefly: it takes the current directory, changes the slash direction and call to cmd.exe to open it. Regards.