Write CMD output to text file using R - r

I'm writing a cryptocurrency mining application where I want to log the output of the miner to a text file. Each miner can be started by using a Windows Batch File or a Windows Command Script. When I try to use the shell function like so:
shell("cd C:\\Users\\Alex\\Documents\\Mining\\bin\\ccminer & SIGT >> C:\\Users\\Alex\\Documents\\Mining\\logs\\sigt.txt")
where SIGT is the Windows Command Script, R doesn't accept any new inputs, since the process in the command line hasn't finished (as it runs indefinitely). I want to be able to analyse the output, stop the mining, change to a different mining program etc., all within R. Any help would be greatly appreciated.

Related

R.exe and Rscript.exe

I'm new to programming and I'm confused on the difference between the two. I have googled this and I am still confused on the difference after reading the responses.
Part of the reason I am confused is I am thinking in terms of running script in Batch files. For instance, lets say I have a script in R and I create a batch file that runs the script where I use R.exe. When I put this in the command prompt and run the batch file, it just takes the script I made and runs it in the console of R right?
I've seen that you can run batch files uses Rscript.exe, which confuses me because when if I take an R script I made and put it into the script part of R (above the console) how would this do anything since the script must be put into the console for it to run. (Unless Rscript.exe runs whatever it is in the script part of R?)
If anyone could please explain how this all works to me, I would greatly appreciate it.
Thanks!
First, some terminology: even though the concept of batch processing is generic, and it means unassisted execution, the term batch file is usually reserved for MS-Windows files processed by cmd.exe, MS-Windows traditional script files. The term used for files containing R commands is usually R scripts, or Rscripts.
That said, please consider the following simple R script, named HelloFriend.R:
my.name <- readline(prompt="Enter name: ")
print(paste("Hello, ", my.name, "!"))
When run directly in R console, as
> source('HelloFriend.R')
it will show the output
Enter name:
If the user types Some Name and hits Enter, the program will output
[1] "Hello, Some Name !"
If it's run in the command line as R --no-save --quiet < HelloFriend.R, it will generate the output
> my.name <- readline(prompt="Enter name: ")
Enter name:
> print(paste("Hello, ", my.name, "!"))
[1] "Hello, !"
>
And finally, if run with Rscript --vanilla HelloFriend.R, it will generate the output
Enter name:
[1] "Hello, !"
In other words, when run inside the R console, the user input will be expected. When run under R, but in the command line, the program will not give the user the opportunity to type anything, but the command echo will be shown.
And finally, under Rscript, the user input will also not be expected, but the command echo will not be shown.
Rscript is the preferred form of running R scripts, as its name suggests. The passing of R scripts in the command line to R via redirection also gives batch processing but will echo the commands executed. Therefore it can help debug code, but it's not the preferred way of executing production code.
The analogy with batch files is a good one. R.exe is for interacting with the language, entering one statement at a time, and evaluating the results before entering the next statement. Rscript.exe is for running an existing script (file) containing R commands. You generally invoke Rscript.exe with the name of the script.
Running Rscript.exe my_script.R from the command-line is sort of like running
source("my_script.R")
q("no")
from the R console.

Run .R functions via CMD - missing execute all command?

Good day,
I want to automate data cleaning and merging via R. I have created multiple folders to which you upload excel files. Than with R I clean the data and merge them to a final file.
I would like to do this process without running R or Rstudio. To accomplish this I run R via CMD (.bat), however, the code does not run.
In my R script I have multiple functions calling from other files, I assume that I need a simple "Run all command" to execute the code via CMD?
Example of Combine_all_excel_files.R
setwd("C:\\Code source\\")
source("Seperate_input_folder_by_years.R")
Seperate_input_folder_by_years()
Merge data.bat file:
start "" "C:\Program Files\R\R-3.3.3\bin\x64\Rscript.exe" CMD BATCH "C:\Code source\Combine_all_excel_files.R"
pause
When I run the .bad file, the code does not run. What I am doing wrong?

Automating R to run scripts

I'm basically looking for any way to automatically run R scripts just like it would run as if I was copy and pasting it into console. I've tried the package 'taskscheduleR' however it just seems to output to a log file in the directory which isn't as if I were to just run it inside the Rstudio application.
An example might be, say I want to get the last closing stock prices of 5 stocks each night, then the script in Rstudio and have the variables there and all of the code would be in the script file.
Any thoughts?
I would suggest the in-built Task Scheduler application if you using Windows.
Create a task that will run a batchscript file. This batchscript file has only 1 line which executes the Rscript you want. Set it to run each night (or whatever time you want).
I am not that well-versed in linux and MacOS but here's what I know:
Linux has cron. Add a job to crontab with your preferred timing and execute your script 'path/to/bin/r /path/to/script.r'
MacOS has Automator + iCal (for scheduling). It also has crontab like Linux.

Using R and with the Terminal

I have combed the internet looking for a tutorial on how to use R through the terminal and I can't find anything that exactly fits what I'm looking for. I have a group of R scripts. They need to be run in a specific order. Each produces a set of .csv files to be used as inputs for the next script that will be executed.
I have used R CMD BATCH filename.R and it makes an output file called filename.Rout
But then when I try to use cat filename.Rout, I get an error Fatal error: cannot open file 'filename.R': No such file or directory
Also I'm not sure where my output (.csv files) should be written to. In my R script, there are some write.csv lines pointing the new datasets to be written to a specific directory but when I run R CMD BATCH filename.R in the Terminal, I don't see the files where they should be. I see a filename.Rout in another directory but I can't open it.

batch process for R gui

I have created a batch file to launch R scripts in Rterm.exe. This works well for regular weekly tasks. The < PBWeeklyMeetingScriptV3.R > is the R script run by Rterm.
set R_TERM="C:\Program Files\R\R-2.14.0\bin\x64\Rterm.exe"
%R_TERM% --slave --no-restore --no-save --args 20120401 20110403 01-apr-12 03-apr-11 < PBWeeklyMeetingScriptV3.R > PBWeeklyMeetingScriptV3.batch 2> error.txt
I've tried to modify this to launch the R GUI instead of the background process as I'd like to inspect and potentially manipulate and inspect the data.
If I change my batch file to:
set R_TERM="C:\Program Files\R\R-2.14.0\bin\x64\Rgui.exe"
the batch file will launch the R GUI but doesn't start the script. Is there a way to launch the script too?
Alternatively is there a way to save/load the work space image to access the variables that are created in the script?
You can save and load workspaces by using save.image() and load(). I do this all the time when scripting to pass data sets between two separate script files, tied together using Python or bash. At the end of each R script, just add:
save.image("Your_image_name.RData")
The image will be the workspace that existed whenever the command was run (so, if it's the last command in the file, it's the workspace right before the exist of the file). We also use this at my job to create "snapshots" of input and output data, so we can reproduce the research later. (We use a simple naming convention to get the time of run, and then label the files with that).
Not sure about launching and then running the GUI with specific scripts in it; I don't think that's a feature you'll find in R, simply because the whole point of running a batch file is usually to avoid the GUI. But hopefully, you can just save the image to disk, and then look at it or pass it to other programs as needed. Hope that helps!

Resources