Skip to main content
Turns out sink throws a warning, not an error!
Source Link
Scott Ritchie
  • 10.4k
  • 3
  • 30
  • 64

Based on @mnel's comment:

sinkall <- function() {
  i <- sink.number()
  while (i > 0) {
    sink()
    i <- i - 1
  }
}

Should close all open sinks.

You may also encounter this problem when dealing with devices and plots, where the number of open devices isn't reported anywhere. For a more general case you could use this:

stopWhenError <- function(FUN) {
  tryCatch({
    while(TRUE) {
      FUN()
    }
  }, warning = function(w) {
    print("All finished!")
  }, error = function(e) {
    print("All finished!")
  })
}

stopWhenError(sink) # for sink.
stopWhenError(dev.off) # close all open plotting devices.

EDIT: sink throws a warning not an error so I've modified the code so that it won't run forever, whoops!

Based on @mnel's comment:

sinkall <- function() {
  i <- sink.number()
  while (i > 0) {
    sink()
    i <- i - 1
  }
}

Should close all open sinks.

You may also encounter this problem when dealing with devices and plots, where the number of open devices isn't reported anywhere. For a more general case you could use this:

stopWhenError <- function(FUN) {
  tryCatch({
    while(TRUE) {
      FUN()
    }
  }, error = function(e) {
    print("All finished!")
  })
}

stopWhenError(sink) # for sink.
stopWhenError(dev.off) # close all open plotting devices.

Based on @mnel's comment:

sinkall <- function() {
  i <- sink.number()
  while (i > 0) {
    sink()
    i <- i - 1
  }
}

Should close all open sinks.

You may also encounter this problem when dealing with devices and plots, where the number of open devices isn't reported anywhere. For a more general case you could use this:

stopWhenError <- function(FUN) {
  tryCatch({
    while(TRUE) {
      FUN()
    }
  }, warning = function(w) {
    print("All finished!")
  }, error = function(e) {
    print("All finished!")
  })
}

stopWhenError(sink) # for sink.
stopWhenError(dev.off) # close all open plotting devices.

EDIT: sink throws a warning not an error so I've modified the code so that it won't run forever, whoops!

added 8 characters in body
Source Link
Ben Bolker
  • 221.6k
  • 25
  • 382
  • 477

Based on @mnel's comment:

sinkall <- function() {
  i <- sink.number()
  while (i > 0) {
    sink()
    i <- i - 1
  }
}

Should close all open sinks.

You may also encounter this problem when dealing with devices and plots, where the number of open devices isn't reported anywhere. For a more general case you could use this:

stopWhenError <- function(FUN) {
  tryCatch({
    while(TRUE) {
      FUN()
    }
  }, error = function(e) {
    print("All finished!")
  })
} 

stopWhenError(sink) # for sink.
stopWhenError(dev.off) # close all open plotting devices.

stopWhenError(sink) # for sink. stopWhenError(dev.off) # close all open plotting devices.

Based on @mnel's comment:

sinkall <- function() {
  i <- sink.number()
  while (i > 0) {
    sink()
    i <- i - 1
  }
}

Should close all open sinks.

You may also encounter this problem when dealing with devices and plots, where the number of open devices isn't reported anywhere. For a more general case you could use this:

stopWhenError <- function(FUN) {
  tryCatch({
    while(TRUE) {
      FUN()
    }
  }, error = function(e) {
    print("All finished!")
  })
}

stopWhenError(sink) # for sink. stopWhenError(dev.off) # close all open plotting devices.

Based on @mnel's comment:

sinkall <- function() {
  i <- sink.number()
  while (i > 0) {
    sink()
    i <- i - 1
  }
}

Should close all open sinks.

You may also encounter this problem when dealing with devices and plots, where the number of open devices isn't reported anywhere. For a more general case you could use this:

stopWhenError <- function(FUN) {
  tryCatch({
    while(TRUE) {
      FUN()
    }
  }, error = function(e) {
    print("All finished!")
  })
} 

stopWhenError(sink) # for sink.
stopWhenError(dev.off) # close all open plotting devices.
Removed clunge
Source Link
Scott Ritchie
  • 10.4k
  • 3
  • 30
  • 64

Based on @mnel's comment:

sinkall <- function() {
  i <- sink.number()
  while (i > 0) {
    sink()
    i <- i - 1
  }
}

Should close all open sinks.

You may also encounter this problem when dealing with devices and plots, where the number of open devices isn't reported anywhere. For a more general case you could use this:

stopWhenError <- function(FUN) {
  tryCatch({
    while(TRUE) {
      tryCatch({ FUN() }, error = function(e) { break })
    }
  }, error = function(e) {
    # This is a bit clungy since break inside tryCatch above complains
    print("All finished!")
  })
}

stopWhenError(sink) # for sink. stopWhenError(dev.off) # close all open plotting devices.

Based on @mnel's comment:

sinkall <- function() {
  i <- sink.number()
  while (i > 0) {
    sink()
    i <- i - 1
  }
}

Should close all open sinks.

You may also encounter this problem when dealing with devices and plots, where the number of open devices isn't reported anywhere. For a more general case you could use this:

stopWhenError <- function(FUN) {
  tryCatch({
    while(TRUE) {
      tryCatch({ FUN() }, error = function(e) { break })
    }
  }, error = function(e) {
    # This is a bit clungy since break inside tryCatch above complains
    print("All finished!")
  })
}

stopWhenError(sink) # for sink. stopWhenError(dev.off) # close all open plotting devices.

Based on @mnel's comment:

sinkall <- function() {
  i <- sink.number()
  while (i > 0) {
    sink()
    i <- i - 1
  }
}

Should close all open sinks.

You may also encounter this problem when dealing with devices and plots, where the number of open devices isn't reported anywhere. For a more general case you could use this:

stopWhenError <- function(FUN) {
  tryCatch({
    while(TRUE) {
      FUN()
    }
  }, error = function(e) {
    print("All finished!")
  })
}

stopWhenError(sink) # for sink. stopWhenError(dev.off) # close all open plotting devices.

Added a more general function that will also work with `dev.off()`
Source Link
Scott Ritchie
  • 10.4k
  • 3
  • 30
  • 64
Loading
Source Link
Scott Ritchie
  • 10.4k
  • 3
  • 30
  • 64
Loading