How to attach to an already running R script - r

I have an R script that has been running for a couple of days. I need to change something there but don't want to lose the calculations that have already been done. Is there a way to attach to it and pause and debug? Like with gdb you can attach to an already running process.

I'm sorry to say that there is not. Not given that the script is already running.
Some extra info that won't make you feel better:
You shouldn't write scripts that have to run for days (at least in R), at least not without having them write out RDS files to save your progress.
You could've set up debugging in RStudio before you ran the script.
If you were running Rcpp code you could've used gdb with it (more info from Hadley Wickam's site here).
You can learn more about debugging R code here.

Related

How do I recover an R script?

I was running an R script code in Rstudio and suddenly my computer power had been lost. Now, I open the script and I can't find anything in my script except red dots (see the below figure).
I know that a similar question has been asked here. However, the answers don't work for me. How can I recover my running code?
There is a file in my directory C:\Users\Mohammad Nabati\AppData\Local\RStudio-Desktop named history_database.1 that I found the history of running.

When should I restart R session, GUI or computer?

I use R, Rstudio and Rcpp and I spent over a week debugging some code, that was just giving errors and warnings in unexpected places, in some cases with direct sample code from online or package documentation.
I often restart the R session or Rstudio if there are obvious problems and they usually go away.
But this morning it was really bad to the point were basic R commands would fail and restarting R did nothing. I closed all the Rstudio sessions and restarted the machine for good measure, (which was unnecessary).
When it came back and I re-loaded the sessions everything seems to be working.
Even the some rcpp code I was working on for weeks with outside packages will now compile and run where it gave gibberish errors before.
I have known for a while that R needs to be restarted once in a while, but I know it when basic functions don't run, how can I know earlier.
I am looking for a good general resource or function that can tell me I need to restart because something is not running right. I would be nice if I can also know what to restart.
Whether the R session, the GUI such as Rstudio, all sessions and GUIs or a full machine restart.
For as long as I have been dabbling with or actually using R (ie more than two decades), it has always been recommended to start a clean and fresh session.
Which is why I prefer to work on command-line for tests. When you invoke R, or Rscript, or, in my case, r (from littler) you know you get a fresh session free of possible side-effects. By keeping these tests to the command-line, my main sessions (often multiple instances inside Emacs via ESS, possibly multiple RStudio sessions too) are less affected.
Even RStudio defaults to 'install and restart' when you rebuild a package.
(I will note that a certain development package implies you could cleanly unload a package. That has been debated at length, and I think by now even its authors qualify that claim. I don't really know or case as I don't use it, having had established workflows before it appeared.)
And to add: You almost never need to restart the computer. But a fresh clean process is a something to use often. Your computer can create millions of those for you.

Automating R Scripts on Mac

So I wrote my father a neat little R script that pulls financial indicators on stocks, and outputs the info to a csv...
I would like to have it set up so that the script will run automatically once a day, skipping the weekends if possible. I looked around for awhile online and it seems as though the Mac "Automator" App is what I'm looking for.
However, after reading many guides and posts (like this one https://www.r-bloggers.com/how-to-source-an-r-script-automatically-on-a-mac-using-automator-and-ical/) I cannot get it to work...
In trying to replicate what this man did above I get the error that the first path is a directory; while the latter returns stuff like "cat: Rscript: No such file or directory"
So I was wondering if anyone could recommend either any good free software that will allow me to do what I would like, or how to run an R script from the /bin/bash shell
EDIT:
The suggested solution isn't really answering my problem. The issue is making this as easy as possibly for my dad to run, that way he doesn't have to do anything, specifically use the terminal. Ideally I could just schedule a task that repeats every morning, but the cronR package requires Daemon, and the others are just command line tools
I had a similar experience.
I created an automator calendar alarm
added a Execute AppleScript Action and used the following code:
on run {input, parameters}
try
tell application "R"
activate
with timeout of 90000 seconds
cmd "source(\"Dropbox/RScripts/CV19/liibre_coronabr.R\")"
end timeout
end tell
end try
return input
end run
When you save it, just choose the date and time for it to run and select the option to repeat everyday
That's it!

Scheduling R Script - OSX

I have written a series of R Scripts that create csv files. From there, Tableau will read the csv's and update various dashboards. As Tableau can easily be scheduled to update on a daily cadence, I was hoping to do the same with my R Script.
While there are a bunch of answers already with solutions for Windows, there hasn't been a solution posted for OSX. I have looked into trying to run my script in Terminal and use automator to do it, but couldn't quite figure it out. Basically, when the shell script runs it terminates midway through because there are errors in the R Script - but I do not care about the errors. The Automator didn't work as well.
Additionally, I also looked into Data Integration/Pentaho but the additional software configuration and subsequent installation seemed difficult.
Any help or insight would be greatly appreciated! Thanks!
Type crontab -e and add this line to the resulting file
#daily Rscript 1.R && Rscript 2.R
It will run the files 1.R, followed by 2.R at midnight every day. Hope that helps.
The most flexible way to do this is to use launchd, the service that manages processes on OS X. You can look at some examples in the official documentation.

How to run R script from command line repeatedly but only load packages the first time

I want to run an R script (in Win 7) from SQL Server 2014 each time a new record is added (to perform some analysis on the data). I saw that this can be done with the xp_cmdshell command which is like running it manually from the command line.
My problems (and questions) are:
I've made out from various websites that probably the best option is to use Rscript. This would have to be used at the command line as:
C:\Program Files\R\R-3.2.3\bin\x64\Rscript "my_file_folder\my_file.r
Can I copy Rscript.exe to the folder where my script is, such that I can run my script independently, even if R is not installed? What other files do I need to copy together with Rscript.exe such that it would work independently?
My script loads some packages that contain functions that it uses. Is there a way to somehow include these in the script such that they don't have to be loaded every time (it takes about 5 sec so far and I need this script to be faster)? Or is there a way to only load these packages the first time that the script runs?
In case the overall approach I've described here is not the best one, I am open to doing it differently. Maybe there is a way to somehow package the R script together with all the required dependencies (libraries and other parts of the R software which the script would need to run independently).
What I ultimately need is a for the script to run silently, and reasonably fast, without any windows or anything else popping up, each time a new record is added to my database, do the analysis and exit.
Thanks in advance for any answers.
UPDATE:
I figured out an elegant solution to running the R script. I'm setting up a job in SQL Server and inside that job I'm using "xp_cmdshell" to run my script as a parameter to Rscript.exe, as detailed at point 1 above. I can start this job from any stored procedure and the beauty of it is that the stored procedure does not wait for the script to finish. It just triggers the job (that runs the script in a separate thread) and then it continues with its business.
But questions from points 1 and 2 still remain.

Resources