Why R system is not able to execute hdfs commands? - r

I am trying to merge some .csv files with the
hdfs dfs -getmerge /path/folderfile filename.csv command.
If I run this command from a terminal or from rstudio's internal terminal, it works fine. If I execute the same command using system("hdfs dfs -getmerge /path/folderfile /destinationpath/filename.csv") it returns the following error:
sh: hdfs: command not found
Warning message:
In system("hdfs dfs -getmerge /path/folderfile /destinationpath/filename.csv") :
error in running command
Thanks for suggestions

Related

Error in ls command in linux (ls: reading directory “.”: Operation not permitted) and unable to access jar file

I am trying to extract orc files from the database using ubuntu2_ssh, linux platform.
The orc files are inside this folder called “100622” but I encountered error when “ls” command is typed.
Error message: ls: reading directory “.”: Operation not permitted.
How to solve this ls error ?
Thus when I extract using this command then I got this error message Error: Unable to access jarfile orc_dump.jar.
19166- is converted from epoch time in second (1655942400) -referring to 23Jun
ANY HELP PLEASE?

How do I restart R in linux using a line of code?

I need to restart an R session running in linux using a line of code. When the R session restarts I need it to load an existing R script and for the new R session to be clear of previously loaded packages, variables etc.
I've managed to make this work from within an R terminal in Windows using the following code:
library(startup)
restart(args = c('source("existing_code.R")'))
However when the same code runs in a Linux environment I get the following error:
sh: -c: line 0: syntax error near unexpected token `('
sh: -c: line 0: `'/opt/R/3.6.3/lib/R/bin/exec/R' source("
existing_code.R")'

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")

Spaces in paths in batch mode R

I'm trying to get an R script to run from a batch file so it can be nice and clean for other users. Currently, you drag and drop a CSV file onto the batch file and it passes the file name to the R script for input.
When there's a space in the file path/name it works fine in RStudio but causes problems when I call it from the batch file. When I do that it tries to open the path before the space.
I've tried to reformat the file path from within R by using shortPathName(inputPath) and by replacing spaces with "\ " but it doesn't seem to work.
At the moment, the script is launched with
"%~dp0\R-3.6.0\bin\R.exe" CMD BATCH "--args %~1" "%~dp0\Script.R"
with the script containing
args <- commandArgs(TRUE)
inputPath <- args[1]
inputPath <- shortPathName(inputPath)
inputData <- read.csv(inputPath)
It runs fine from within RStudio but crashes when launched from the batch producing this error message in the output file:
Error in file(file, "rt") : cannot open the connection
Calls: read.csv -> read.table -> file
In addition: Warning message:
In file(file, "rt") :
cannot open file 'file path up to the space': No such file or directory
Execution halted
By no means a R expert, but I'd try
%~dp0\R-3.6.0\bin\R.exe" CMD BATCH "--args %~s1" "%~dp0\Script.R"
The %~s1 should supply the short filename as the argument.
After trying several formulations of the batch file and some debugging, I found that the batch file was passing the first part of the file before the space as the first argument.
After finding that the use of R in CMD BATCH mode is no longer advisable so switched to running using Rscript mode as
"%~dp0\R-3.6.0\bin\Rscript.exe" --vanilla "%~dp0\Script.R" "%~1"
This allowed for the argument to be passed to R with "", and hence with the space.
Since v3.5.1, R accepts file paths with spaces.

R system call returns error

I am trying to execute a python script from within R, with the following command:
system("python ~/speedtest-cli/speedtest-cli --simple")
The error I get is:
python: can't open file '/home/speedtest-cli/speedtest-cli': [Errno 2] No such file or directory
The same command works just fine on the terminal. Am I using the command wrong? Any help is appreciated.

Resources