Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6802539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:14:08+00:00 2026-05-26T19:14:08+00:00

I am using Rscript more and more where I would have normally used bash

  • 0

I am using Rscript more and more where I would have normally used bash scripts. One small annoyance is that many of these scripts loop over some system() call that leaves basically no time for R to catch my control-c if I try to interrupt it. Instead it just aborts the system command that was running and continues on to the next loop iteration. For example, when I try to interrupt the following by holding down control-c, it still makes it through all the iterations:

for(i in 1:10) {
  cat(i)
  system('sleep 3')
}

So far I have always just hacked around this by inserting a small pause in each loop like

for(i in 1:10) {
  Sys.sleep(0.25)
  cat(i)
  system('sleep 3')
}

that will let me abort within an iteration or two if I hold down control-c, but I’m wondering, is there a more efficient way to accomplish this behavior?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:14:08+00:00Added an answer on May 26, 2026 at 7:14 pm

    John, I’m not sure if this will help, but from investigating setTimeLimit, I learned that it can halt execution whenever a user is able to execute an interrupt, like Ctrl-C. See this question for some of the references.

    In particular, callbacks may be the way to go, and I’d check out addTaskCallback and this guide on developer.r-project.org.

    Here are four other suggestions:

    1. Although it’s a hack, a very different approach my be to invoke two R sessions, one is a master session and the other simply exists to execute shell commands passed by the master session, which solely waits for a confirmation that the job was done before starting the next one.

    2. If you can use foreach instead of for (either in parallel, via %dopar%, or serial %do% rather than %dopar% or w/ only 1 registered worker), this may be more amenable to interruptions, as it may be equivalent to the first suggestion (since it forks R).

    3. If you can retrieve the exit code for the external command, then that could be passed to a loop conditional. This previous Q&A will be helpful in that regard.

    4. If you want to have everything run in a bash script, then R could just write one long script (i.e. output a string or series of strings to a file). This could be executed and the interrupt is guaranteed not to affect a loop, as you’ve unrolled the loop. Alternatively, you could write loops in bash. Here are examples. Personally, I like to apply commands to files using find (e.g. find .... -exec doStuff {} ';') or as inputs via backquotes. Unfortunately, I can’t easily give well-formatted code on SO, since it embeds backquotes inside of backquotes… See this page for examples So, it may be the case that you could have one command, no looping, and apply a function to all files meeting a particular set of criteria. Using command substitution via backquotes is a very handy trick for a bash user.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using TortoiseSVN against VisualSVN I delete a source file that I should not have
I am using R, on linux. I have a set a functions that I
Using report builder 3.0, I have a report that queries a cube. How do
I have R code (using ggplot2) that pumps out a bunch of charts to
Using CRM 4, I have an entity form that contains a tab with an
I have a R script I'm running now that is currently using 3 correlated
Using online interfaces to a version control system is a nice way to have
Using ASP.NET MVC there are situations (such as form submission) that may require a
Using C#, I need a class called User that has a username, password, active
I have the following script #!/usr/bin/Rscript print (shebang works) in a file called shebang.r.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.