AutoIt and RSelenium to navigate Save As dialogue (Firefox) - r

I have been trying now for days (if not weeks...) to get the code below to work. What I am trying to achieve is that an R script runs daily (via batch script and Windows Task Scheduler on Windows Server 2008 64bit).
That R script shall navigate to certain websites, log in and invoke the Save As dialogue to save complete pages to a specific path.
If I run my script on my machine (Win 8 - 64bit) within RStudio it works like a charme - remotely and via Rscript.exe it does not.
The problem appears to be with the SaveAs.au3 script - when I call it via RStudio or from file explorer it works perfectly. The same R Script executed within a batch file with:
"C:\Program Files\R\R-3.2.3\bin\x64\Rscript.exe" "C:\JN\abc.R"
works up until the system() command as well and crahses then without providing any error or warning.
Maybe there is someone who had a similar problem and can help? Thanks!
The 3 AutoIt scripts are the following:
SaveAs.au3 / exe
ControlFocus("[CLASS:MozillaWindowClass]", "", "")
ControlSend("[CLASS:MozillaWindowClass]", "", "", "^s")
EditName.au3 will be variably written from within R and then called
KeyEnter.aut3
ControlFocus("Save as", "", "")
ControlClick("Save as","","[CLASS:Button; INSTANCE:1]")
The R script looks as follows:
# compl is a vector of n href
if(length(compl) != 0) {
foreach(i=1:length(compl)) %do% {
server_check() # checks whether selenium driver is still active and firefox window is open
remDr$navigate(compl[i])
Sys.sleep(10)
login_check() # checks whether login is still active
Sys.sleep(5)
print(paste("attempt to save:",compl[i]))
system('C:\\JN\\SaveAs.exe') # does not matter whether .exe or .au3
Sys.sleep(3)
system("cmd", input = c('echo ControlSetText("Save as", "", "[CLASS:Edit; INSTANCE:1]", "") > C:\\JN\\EditName.au3',
paste0('echo ControlSend("Save as", "", "[CLASS:Edit; INSTANCE:1]", "',
gsub("/","_",gsub(website_url,"", compl[i])), ".htm",
'") >> C:\\JN\\EditName.au3')))
Sys.sleep(3)
system('C:\\"Program Files (x86)"\\AutoIt3\\AutoIt3.exe C:\\JN\\EditName.au3')
Sys.sleep(8)
system('C:\\"Program Files (x86)"\\AutoIt3\\AutoIt3.exe C:\\JN\\KeyEnter.au3')
Sys.sleep(30)
}
}
print("Complete save end")

The problem was related to Windows Server 2008 R2 - if you disconnect from the remote session the server will initiate a screen server (or alike) making the AutoIt script unable to interact with the GUI (as there is non...)
There are options within Windows Server to prevent the server from disabling the GUI, but my script was still not stable enough to run on its own for months...

Related

How to release R's prompt when using 'system'?

I am writing an R code on a Linux system using RStudio. At some point in the code, I need to use a system call to a command that will download a few thousand of files from the lines of a text file:
down.command <- paste0("parallel --gnu -a links.txt wget")
system(down.command)
However, this command takes a little while to run (a couple of hours), and the R prompt stays locked while the command runs. I would like to keep using R while the command runs on the background.
I tried to use nohup like this:
down.command <- paste0("nohup parallel --gnu -a links.txt wget > ~/down.log 2>&1")
system(down.command)
but the R prompt still gets "locked" waiting for the end of the command.
Is there any way to circumvent this? Is there a way to submit system commands from R and keep them running on the background?
Using ‘processx’, here’s how to create a new process that redirects both stdout and stderr to the same file:
args = c('--gnu', '-a', 'links.txt', 'wget')
p = processx::process$new('parallel', args, stdout = '~/down.log', stderr = '2>&1')
This launches the process and resumes the execution of the R script. You can then interact with the running process via the p name. Notably you can signal to it, you can query its status (e.g. is_alive()), and you can synchronously wait for its completion (optionally with a timeout after which to kill it):
p$wait()
result = p$get_exit_status()
Based on the comment by #KonradRudolph, I became aware of the processx R package that very smartly deals with system process submissions from within R.
All I had to do was:
library(processx)
down.command <- c("parallel","--gnu", "-a", "links.txt", "wget", ">", "~/down.log", "2>&1")
processx::process$new("nohup", down.comm, cleanup=FALSE)
As simple as that, and very effective.

