break/exit script

前端 未结 8 1365
Happy的楠姐
Happy的楠姐 2021-01-30 15:22

I have a program that does some data analysis and is a few hundred lines long.

Very early on in the program, I want to do some quality control and if there is not enou

8条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-30 16:23

    You can use the pskill function in the R "tools" package to interrupt the current process and return to the console. Concretely, I have the following function defined in a startup file that I source at the beginning of each script. You can also copy it directly at the start of your code, however. Then insert halt() at any point in your code to stop script execution on the fly. This function works well on GNU/Linux and judging from the R documentation, it should also work on Windows (but I didn't check).

    # halt: interrupts the current R process; a short iddle time prevents R from
    # outputting further results before the SIGINT (= Ctrl-C) signal is received 
    halt <- function(hint = "Process stopped.\n") {
        writeLines(hint)
        require(tools, quietly = TRUE)
        processId <- Sys.getpid() 
        pskill(processId, SIGINT)
        iddleTime <- 1.00
        Sys.sleep(iddleTime)
    }
    

提交回复
热议问题