I'm currently running some queries to a database and getting back some big files as a result. I have encountered the common problem of Windows not freeing the memory, even though I 'rm()' everything and (edit) calling 'gc()'. One workaround i have found is using .rs.restartR() in Rstudio.
This though requires me to constantly watch my script, in order to continue it after the session restart. Is it possible to automate it? If not what other methods do people use to overcome this problem?
You could break the code into 2 files and write a batch file (.bat) that runs the first file through .rs.restartR() and then the remainder of the code in the next file.
You could also skip the .bat and just schedule both .R scripts to run in Task Scheduler.
Also, please see my comment regarding garbage collection (gc()).
Related
I'm running a long R script, which takes 2 or 3 days to finish. I accidentally run another script, which, if it works as R usually does, will go in some queue and R will run it as soon as the first script is over. I need to stop that, as it would compromise the results from the first script. Is there a visible queue or any other way to stop R from running some code?
I'm working on an interactive session in R studio, on windows 10.
Thanks a lot for any help!
Assuming you're running in console (or interactive session in R studio, that's undetermined from your question) and that what you did was sourcing a script/pasting code and while it was running pasting another chunck of code:
What is ongoing is that you pushed data into R process input stream, it's a buffered input, so it will run each line once the previous line call has ended and free the process.
There's no easy way to play with an input buffer, that's R internal input/output system and mostly it's the Operating system which have those information in cache for now.
Asking R itself is not possible as it already has this buffer to read, any new command would go after.
Last chance thing: If you can spot your another chunck of code starting in your console, you can try pressing the esc key to stop the code running.
You may try messing with the process buffers with procexp but there's a fair chance to just make your R session segfault anyway.
To avoid that in the future, use scripts and run them on the command line separately with Rscript (present in R bin directory under windows too despite the link pointing to a linux manpage).
This would create one session per script and allow to kill them independently. That said if they both write to the same place (database, a file would create an error if accessed by two process) that won't prevent data corruption.
I am guessing OP has below problem:
# my big code, running for a long time
Sys.sleep(10); print("hello 1")
# other big code I dropped in console while R was still busy with above code
print("hello 2")
If this is the case, I don't think it is possible to stop the 2nd process from running.
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.
I have a cron job that launches an Rscript to go fetch some data and stash it away in a database. Sometimes the source isn't ready so it waits. Sometimes the source skips a data point so the script ends up waiting until another instance is started from cron. Now that two instances are running they cause problems with each other. Is there something like the following pseudo code that I could put at the top of my scripts so they stop when they see another instance of themselves is already running:
stopifnot(Sys.running('nameofscript.r'))
One thing I thought to do would be for the script to make a temp file with a fixed name at the start and then delete that temp file at the end. This way the script can check for the existence of the file to know if it's already running. I also see there's something called flock which is probably a better solution than that but I'd prefer if there was a way R can do it without bash commands (I've never really learned any bash). If there isn't a pure R way to do this, can the R script call flock on itself or would it only work if the cron task calls a bash script that then calls the rscript. If it isn't already obvious, I don't really know how to use flock.
I have a scheduled task in windows to run a R program ("ftp.R")
After many attempts and reading many SO literature, I found that the only way to make it work properly was writing this piece of code into a .bat file:
#ECHO OFF
RSCRIPT ftp.R
Everything goes fine except wqith it, when I am trying to use functions that I have in other R programs.
For example, in the "ftp.R" program I have this pice of code:
source("//BCN-01/Server/R/Main/Production/Check_Integrity.R")
In the "Check_Integrity.R" program I have some functions I need to use in "ftp.R".
The thing is that if I execute the .bat file manually, there is no problem, and the "ftp.R" runs perfectly. But if I run exactly the same .bat file but from the task scheduler the "ftp.R" is unable to find the external functions.
(I am running the code in a Windows Server 2012)
One big difference between running a batch manually / with the scheduler is that the scheduler starts the script with system32 folder as work directory. So it might be enough to add the following line to your batch file: CD %~dp0.
Another point is that the scheduler runs your batch as a different user. So it is possible that you (your user account) has access to //BCN-01/Server/R/Main/Production/ while the system user the scheduler is running your script with does not. You could also try to tell the scheduler to run the script with the same user which you are logged in when running it successfully by hand.
Finally I figured out what was wrong:
All the paths have to be defined as absolute paths: mapped units are not valid.
Althought I already I knew that (thanks to some posts I read here in SO), I missed one path mapped into one of the functions in 'Check_Integrity.R' .
I've found a lot of answers on how to run R from a Batch file, but nothing about running a Batch File from R. I know one way to do this is to use system, system2 or shell, but these methods wait for process in the Windows Command Prompt to finish before R accepts another input. I want to run a Batch File which calls a console application that runs indefinitely, and then allow R to do other things. Any help would be greatly appreciated.
The help page ?shell says how to do it. Just run
shell("MyScript.bat", wait=FALSE)