R beakr script as Rscript Windows 10 service

I'm trying to setup a simple beakr service in Windows that implements the example at https://github.com/MazamaScience/beakr. I'm able to run the script from the command line successfully and I've been able to add the service in Windows using NSSM, but I am unable to start the service.
When I dig into the service error logs I see that Rscript.exe cannot be executed due to a non-specific permissions problem.
My Rscript.exe is running out of C:\Program Files\R<Version>\bin and my beakr.R script is running out of my User home directory.
If anyone has had success implementing a similar service (Web page based REST endpoint) using R in Windows, I would love to know how you did it.
This is what I did to run an R script as Service using latest pre-release version of NSSM on Windows 10:
Create a directory to store the files
In this example : C:\R\ServiceTest
Create in this directory a never ending script : ServiceTest.R
library(beepr)
# Test script : beeps every 10 seconds
while (T) {
beepr::beep(1)
if (interactive()) {
# Shows spin cursor to facilitate test in interactive mode
for (i in 1:10) {
if (i%%4==0) {cursor <- '/'}
if (i%%4==1) {cursor <- '-'}
if (i%%4==2) {cursor <- '\\'}
if (i%%4==3) {cursor <- '|'}
cat('\r',cursor)
flush.console()
Sys.sleep(1)
}
} else {
Sys.sleep(10)
}
}
I used to let this kind of script run in a console open on my desktop to check various alarms regularly.
Create a batch file to run the script : ServiceTest.bat
Rscript ServiceTest.R
Open an Admin console and make sure the batch file runs correctly:
C:\R\ServiceTest>ServiceTest.bat
C:\R\ServiceTest>Rscript ServiceTest.R
|
Cancel the batch (Ctrl+C)
Using the Admin console, install the batch file as service using NSSM :
nssm install
Set Service name : ServiceTest
Set Application path : C:\R\ServiceTest\ServiceTest.bat
Set Working directory : C:\R\ServiceTest\
Set Logon : Windows User + Password
Install Service
Open Windows Services Manager, find ServiceTest and start it : if everything went well, that's it!
If you get an error message, check Windows Event Log / Services : you can find there hints on the cause of the problem. Most common problems I encountered :
error on path
used local user instead of own user account / password to run the service
If you want to remove the service :
nssm remove ServiceTest
This replaced very nicely the many R consoles I left running in the background on my Desktop.
I see no reasons it wouldn't work with a REST endpoint.

Deploying shinyapps on a desktop

I have a shiny app that runs perfectly on my computer and shinyapps.io. This app is being built for a client where i need to share it to run on their desktops. I have used the guidance given on this here.
The first line of my shiny server reads a RDS file and loads the data. I have copied the RDS within the shiny folder as given in the example shared above. My current folder looks like this:
C:/dist/
GoogleChromePortable
R-Portable
shiny - within shiny folder i have ui.R, server.R, lhg.RDS
run.vbs
runShinyApp.R
run.vbs
Randomize
CreateObject("Wscript.Shell").Run "R-Portable\App\R-Portable\bin\i386\R.exe CMD BATCH --vanilla --slave runShinyApp.R" & " " & RND & " ", 0, False
runShinyApp.R
.libPaths("./R-Portable/App/R-Portable/library")
browser.path <- file.path(getwd(),"GoogleChromePortable/GoogleChromePortable.exe")
options(browser = browser.path)
shiny::runApp("./Shiny/",port=8888,launch.browser=TRUE)
My server begins like this:
lhg <- readRDS(file.path(getwd(),"LHG.RDS"))
When I run the vbs file, it opens a browser, but throws a error message:
Error:object "lhg" not found
And everytime I run, the error creates a new file with some random numbers that contains the logs of the same error.
Can someone help please?
You might want to try out the RInno package (I just published it to CRAN last month). It helps automate these types of desktop deployments, and you should be able to get setup with:
install.packages("RInno")
require(RInno)
RInno::install_inno()
The directory containing ui.R & server.R is app_dir:
create_app(app_name = "Your app's name", app_dir = "path/to/app_dir")
compile_iss()
The resulting installer should install your app "as is," so you don't need to manage those tricky working directory issues. If it works in your dev environment, it should work after it is installed. If you'd like more resources on how to customize your installation, check out FI Labs - RInno

Opening two Rstudio sessions while one is sinking

