Home > OS >  How to close a url form R?
How to close a url form R?

Time:10-09

I am attempting to open a url and close a url in an effort to fool my virtual machine into thinking I am working. I work in R with linx os. So I need it to open a browser wait a minute and close the browser. my code so far:

repeat{
 startTime <- Sys.time()
 site <- browseURL("https://youtu.be/_W0bSen8Qjg")
 #does not work
 open(site)
 sleepTime <- startTime   20 *60*60*60 -Sys.time()
 #does not work
 close(site) 
 if (sleepTime >0)
   Sys.sleep(sleepTime)
}

I can not figure out how to make the browser close. Any thoughts or ideas would be helpful .

CodePudding user response:

Great project :)

I'd recommend working with library(processx) as it allows us to control the started process from within R. See ?process.

I tested this on windows 10, but it should work in a similar way on linux (after adapting the paths):

library(processx)

while (TRUE) {
  myProcess <- process$new("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe", "https://stackoverflow.com/")
  # myProcess$is_alive()
  # myProcess$get_pid()
  Sys.sleep(10)
  try(myProcess$kill(), silent = TRUE)
}

Here is another approach hosting a shiny app which opens a new window every 10s (and closes the previous session):

library(shiny)
library(shinyjs)

ui <- fluidPage(
  useShinyjs(),
  p("My dummy app")
)

server <- function(input, output, session) {
  observe({
    Sys.sleep(10)
    runjs("window.close();")
    browseURL("http://127.0.0.1:3838/")
  })
}

myDummyApp <- shinyApp(ui, server)

runApp(appDir = myDummyApp,
        port = 3838,
        launch.browser = TRUE,
        host = "127.0.0.1")

CodePudding user response:

Also, we can to make a solution with only basic things ;)

a <- "TRUE"
while(a == TRUE){
    browseURL("https://stackoverflow.com", 
             browser = "C:\\Program Files\\Internet Explorer\\iexplore.exe")
Sys.sleep(10)
system ("TASKKILL /IM iexplore*")
}
  • Related