Mac to Windows Compatibility with an R Script Calling Bash - r

Current script
I work on Mac OSX. I have an R script that I like to use to organize my files and produce .pdf via LaTex. In order to (1) produce the .pdf with LaTeX and (2) manipulate some files and directories I use Bash script from R with system("").
From Mac to Windows
I would like to offer this R script (once compiled to make an executable) to someone that knows nothing about programming and uses Windows. I have no idea how the windows prompt commands work and what language it uses. I am afraid that (1) I cannot use the function system("") on Windows and (2) I cannot use bash such as system("rm dir/file.txt").
Question
What do you think would be the easiest for me in order to make my script compatible with Windows (given that I know nothing about Windows)? Will I be able to use system("") and Bash from R on Windows?

I think the best solution is to handle both cases separately (keep in mind that windows paths are also different from unix paths).
if(.Platform$OS.type == "unix") {
system("rm dir/file.txt")
[...]
}
else {
system("DEL /Q dir\file.txt")
[...]
}
You should have no problem searching for unix equivalent commands in windows cmd.
Also keep in mind that if you need the R script to exec LaTeX binaries, it would pretty much kill the portability because of the huge dependencies that LaTeX has.

Related

How to make R executable file

Is it possible to create an executable file where I can just upload the excels and an output is generated based on the coding and without sharing it as well.
On Linux or OSX you can make an R script double clickable with the shebang trick:
Add #!/usr/bin/Rscript as the first line
Make the script executable with chmod +x
On OSX, there is also the option to convert an R script into an application bundle with Platypus. On Windows, I do not know whether this is easily achievable at all, especially as executables usually are not placed in the search path on Windows.
is using R-Shiny apps can cover that? Its an R based program and you can customize the output / logic there.

Is there an easy way to run bash scripts on R Markdown, using Windows 10?

I've been learning R Markdown for the past months. It was fairly easy to get Anaconda Python to run within a R Markdown script, but I haven't yet found a way to get to run chunks of bash code within the same document.
Using
```{bash}
echo hello world
```
I get the error message
/bin/bash: C:\Users\MyName\AppData\Local\Temp\Rtmp0MpNfS\chunk-code-4cbc213a3545.txt: No such file or directory
Might any of you guys know what might be the root of this problem?
I know maybe is to late but I had the same problem and I find the solution.
First, in Windows 8.1 or if you donĀ“t have the Windows Subsystem for Linux (WSL) you can install Cygwin: https://cygwin.com/ > Install Cygwin
This is the recommendation pointed out in R Markdown: The Definitive Guide (https://bookdown.org/yihui/rmarkdown/) > 2.7 Other language engines > 2.7.2 Shell scripts
Second, add Cygwin to the PATH. Usually Cygwin is installed in C:\cygwin64 and the path to include will be in this case C:\cygwin64\bin
Third, check that everything is correct using base::Sys.which("bash") in R
If everything is correct it will appear something like this "C:\\cygwin64\\bin\\bash.exe"
Fourth, you can run in a Rmarkdown document, the chunk that you pointed out to double check:
```{bash}
echo hello world
```

calling Qiime with system call from R

Hej,
When I try to call QIIME with a system call from R, i.e
system2("macqiime")
R stops responding. It's no problem with other command line programs though.
can certain programs not be called from R via system2() ?
MacQIIME version:
MacQIIME 1.8.0-20140103
Sourcing MacQIIME environment variables...
This is the same as a normal terminal shell, except your default
python is DIFFERENT (/macqiime/bin/python) and there are other new
QIIME-related things in your PATH.
(note that I am primarily interested to call QIIME from R Markdown with engine = "sh" which fails, too. But I strongly suspect the problems are related)
In my experience, when you call Qiime from unix command line, it usually creates a virtual shell of it`s own to run its commands which is different from regular system commands like ls or mv. I suspect you may not be able to run Qiime from within R unless you emulate that same shell or configuration Qiime requires. I tried to run it from a python script and was not successful.

Unix command to print directory structure in the form of a tree?

I am a newbie to UNIX, i want to print tree structure of files in a directory. below image is example in DOS, what will be the command of Unix to achieve same objective
I think you are looking for the "tree" command. If you are having issues running it you might have to find out how to install it on your specific distribution. For ubuntu installs you can find instructions here:
https://askubuntu.com/questions/507588/not-able-to-install-tree-comand-in-ubuntu
Not sure what you mean by "on Unix". What OS are you running, specifically? Tree should be compatible on Unix systems. You may just have to compile it for your particular OS.
This command prints output like the following (on cygwin):

Can I invoke linux shell commands in a R session?

I am using RStudio and at times I want to remove some files in the working directory (e.g., previously generated .csv files).
It is quite inconvenient to frequently switch to bash and execute rm. Is there any way of invoking commands in the R console?
See here
Use system (or shell) as agstudy's comment says
e.g. system("pwd")
If you are just removing files, rather than executing arbitrary commands on the shell, you would be better off following Thomas's suggestion:
?file.remove
Using this function instead of shell("rm example.csv") is safer and more portable.

Resources