title: Launch RSelenium Browser regardless of port open/closed
body: <p>Question: Is there a function that will open a selenium browser if none is already open, or close the current browser and reset the port and relaunch a browser?</p>
  
  <p>Rationale: I work with big loops in RSelenium that occasionally crash, so sometimes I don't know if a port is open or browser is open in looped code. I'd like a RSelenium launcher that launches a browser regardless if one is open or if the port is in use. </p>
  
  <p>Attempt: I tried this approach with tryCatch() but I still get the wdman error that a port is open if I try to launch it twice:</p>
  
  <pre><code>browserpreference &lt;- "chrome"
  tryCatch({rD &lt;- rsDriver(port = 4444L, browser = paste0(browserpreference))}
    ,error=function(rD,remDr){
            try(remDr$close(), silent=T)
            try(rD$server$stop(),silent=T)
            try(suppressWarnings(rm(rD, envir = .GlobalEnv)), silent=T)
            try(suppressWarnings(rm(remDr, envir = .GlobalEnv)), silent=T)
            gc()
            rD &lt;- rsDriver(port = 4444L, browser = paste0(browserpreference))
          })
  </code></pre>
  
  <p>If I try this twice I get this error:</p>
  
  <pre><code>Error in wdman::selenium(port = port, verbose = verbose, version = version,  : 
    Selenium server signals port = 4444 is already in use.
  </code></pre>
  
  <p>Thanks!</p>