In Rstudio I have one script which is running and using sink() to save the outputs to a text file. However, when I open up a second R studio session, while the first session is still running, it seems like I am unable to print anything to the console. How do you print to the console in Rstudio while another session is using sink?
For example open up Rstudio and start by setting workspace to source file, with this as your source file
#set up sinking
logFile = file("logMod.txt")
sink(file=logFile)
N=10000;
N.c=10000;
#run program
for (i in 1:N){
for (j in 1:N.c){
if( i*j %% 10000==0 ) print(i*j)
}
}
sink()
then open up another Rstudio session and try to print to console. I can't do it. I can open the standard R gui and print to console but not Rstudio. This is all done on a windows machine.

R and System calls

I have used R in the past to do very basic calls to the commmand line. The example can be found here.
This time around, I am looking to mimic this code which runs successfully from the command line in Windows:
> cd C:\Documents and Settings\BTIBERT\My Documents\My Dropbox\Eclipse\Projects\R\MLB\retrosheet\rawdata
> bgame -y 2010 2010bos.eva >2010bos.txt
This is the code I am trying to run inside of R. I have already set the working directory inside of R.
dir <- paste("cd", getwd(), sep=" ")
system(dir)
system("bgame -y 2010 2010bos.eva >2010bos.txt")
I am sure this is user error, but what am I doing wrong? It appears to work initially, but returns the following error. I very well could be doing something wrong, but I believe I am using the same commands.
Expanded game descriptor, version 109(185) of 05/08/2008.
Type 'bgame -h' for help.
Copyright (c) 2001 by DiamondWare.
[Processing file 2010bos.eva.]
>2010bos.txt: can't open.
Warning message:
running command 'bgame -y 2010 2010bos.eva >2010bos.txt' had status 2
Any help you can provide will be appreciated.
You need to issue all commands in one system() call:
system(paste("cd",getwd() "&& bgame -y 2010 2010bos.eva >2010bos.txt",sep=" "))
You should already be in your working directory, so I'm not sure the cd getwd() is necessary. And you may need quotes around your path because it contains spaces. The error may be resolved by putting spaces around >.
If I were in your shoes, I would try this:
system("bgame -y 2010 2010bos.eva > 2010bos.txt")
UPDATE:
And you should probably heed this advice in the "Differences between Unix and Windows" section of ?system that says you should use shell:
• The most important difference is that on a Unix-alike
‘system’ launches a shell which then runs ‘command’. On
Windows the command is run directly - use ‘shell’ for an
interface which runs ‘command’ _via_ a shell (by default the
Windows shell ‘cmd.exe’, which has many differences from the
POSIX shell).
This means that it cannot be assumed that redirection or
piping will work in ‘system’ (redirection sometimes does, but
we have seen cases where it stopped working after a Windows
security patch), and ‘system2’ (or ‘shell’) must be used on
Windows.
Has no-one else found that system("dir", intern = T) for example doesn't work, but that you need system("cmd.exe /c dir", intern = T)? Only the latter works for me. I found this at the discussion site here (William Dunlap's post, about a third of the way down).
Also, it doesn't work with the "cd" command, but you can use the setwd() function within R and then the command will be executed within that directory.
I created the following functions for convenience, for executing programmes and running commands:
#the subject is an input file that a programme might require
execute <- function(programme, subject.spec = "", intern = FALSE, wait = FALSE){
if(!identical(subject.spec, "")){subject.spec <- paste0(" ", subject.spec)} #put space before the subject if it exists
system(paste0("cmd.exe /c ", programme, subject.spec), intern = intern, wait = wait)
}
command <- function(command, intern = TRUE, wait = FALSE){
system(paste("cmd.exe /c", command), intern = T, wait = wait)
}
Does it break your code when you get error 1 or does execution continue?
Whenever executing system commands through another language it is useful to print the system call before you call it to see exactly what is happening, pull up the shell you are intending to use and check for the same error. As the command is executed correctly this could be a hickup in bgame or R.
If you look at http://astrostatistics.psu.edu/datasets/R/html/base/html/shell.html you can see the variable flag passed to the system call."flag the switch to run a command under the shell. If the shell is bash or tcsh the default is changed to "-c"."
Also "the shell to be used can be changed by setting the configure variable R_SHELL to a suitable value (a full path to a shell, e.g. /usr/local/bin/bash)."

Resources