Search code examples
rwebpage-screenshotr-taskscheduler

Automatical web-screenshot via taskscheduleR


I promise I'm not an expert of sites policy and that all the information that I'll eventually get will be used for didactical studies, as I'm a student.

My purpose is to get a sequence of images of a web page (on which i will apply techniques of image recognition in future). I thought to write some lines of code in R to make the pc automatically make screenshots at certain times.

I thought that Rpackages taskscheduleR and webshot might work for me.

I wrote a first script which is the scheduler:

library(taskscheduleR)
myscript <- system.file("extdata", "shooter.R", package = "taskscheduleR") 

## run script
taskscheduler_create(taskname = "SCHEDULE_TRAINING", rscript = myscript,  
                     schedule = "MINUTE", starttime = format(Sys.time() + 5, 
                     "%H:%M"))

## delete the tasks
taskscheduler_delete(taskname = "SCHEDULE_TRAINING")

and then i wrote a second script which makes the screenshot (shooter.R):

library(webshot)
#webshot::install_phantomjs() # only the first time 

URL="https://www.hattrick.org"

time0=gsub(":","",Sys.time())
time=gsub(" ","",time0)
out=paste(time,".png",sep="")

webshot(URL, out, delay=1)

The second script works if I execute it individually and the screenshot appears in my folder, although if I execute the script through the first script it doesn't work (even if in the output I don't get any error) and no screenshots appear.

Someone could help me or knows something alternative I can do to reach my goal?


Solution

  • Create new notepad and write below and save it to r.bat

    start /min T:\Repository\Mehul\shooter.R
    
    • Avoid space in path.
    • Write full path of r script.
    • When you create schedule task run r.bat instead of shooter.R
    • Set r script to run on default program in Rscript.exe that will be find from ..\R-3.4.3\bin\Rscript.exe