tag:blogger.com,1999:blog-25839105107179127792024-03-27T16:53:20.954-07:00The aRt ofAnonymoushttp://www.blogger.com/profile/09290563285467003974noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-2583910510717912779.post-14899281263162344582014-05-22T09:48:00.000-07:002014-05-22T21:39:00.746-07:00Orange County R Users Group (OC-RUG): RSelenium Webinar<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta name="generator" content="pandoc" />
<title></title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style type="text/css">
pre:not([class]) {
background-color: white;
}
</style>
</head>
<body>
<div class="container">
<div id="orange-county-r-users-group-oc-rug-rselenium-webinar" class="section level1">
<h1>Orange County R Users Group (OC-RUG): RSelenium Webinar</h1>
<p>Yesterday I gave a webinar on the <a href="https://github.com/johndharrison/RSelenium">RSelenium</a> package to the OC-RUG. When I published RSelenium on CRAN Ray DiGiacomo the president of OC-RUG was kind enough to invite me to present RSelenium and that webinar was held on the 21st of May. I would like to thank OC-RUG and Ray in particular for the opportunity.</p>
<p>I will use this blog to present the video of the webinar and also for a bit of bookkeeping. Firstly I would like to return to two of the questions asked in the Q&A.</p>
<div id="qa-question-2-and-3" class="section level2">
<h2>Q&A question 2 and 3</h2>
<div id="how-would-you-save-a-screenshot-to-file-using-rselenium" class="section level3">
<h3>How would you save a screenshot to file using RSelenium?</h3>
<p>In the seminar I suggested that the Selenium webdriver returned the png in a base 64 encoded format. I tried to save the output to file using <code>writeBin</code> and remarked that I probably needed to decode the output. So here is what I should have done:</p>
<pre><code>b64out <- remDr$screenshot()
writeBin(base64Decode(b64out, "raw"), 'nameoffile.png')</code></pre>
</div>
<div id="how-would-you-use-a-chrome-profile" class="section level3">
<h3>How would you use a chrome profile?</h3>
<p>In the seminar I remarked that the google chrome browser had hundreds of start up options. They are actually termed <a href="http://peter.sh/experiments/chromium-command-line-switches">command line switches</a>. The two I was after were <code>--user-data-dir</code> and <code>--profile-directory</code>. It would then have been sufficient to use:</p>
<pre><code>args = list(paste0('--user-data-dir=',dataDir)
, paste0('--profile-directory=',profileDir))
cprof <- list(chromeOptions = list(args = args))
remDr <- remoteDriver(browserName = 'chrome', extraCapabilities = cprof)
</code></pre>
</div>
<div id="added-to-rselenium" class="section level3">
<h3>Added to RSelenium</h3>
<p>I add the above functionality to RSelenium. So now the <code>screenshot</code> method of the <code>remoteDriver</code> class has an optional <code>file</code> argument. If <code>display</code> is <code>FALSE</code> and a <code>file</code> argument is given <code>RSelenium</code> will now save the <code>png</code> to file.</p>
<p>Secondly there is now a <code>getChromeProfile</code> utility function in a similar vein to the existing getFirefoxProfile function. The function is documented in the RSelenium package. Its basic usage is as follows:</p>
<pre><code># example from windows using a profile directory "Profile 1"
dataDir <- "C:\\Users\\john\\AppData\\Local\\Google\\Chrome\\User Data"
cprof <- getChromeProfile(dataDir, "Profile 1")
remDr <- remoteDriver(browserName = "chrome", extraCapabilities = cprof)</code></pre>
<p>So it takes two arguments: a users chrome data directory and the profile directory within that data directory from which you want to run.</p>
<p>I will update the version of <code>RSelenium</code> on CRAN in the coming weeks. If you would like to run the dev version with added features you can install it with:</p>
<pre><code>devtools::install_github("johndharrison/RSelenium")</code></pre>
<p>and remember to file any issues you may have to github so <code>RSelenium</code> can improve as a package :).</p>
</div>
</div>
<div id="the-webinar-screencast" class="section level2">
<h2>The webinar screencast</h2>
<iframe width="653" height="480" src="//www.youtube.com/embed/ic65SWRWrKA?rel=0" frameborder="0" allowfullscreen></iframe>
</div>
<div id="the-directors-cut" class="section level2">
<h2>The directors cut</h2>
<p>I have shot an extended version of the webinar with the fabled missing <code>javascript</code> example which follows:</p>
<iframe width="653" height="480" src="//www.youtube.com/embed/zcUGla8EjjY?rel=0" frameborder="0" allowfullscreen></iframe>
</div>
<div id="the-slides-and-r-scripts." class="section level2">
<h2>The slides and R scripts.</h2>
<p>And finally I have posted the slides for the webinar to github. Please find them at <a href="http://johndharrison.github.io/RSOCRUG"><a href="http://johndharrison.github.io/RSOCRUG">http://johndharrison.github.io/RSOCRUG</a></a>. There are also some R scripts contained in the master branch of the github project <a href="https://github.com/johndharrison/RSOCRUG">https://github.com/johndharrison/RSOCRUG</a> .</p>
</div>
<div id="conclusion" class="section level2">
<h2>Conclusion</h2>
<p>Hopefully that ties up any loose ends. It was great fun preparing the webinar. I enjoyed giving it and I’m grateful to everyone who attended.</p>
<div class="references">
</div>
</div>
</div>
</div>
<script>
$(document).ready(function () {
$('tr.header').parent('thead').parent('table').addClass('table table-condensed');
});
</script>
<!-- dynamically load mathjax for compatibility with --self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://c328740.ssl.cf1.rackcdn.com/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>
Anonymoushttp://www.blogger.com/profile/09290563285467003974noreply@blogger.com60tag:blogger.com,1999:blog-2583910510717912779.post-27181374138828407792014-03-29T11:16:00.001-07:002014-04-06T06:22:22.502-07:00RStudio/Shiny server on digital ocean fuelled by hot chocolate and a kit-kat chunky<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta name="generator" content="pandoc" />
<meta name="author" content="John D Harrison" />
<meta name="date" content="2014-03-29" />
<title>RStudio/Shiny server on digital ocean fuelled by hot chocolate and a kit-kat chunky</title>
</head>
<body>
<div class="container">
<div id="header">
<h1 class="title">RStudio/Shiny server on digital ocean fuelled by hot chocolate and a kit-kat chunky</h1>
<h4 class="author"><em>John D Harrison</em></h4>
<h4 class="date"><em>29 March 2014</em></h4>
</div>
<div id="introduction" class="section level1">
<h1>Introduction</h1>
<p>A number of people had told me about <a href="https://www.digitalocean.com/?refcode=06ce3c289d3">digitalocean</a> recently so when a new project came along I decided to try it out. Digital Ocean I had gathered was a cloud hosting platform in the vein of amazon and co. I had used AWS previously so I was interested to see how this platform would compare.</p>
<div id="setting-up" class="section level2">
<h2>Setting up</h2>
<p>Creating an account involved giving an email address and creating a password. After this an option was given to load credit onto the account. Paypal was accepted so I used this option to credit the account.
After this I was taken to a basic screeen with a big green button labelled create. Seemed simple enough so I clicked it.
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEizJP-qXyqSJ5v3S2OwJTBjOoijDRr03UhhyZdjsVeE0JScZiyDgAVzs4B6tAf6i3cu6CoQauQJRxJZllwv3sTiCSgHGPBNPTKAwN83_gFqbnMbSAYEXIvm58NSjNEKJc-bJIijQv2Xnl29/s1600/moneyloaded.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEizJP-qXyqSJ5v3S2OwJTBjOoijDRr03UhhyZdjsVeE0JScZiyDgAVzs4B6tAf6i3cu6CoQauQJRxJZllwv3sTiCSgHGPBNPTKAwN83_gFqbnMbSAYEXIvm58NSjNEKJc-bJIijQv2Xnl29/s400/moneyloaded.png" /></a></div>
<a name='more'></a>
</p>
</div>
<div id="stunning-stats" class="section level2">
<h2>Stunning Stats</h2>
<p>The next screen presented the various options for the instance. I choose the 1GB / 1 CPU 30GB SSD Disk 2TB Transfer weighing in at $10 a month. I chose Singapore for my region. This was a plus point if you were in South east Asia like me. I picked ubuntu 12.04 as the OS. Apart from when installing tex live 2013 ubuntu 12.04 and I usually got on. There was an applications tab with things like LAMP on Ubuntu 12.04 and GitLab 6.6.5 CE. This was neat nothing stat orientated however but the option to have LAMP running could have been useful. After scrolling down and choosing these options I was present with a large green create droplet button which I pressed.
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6SX5DGEEqbA-d7h2C2unpxqnlaewLdJiL6XNFpNb0IRl3yhtgEIU8x4KAINUfnd2rn9-lMIh3CYpu0gBgdyNgYUowo4pZByGRqcmkzziEl4Q1ohkptxOKkmP8H1blmfii5LMmv9CSrdTe/s1600/scarygreenbutton.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6SX5DGEEqbA-d7h2C2unpxqnlaewLdJiL6XNFpNb0IRl3yhtgEIU8x4KAINUfnd2rn9-lMIh3CYpu0gBgdyNgYUowo4pZByGRqcmkzziEl4Q1ohkptxOKkmP8H1blmfii5LMmv9CSrdTe/s400/scarygreenbutton.png" /></a></div>
</p>
</div>
<div id="droplets" class="section level2">
<h2>Droplets</h2>
<p>It took about a minute to create my "droplet". Upon creation I was presented with a tab based control panel</p>
<ul>
<li>Power</li>
<li>Access</li>
<li>Resize</li>
<li>Snapshots</li>
<li>Settings</li>
<li>Graphs</li>
<li>Destroy</li>
</ul>
<p>It all seemed fairly straightforward. I picked access which led to a large blue connect button. This led to failure initially as I had a vpn running. Turning of the vpn allowed the vnc to connect and we were ready to go. Initially I had to login as root with a password that digital ocean emailed once the droplet was created.
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgTH0LU4S8QDXc_9KhE81R8xPncWjwz8lp2W2HE2PT4n6vaEKtI68uzjoMMBuQRFH_KGJSv4yrzcSQ4Fs_S7J5jezvSr-A3N4OPjh1L3mMTiDbPP0UN1kHr9DGgzKe9qMxokwAp5-7EsjDa/s1600/initiallogin.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgTH0LU4S8QDXc_9KhE81R8xPncWjwz8lp2W2HE2PT4n6vaEKtI68uzjoMMBuQRFH_KGJSv4yrzcSQ4Fs_S7J5jezvSr-A3N4OPjh1L3mMTiDbPP0UN1kHr9DGgzKe9qMxokwAp5-7EsjDa/s400/initiallogin.png" /></a></div>
</p>
</div>
</div>
<div id="setting-up-ubuntu" class="section level1">
<h1>Setting up ubuntu</h1>
<p>I was now logged in. The first thing to do was change my root password</p>
<pre><code>$ passwd</code></pre>
<p>With that done I set about setting up a non root account <code>john</code></p>
<pre><code>$ adduser john</code></pre>
<p>I didnt want user john to be a second class citizen so I gave the user root access.</p>
<pre><code>$ visudo</code></pre>
<p>brought up a nano editor and I copied the root settings for user john:</p>
<pre><code># User privilege specification
root ALL=(ALL:ALL) ALL</code></pre>
<p>became</p>
<pre><code># User privilege specification
root ALL=(ALL:ALL) ALL
john ALL=(ALL:ALL) ALL</code></pre>
<p>Pressing ctrl+x and saving I figured I was done. Next I would look to install rstudio server.</p>
<div id="rstudio-server" class="section level2">
<h2>RStudio Server</h2>
<p>I followed the guide at <a href="https://www.rstudio.com/ide/download/server.html">https://www.rstudio.com/ide/download/server.html</a> .</p>
<pre><code>$ nano /etc/apt/sources.list</code></pre>
<p>At the bottom of this file I added</p>
<pre><code>deb http://cran.stat.nus.edu.sg/bin/linux/ubuntu precise/
</code></pre>
<p>as the server was based in singapore.
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg7OHjBHqB-mc17xf9h_StmH3-6X78OWcWnH7y0ubcxwuYdYeK7hjB6awwPZQoOX5NZOyPOSvc1NlPGm1kylVryUHrIPjgUuGpwwImxDOQzEZYvO9Hb2P5h_gWYXx257tlWTaTQcKyX1lX9/s1600/addcran.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg7OHjBHqB-mc17xf9h_StmH3-6X78OWcWnH7y0ubcxwuYdYeK7hjB6awwPZQoOX5NZOyPOSvc1NlPGm1kylVryUHrIPjgUuGpwwImxDOQzEZYvO9Hb2P5h_gWYXx257tlWTaTQcKyX1lX9/s400/addcran.png" /></a></div>
From the cran project README</p>
<blockquote>
<p>The Ubuntu archives on CRAN are signed with the key of Michael Rutter <script type="text/javascript">
<!--
h='gmail.com';a='@';n='marutter';e=n+a+h;
document.write('<a h'+'ref'+'="ma'+'ilto'+':'+e+'">'+e+'<\/'+'a'+'>');
// -->
</script><noscript>marutter at gmail dot com</noscript> with key ID E084DAB9. To add the key to your system with one command use (thanks to Brett Presnell for the tip):</p>
</blockquote>
<pre><code>$ apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E084DAB9
</code></pre>
<p>Now I was ready to begin installing rstudio. I updated the sources and installed R.</p>
<pre><code>$ apt-get update
$ apt-get install r-base-dev</code></pre>
<p>We then checked if R was installed.
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiPpGwufnRom003dzFCGi_zo_Mo5qom4MSdeTURe3b1kxVw99_BL9Yit9Z88yJhpFcCasqdu1xWEAGtvsqIYc48_ycE3GFOmd6gxz4_gvrMn5Gfo0bbXJ0vsFank1vRFU8-3SCdhLKkfo4l/s1600/RRRRworRRks.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiPpGwufnRom003dzFCGi_zo_Mo5qom4MSdeTURe3b1kxVw99_BL9Yit9Z88yJhpFcCasqdu1xWEAGtvsqIYc48_ycE3GFOmd6gxz4_gvrMn5Gfo0bbXJ0vsFank1vRFU8-3SCdhLKkfo4l/s400/RRRRworRRks.png" /></a></div>
</p>
<p>Now we tried to install RStudio Server</p>
<pre><code>$ apt-get install gdebi-core
$ apt-get install libapparmor1
$ wget http://download2.rstudio.org/rstudio-server-0.98.501-amd64.deb
$ gdebi rstudio-server-0.98.501-amd64.deb</code></pre>
<p>If everything had gone smoothly I should now be able to log into rstudio remotely.
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi0tJaMrC6VzFptTvbRUyxlyR-yWylg022ZxZA0FyZlOBZ2GNW9WA19gVlt4otCrLEpN1LHcqENkWw7BlHV85XgTEeGjGbnbkKABCPfQEu8wJAkEAolZSC6p_OwDOknjanicM8oBpEmtsQ5/s1600/woooRStudio.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi0tJaMrC6VzFptTvbRUyxlyR-yWylg022ZxZA0FyZlOBZ2GNW9WA19gVlt4otCrLEpN1LHcqENkWw7BlHV85XgTEeGjGbnbkKABCPfQEu8wJAkEAolZSC6p_OwDOknjanicM8oBpEmtsQ5/s400/woooRStudio.png" /></a></div>
</p>
<p>Excellent everything had gone smoothly.</p>
</div>
</div>
<div id="shiny-server" class="section level1">
<h1>Shiny Server</h1>
<p>Next up was shiny server. The relevant rstudio guide was at <a href="https://www.rstudio.com/shiny/server/install-opensource">https://www.rstudio.com/shiny/server/install-opensource</a> . Firstly I installed the shiny package as root.</p>
<pre><code>$ R -e "install.packages('shiny', repos='http://cran.rstudio.com/')"
</code></pre>
<p>I then ran the commands to install shiny server</p>
<pre><code>$ wget http://download3.rstudio.org/ubuntu-12.04/x86_64/shiny-server-1.0.0.42-amd64.deb
$ gdebi shiny-server-1.0.0.42-amd64.deb</code></pre>
<p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEihIahohcZKV5kDdk9DQdqlaVi_EXCpAUHz740dPnzkpvn1SeViGHy-TRCaKrFTU66wDzeiCFO8aRDlRv29AdrOE-hMa6u1WJM6rL3eTZc6OFW9IfosFzePjvr96pQn1Qdt1PZ0RQmEGcXG/s1600/suspectmessages.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEihIahohcZKV5kDdk9DQdqlaVi_EXCpAUHz740dPnzkpvn1SeViGHy-TRCaKrFTU66wDzeiCFO8aRDlRv29AdrOE-hMa6u1WJM6rL3eTZc6OFW9IfosFzePjvr96pQn1Qdt1PZ0RQmEGcXG/s400/suspectmessages.png" /></a></div>
That seemed to work. Now I needed to setup the shiny server so that user <code>john</code> could serve shiny apps from a <code>Shinyapps</code> folder in his home directory. When I last did this (version 0.4 or so) it required adding some detail to <code>/etc/shiny-server/shiny-server.conf</code>. So lets try and see if that still works.</p>
<pre><code>$ mkdir /etc/shiny-server
$ touch /etc/shiny-server/shiny-server.conf
$ nano /etc/shiny-server/shiny-server.conf</code></pre>
<p>I added the following to this file:</p>
<pre><code># Define the user we should use when spawning R Shiny processes
run_as shiny;
# Define a top-level server which will listen on a port
server {
# Instruct this server to listen on port 3838
#listen 3838 127.0.0.1;
listen 3838;
location /userApps {
user_apps;
directory_index on;
}
}</code></pre>
<p>It was a very simple configuration script that would allow users to serve shiny apps from a ShinyApps folder in their home directory. In theory all I needed to do now was to login to rstudio server and create such a setup. I did that and added the example <a href="https://github.com/rstudio/shiny/tree/master/inst/examples/01_hello"><code>01_hello</code></a> from the shiny package. I now navigate to <a href="http://128.199.255.233:3838/userApps/john/helloapp/">http://128.199.255.233:3838/userApps/john/helloapp/</a> and amazingly its all there.
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgFit_ifAQj6KEBv9QbzDzwigSGbYAH9f0YbluZyJAeRzSCnXslN1USy3DRvqMMiFdXQJnbFpRKyHm27xv6MvFGYQXarAtYF_oCpP5haE6sqRaDUUj4pVUJX-U_e7V8F2P3wAJUcCvOjzxo/s1600/shinysalive.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgFit_ifAQj6KEBv9QbzDzwigSGbYAH9f0YbluZyJAeRzSCnXslN1USy3DRvqMMiFdXQJnbFpRKyHm27xv6MvFGYQXarAtYF_oCpP5haE6sqRaDUUj4pVUJX-U_e7V8F2P3wAJUcCvOjzxo/s400/shinysalive.png" /></a></div>
</p>
<div id="bring-your-life-belt" class="section level2">
<h2>Bring your life belt</h2>
<p>Often one of the biggest problem people have with VPS and R involves RAM. When people use AWS EC2 micro instances or digital river $5 512mb droplets they experience the following:</p>
<p><a href="http://www.inside-r.org/questions/installing-r-packages-digitalocean-com-vps">www.inside-r.org</a></p>
<blockquote>
<p>For some reason, when I run install.packages() to install any package, it always just dumps the source package to the /tmp folder and does not actually install packages.</p>
</blockquote>
<p><a href="http://stackoverflow.com/questions/22553411/cannot-install-packages-from-within-rstudio-server#comment34325578_22553411">www.stackoverflow.com</a></p>
<blockquote>
<p>When trying to install certain (or all?) R packages, I always bump into the following type of error, which seems to me to be not specific to a particular package, but rather specific to for my R environment.</p>
</blockquote>
<p><a href="http://stackoverflow.com/questions/21837036/install-packages-r-on-ubuntu-12-04-downloads-but-does-not-install-packages#comment33053086_21837036">www.stackoverflow.com</a></p>
<blockquote>
<p>I installed the latest version of R for Ubuntu. I enter R, no issues at all, gives me the latest version, and I can load native packages. But When I try to install new packages, they download, but I get nothing.</p>
</blockquote>
<p>What is happening? Well they are running out of RAM and R is silently failing. Normally on your desktop this wouldnt likely be a problem as you would have swap. We can check for swap on our current server.</p>
<pre><code>$ swapon -s</code></pre>
<p>If the result returns empty then we need to assign some swap to this droplet. We can check the current storage state using df:</p>
<pre><code>$ df</code></pre>
<p>We are going to make a 512mb swap space</p>
<pre><code>$ dd if=/dev/zero of=/swapfile bs=1024 count=512k</code></pre>
<p>An explanation of the size is <a href="http://en.wikipedia.org/wiki/Dd_%28Unix%29#Block_size">here</a>. We then create a swap area and activate it.</p>
<pre><code>$ mkswap /swapfile
$ swapon /swapfile</code></pre>
<p>Now rerunning <code>swapon -s</code> we can see:
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhgXZPm3zD9FVWCVvOLZXxr7jv8ZM7SCTIp5wv_lRlMmX3DP5AAIw_NF2XAX6uudpO_Yo0veH2O7VHEXTfGfSUlE2BBmUSORQXEJe5qfd4cMw0vl2XPY0LvNJ_zd-vGiGq5XhtQftkEya8U/s1600/enableswap.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhgXZPm3zD9FVWCVvOLZXxr7jv8ZM7SCTIp5wv_lRlMmX3DP5AAIw_NF2XAX6uudpO_Yo0veH2O7VHEXTfGfSUlE2BBmUSORQXEJe5qfd4cMw0vl2XPY0LvNJ_zd-vGiGq5XhtQftkEya8U/s400/enableswap.png" /></a></div>
</p>
<p>This file will last on the virtual private server until the machine reboots. To ensure it returns on reboot we need to add an entry to <code>/etc/fstab</code></p>
<pre><code>$ nano /etc/fstab</code></pre>
<p>and add</p>
<pre><code>/swapfile none swap sw 0 0 </code></pre>
<p>The swap area has an attribute "swappiness" which we would like to set to 10</p>
<pre><code>$ echo 10 | sudo tee /proc/sys/vm/swappiness
$ echo vm.swappiness = 10 | sudo tee -a /etc/sysctl.conf</code></pre>
<p>An explantion of swappiness is found <a href="http://en.wikipedia.org/wiki/Swappiness">here</a>. Finally we set the permissions of the swap area so only root can access:</p>
<pre><code>$ chown root:root /swapfile
$ chmod 0600 /swapfile</code></pre>
<p>On the VNC that all looks like this:
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqi7qmw-vQODrHtGCxncwljgi8yNikPQ2LXxQd30eUwqOcKEF9OJf1TNZ94P0RL4HEZNDuKfhuWkIMznudtRDBZj6EJ3fJiTBLk3CBAeF9qmNwlv2dy13MWWjMMdMI1IXUVE4-xliIJD9y/s1600/swappermissions.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqi7qmw-vQODrHtGCxncwljgi8yNikPQ2LXxQd30eUwqOcKEF9OJf1TNZ94P0RL4HEZNDuKfhuWkIMznudtRDBZj6EJ3fJiTBLk3CBAeF9qmNwlv2dy13MWWjMMdMI1IXUVE4-xliIJD9y/s400/swappermissions.png" /></a></div>
</p>
</div>
</div>
<div id="conclusion" class="section level1">
<h1>Conclusion</h1>
<p><a href="https://www.digitalocean.com/?refcode=06ce3c289d3">Digital Ocean</a> has a very nice simple interface. Setting up a "droplet" is a breeze. The only tricky part with the installation process is using the command line and nano if you are not used to it. Installing RStudio Server and Shiny Server is now very straightforward. What did it cost? $10 for a month on the package I picked, 2 cups of hot chocolate and a kit-kat chunky.</p> <p>UPDATE: I added a section on including a swap file sourced from <a href="https://www.digitalocean.com/community/articles/how-to-add-swap-on-ubuntu-12-04">here</a>. This is slightly more involved but I have given some pointers to extra information.</p>
<div class="references">
</div>
</div>
</div>
<script>
$(document).ready(function () {
$('tr.header').parent('thead').parent('table').addClass('table table-condensed');
});
</script>
<!-- dynamically load mathjax for compatibility with --self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://c328740.ssl.cf1.rackcdn.com/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>
Anonymoushttp://www.blogger.com/profile/09290563285467003974noreply@blogger.com399tag:blogger.com,1999:blog-2583910510717912779.post-23760853047872794632014-03-19T00:11:00.002-07:002014-03-19T02:58:49.749-07:00Installing RSelenium on Win 8.1I was asked was it difficult to install and get RSelenium up and running on the windows platform.<br />
It isn't and I made a quick screenr to show you how.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<iframe frameborder="0" height="396" src="http://www.screenr.com/embed/MbUN" width="550"></iframe></div>
<br />
So in summary:<br />
<br />
<br />
<a name='more'></a><br />
<blockquote class="tr_bq">
install.packages('RSelenium')</blockquote>
<blockquote>
library(RSelenium)<br />
checkForServer()<br />
startServer()<br />
remDr <- remoteDriver()<br />
remDr$open()<br />
remDr$navigate("http://www.google.com/ncr")<br />
remDr$findElement(using = "name", value = "q")<br />
remDr$highlightElement()<br />
webElem$sendKeysToElement(list("R Cran"))<br />
webElem$clearElement()<br />
webElem$sendKeysToElement(list("R Cran", key = "enter"))<br />
remDr$close()<br />
remDr$closeServer()</blockquote>
Anonymoushttp://www.blogger.com/profile/09290563285467003974noreply@blogger.com29tag:blogger.com,1999:blog-2583910510717912779.post-80630891378452739632014-03-18T20:44:00.000-07:002014-03-21T04:08:22.171-07:00RSelenium: RSelenium basics<html>
<head>
<title>RSelenium basics</title>
<style type="text/css">
body, td {
font-family: sans-serif;
background-color: white;
font-size: 12px;
margin: 8px;
}
tt, code, pre {
font-family: 'DejaVu Sans Mono', 'Droid Sans Mono', 'Lucida Console', Consolas, Monaco, monospace;
}
h1 {
font-size:2.2em;
}
h2 {
font-size:1.8em;
}
h3 {
font-size:1.4em;
}
h4 {
font-size:1.0em;
}
h5 {
font-size:0.9em;
}
h6 {
font-size:0.8em;
}
a:visited {
color: rgb(50%, 0%, 50%);
}
pre {
margin-top: 0;
max-width: 95%;
border: 1px solid #ccc;
white-space: pre-wrap;
}
pre code {
display: block; padding: 0.5em;
}
code.r, code.cpp {
background-color: #F8F8F8;
}
table, td, th {
border: none;
}
blockquote {
color:#666666;
margin:0;
padding-left: 1em;
border-left: 0.5em #EEE solid;
}
hr {
height: 0px;
border-bottom: none;
border-top-width: thin;
border-top-style: dotted;
border-top-color: #999999;
}
@media print {
* {
background: transparent !important;
color: black !important;
filter:none !important;
-ms-filter: none !important;
}
body {
font-size:12pt;
max-width:100%;
}
a, a:visited {
text-decoration: underline;
}
hr {
visibility: hidden;
page-break-before: always;
}
pre, blockquote {
padding-right: 1em;
page-break-inside: avoid;
}
tr, img {
page-break-inside: avoid;
}
img {
max-width: 100% !important;
}
@page :left {
margin: 15mm 20mm 15mm 10mm;
}
@page :right {
margin: 15mm 10mm 15mm 20mm;
}
p, h2, h3 {
orphans: 3; widows: 3;
}
h2, h3 {
page-break-after: avoid;
}
}
</style>
</head>
<body>
<h1>
RSelenium basics</h1>
<h2>
Introduction</h2>
The goal of RSelenium is to make it easy to connect to a Selenium Server/ Remote Selenium Server from within R. RSelenium provides R bindings for the Selenium Webdriver API. <a href="http://docs.seleniumhq.org/">Selenium</a> is a project focused on automating web browsers. RSelenium allows you to carry out unit testing and regression testing on your webapps and webpages across a range of browser/OS combinations. This allows us to integrate from within R testing and manipulation of popular projects such as <a href="http://www.rstudio.com/shiny/">shiny</a>, <a href="https://saucelabs.com/">sauceLabs</a>.<br />
This vignette is divided into six main sections:<br />
<ul>
<li><a href="https://www.blogger.com/blogger.g?blogID=2583910510717912779#id1">Connecting to a Selenium Server.</a></li>
<li><a href="https://www.blogger.com/blogger.g?blogID=2583910510717912779#id2">Navigating using RSelenium.</a></li>
<li><a href="https://www.blogger.com/blogger.g?blogID=2583910510717912779#id3">Accessing elements in the DOM.</a></li>
<li><a href="https://www.blogger.com/blogger.g?blogID=2583910510717912779#id4">Sending events to elements.</a></li>
<li><a href="https://www.blogger.com/blogger.g?blogID=2583910510717912779#id5">Injecting JavaScript.</a></li>
<li><a href="https://www.blogger.com/blogger.g?blogID=2583910510717912779#id6">Frames and Windows.</a></li>
</ul>
Each section will be an introduction to a major idea in Selenium, and point to more detailed explanation in other vignettes.<br />
<a name='more'></a>
<h2>
<a href="https://www.blogger.com/null" id="id1">Connecting to a Selenium Server.</a></h2>
<h3>
What is a Selenium Server?</h3>
Selenium Server is a standalone java program which allows you to run HTML test suites in a range of different browsers, plus extra options like reporting.
You may, or may not, need to run a Selenium Server, depending on how you intend to use Selenium-WebDriver (RSelenium). <br />
<h3>
Do I need to run a Selenium Server?</h3>
If you intend to drive a browser on the same machine that RSelenium is running on then you will need to have Selenium Server running on that machine. <br />
<h3>
How do I get the Selenium Server stand-alone binary?</h3>
RSelenium has a built-in function that will download the stand-alone java binary and place it in the RSelenium package location in the <code>/bin/</code> directory. If you would like to install elsewhere the function takes a <code>dir</code> argument and can also update an existing binary. <br />
<pre><code>RSelenium::checkForServer()
</code></pre>
If you would like to download the binary manually it is currently found <a href="http://selenium-release.storage.googleapis.com/index.html">here</a>. Look for <code>selenium-server-standalone-x.xx.x.jar</code> in the latest directory.<br />
<h3>
How do I run the Selenium Server?</h3>
There is a utility function included in <code>RSelenium</code> to run an existing stand-alone Selenium Server binary. <br />
<pre><code>RSelenium::startServer()
</code></pre>
By default it looks in the <code>RSelenium</code> package <code>/bin/</code> directory. It has an optional <code>dir</code> argument if your binary is elsewhere. Alternatively you can run the binary manually. Open a console in your OS and navigate to where the binary is located and run:<br />
<pre><code>java -jar selenium-server-standalone-x.xx.x.jar
</code></pre>
By default the <code>Selenium Server</code> listens for connections on port 4444.<br />
<h3>
How do I connect to a running server?</h3>
<code>RSelenium</code> has a main reference class named <code>remoteDriver</code>. To connect to a server you need to instantiate a new <code>remoteDriver</code> with appropriate options.<br />
<pre><code># RSelenium::startServer() if required
require(RSelenium)
remDr <- remoteDriver(remoteServerAddr = "localhost"
, port = 4444
, browserName = "firefox"
)
</code></pre>
It would have been sufficient to call <code>remDr <- remoteDriver()</code> but the options where explicitly listed to show how one may connect to an arbitrary ip/port/browser etc. More detail maybe found on the <code>sauceLabs</code> vignette. To connect to the server use the <code>open</code> method.<br />
<pre><code>remDr$open()
</code></pre>
RSelenium should now have a connection to the Selenium Server. You can query the status of the remote Server using the <code>status</code> method.<br />
<pre><code>> remDr$getStatus()
$os
arch name version
"amd64" "Linux" "3.8.0-35-generic"
$java
version
"1.6.0_27"
$build
revision time version
"ff23eac" "2013-12-16 16:11:15" "2.39.0"
</code></pre>
<h1>
<a href="https://www.blogger.com/null" id="id2">Navigating using RSelenium.</a></h1>
<h3>
Basic Navigation</h3>
To start with we navigate to a url.<br />
<pre><code>remDr$navigate("http://www.google.com")
</code></pre>
Then we navigate to a second page.<br />
<pre><code>remDr$navigate("http://www.bbc.co.uk")
> remDr$getCurrentUrl()
[[1]]
[1] "http://www.bbc.co.uk/"
</code></pre>
We can go backwards and forwards using the methods <code>goBack</code> and <code>goForward</code>.<br />
<pre><code>remDr$goBack()
> remDr$getCurrentUrl()
[[1]]
[1] "https://www.google.com/"
remDr$goForward()
> remDr$getCurrentUrl()
[[1]]
[1] "http://www.bbc.co.uk/"
</code></pre>
To refresh the current page you can use the refresh method.<br />
<pre><code>remDr$refresh()
</code></pre>
<h2>
<a href="https://www.blogger.com/null" id="id3">Accessing elements in the DOM.</a></h2>
The DOM stands for the Document Object Model. It is a cross-platform and language-independent convention for representing and interacting with objects in HTML, XHTML and XML documents. Interacting with the DOM will be very important for us with Selenium and the webDriver provides a number of methods in which to do this.
A basic html page is <br />
<pre><code><!DOCTYPE html>
<html>
<body>
<h1>My First Heading</h1>
<p>My first paragraph.</p>
</body>
</html>
</code></pre>
The query box on the front page of <code>http://www.google.com</code> has html code <code>input id="gbqfq" class="gbqfif"</code> associated with it. The full html associated with the input tag is:<br />
<pre><code><input type="text" value="" autocomplete="off" name="q" class="gbqfif" id="gbqfq" style="border: medium none; padding: 0px; margin: 0px; height: auto; width: 100%; background: url(&quot;data:image/gif;base64,R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw%3D%3D&quot;) repeat scroll 0% 0% transparent; position: absolute; z-index: 6; left: 0px; outline: medium none;" dir="ltr" spellcheck="false">
</code></pre>
<h3>
Search by id.</h3>
To find this element in the DOM a number of methods can be used. We can search by the id.<br />
<pre><code>remDr$navigate("http://www.google.com/ncr")
webElem <- remDr$findElement(using = 'id', value = "gbqfq")
> webElem$getElementAttribute("id")
[[1]]
[1] "gbqfq"
> webElem$getElementAttribute("class")
[[1]]
[1] "gbqfif"
</code></pre>
<h3>
Search by class.</h3>
We can also search by class name.<br />
<pre><code>webElem <- remDr$findElement(using = 'class name', "gbqfif")
> webElem$getElementAttribute("class")
[[1]]
[1] "gbqfif"
> webElem$getElementAttribute("type")
[[1]]
[1] "text"
</code></pre>
<h3>
Search using css-selectors</h3>
The class is denoted by <code>.</code> when using css selectors. To search on class using css selectors we would use<br />
<pre><code>webElem <- remDr$findElement(using = 'css selector', "input.gbqfif")
</code></pre>
and to search on id using css-selectors<br />
<pre><code>webElem <- remDr$findElement(using = 'css selector', "input#gbqfq")
</code></pre>
A good example of searching using css-selectors is given <a href="http://saucelabs.com/resources/selenium/css-selectors">here</a>.<br />
<h3>
Search by name</h3>
To search using the <code>name</code> if given of the element. Note that ids are unique in a given html page. Names are not necessarily unique. <br />
<pre><code>webElem <- remDr$findElement(using = 'name', "q")
> webElem$getElementAttribute("name")
[[1]]
[1] "q"
> webElem$getElementAttribute("id")
[[1]]
[1] "gbqfq"
</code></pre>
<h3>
Search using xpath</h3>
The final option is to search using xpath. Normally one would use xpath by default when searching. <br />
Xpath using id.<br />
<pre><code>webElem <- remDr$findElement(using = 'xpath', "//*/input[@id = 'gbqfq']")
</code></pre>
Xpath using class.<br />
<pre><code>webElem <- remDr$findElement(using = 'xpath', "//*/input[@class = 'gbqfif']")
</code></pre>
<h2>
<a href="https://www.blogger.com/null" id="id4">Sending events to elements.</a></h2>
To illustrate how to interact with elements we will again use the <code>http://www.google.com/ncr</code> as an example.<br />
<h3>
Sending text to elements</h3>
Suppose we would like to search for <code>R cran</code> on google. We would need to find the element for the query box and send the appropriate text to it. We can do this using the <code>sendKeysToElement</code> method for the <code>webElement</code> class.<br />
<pre><code>remDr$navigate("http://www.google.com/ncr")
webElem <- remDr$findElement(using = "xpath", "//*/input[@id = 'gbqfq']")
webElem$sendKeysToElement(list("R Cran"))
</code></pre>
<h3>
Sending key presses to elements</h3>
We should see that the text <code>R Cran</code> has now been entered into the query box.
How do we press enter. We can simply send the enter key to query box. The enter key would be denoted as <code>"\uE007"</code>. So we could use:<br />
<pre><code>remDr$navigate("http://www.google.com/ncr")
webElem <- remDr$findElement(using = "xpath", "//*/input[@id = 'gbqfq']")
webElem$sendKeysToElement(list("R Cran", "\uE007"))
</code></pre>
It is not very easy to remember utf8 codes for appropriate keys so a mapping has been provided in <code>RSelenium</code>. ?selkeys' will bring up a help page explaining the mapping. The utf codes given <a href="http://code.google.com/p/selenium/wiki/JsonWireProtocol#/session/:sessionId/element/:id/value">here</a> have been mapped to easy to remember names. <br />
To use <code>selkeys</code> we would send the following<br />
<pre><code>remDr$navigate("http://www.google.com/ncr")
webElem <- remDr$findElement(using = "xpath", "//*/input[@id = 'gbqfq']")
webElem$sendKeysToElement(list("R Cran", key = "enter"))
</code></pre>
Typing <code>selKeys</code> into the console will bring up the list of mappings.<br />
<h3>
Sending mouse events to elements</h3>
For this example we will go back to the google frontpage and search for
<code>R Cran</code> then we will click the link for the <code>The Comprehensive R Archive Network</code>.<br />
<pre><code>remDr$navigate("http://www.google.com/ncr")
webElem <- remDr$findElement(using = "xpath", "//*/input[@id = 'gbqfq']")
webElem$sendKeysToElement(list("R Cran", key = "enter"))
</code></pre>
<code><li class="g"></code> contains the search results we can find all the search entries on the first page using the <code>findElements</code> method. The header for each link is contained further in with a <code><h3 class = "r"></code> tag. We will access the <code>h3</code> headers first. It will be succinct to find these elements using <code>css selectors</code>.<br />
<pre><code>webElems <- remDr$findElements(using = 'css selector', "li.g h3.r")
resHeaders <- unlist(lapply(webElems, function(x){x$getElementText()}))
> resHeaders
[1] "The Comprehensive R Archive Network"
[2] "Comprehensive R Archive"
[3] "Mirrors"
[4] "Contributed Packages"
[5] "R for Mac OS X"
[6] "Download R 3.0.2"
[7] "CRAN Repository Policy"
[8] "The R Project for Statistical Computing"
[9] "Cran - Wikipedia, the free encyclopedia"
[10] "Comprehensive R Archive Network (CRAN) - StatLib - Carnegie"
[11] "CRAN - Package R.methodsS3"
[12] "CRAN - Package R.cache"
[13] "Ubuntu – Details of package r-cran-sp in lucid"
</code></pre>
We can see that the first link is the one we want but in case googles search results change we refer to it as <br />
<pre><code>webElem <- webElems[[which(resHeaders == "The Comprehensive R Archive Network")]]
</code></pre>
How do we click the link. We can use the <code>clickElement</code> method:<br />
<pre><code>webElem$clickElement()
> remDr$getCurrentUrl()
[[1]]
[1] "http://cran.r-project.org/"
> remDr$getTitle()
[[1]]
[1] "The Comprehensive R Archive Network"
</code></pre>
<h2>
<a href="https://www.blogger.com/null" id="id5">Injecting JavaScript</a></h2>
Sometimes it is necessary to interact with the current url using JavaScript. This maybe necessary to call bespoke methods or to have more control over the page for example by adding the <code>JQuery</code> library to the page if it is missing. <code>Selenium</code> has two methods we can use to execute JavaScript namely
<code>executeScript</code> and <code>executeAsyncScript</code> from the <code>remoteDriver</code> class. We return to the google front page to investigate these methods.<br />
<h3>
Injecting JavaScript synchronously</h3>
Returning to the google homepage we can find the element for the <code>google</code> image. The image has <code>id = "hplogo"</code> and
we can use this in an xpath or search by id etc to select the element. In this case we use <code>css selectors</code>:<br />
<pre><code>remDr$navigate("http://www.google.com/ncr")
webElem <- remDr$findElement("css selector", "img#hplogo")
</code></pre>
Is the image visible? Clearly it is but we can check using javascript. <br />
<pre><code>> remDr$executeScript("return document.getElementById('hplogo').hidden;", args = list())
[[1]]
[1] FALSE
</code></pre>
Great so the image is not hidden indicated by the <code>FALSE</code>. We can hide it executing some simple JavaScript.<br />
<pre><code>remDr$executeScript("document.getElementById('hplogo').hidden = true;", args = list())
> remDr$executeScript("return document.getElementById('hplogo').hidden;", args = list())
[[1]]
[1] TRUE
</code></pre>
So now the image is hidden. We used an element here given by <code>id = "hplogo"</code>. We had to use the JavaScript function
<code>getElementById</code> to access it. It would be nicer if we could have used <code>webElem</code> which we had specified earlier.
If we pass a webElement object as an argument to either <code>executeScript</code> or <code>executeAsyncScript</code> <code>RSelenium</code> will pass it in an appropriate fashion.<br />
<pre><code>> remDr$executeScript(script = "return arguments[0].hidden = false;", args = list(webElem))
[[1]]
[1] FALSE
</code></pre>
Notive how we passed the web element to the method <code>executeScript</code>. The script argument defines the script to execute in the form of a function body. The value returned by that function will be returned to the client. The function will be invoked with the provided args. If the function returns an element then this will be returned as an object of class webElement:<br />
<pre><code>test <- remDr$executeScript("return document.getElementById('gbqfq');", args = list())
> test[[1]]
[1] "remoteDriver fields"
$remoteServerAddr
[1] "localhost"
$port
[1] 4444
$browserName
[1] "firefox"
$version
[1] ""
$platform
[1] "ANY"
$javascript
[1] TRUE
$autoClose
[1] FALSE
$nativeEvents
[1] TRUE
$extraCapabilities
list()
[1] "webElement fields"
$elementId
[1] 1
> class(test[[1]])
[1] "webElement"
attr(,"package")
[1] "RSelenium"
</code></pre>
<h3>
Injecting JavaScript asynchronously</h3>
I will briefly touch on asynch versus sync calls here. With the current firefox and selenium server combination (firefox 26.0 sel server 2.39.0) I had issues with async javascript calls when <code>nativeEvents = TRUE</code> (the default) was used. <br />
For the example below I switched to <code>nativeEvents = FALSE</code><br />
<pre><code>
remDr <- remoteDriver(nativeEvents = FALSE)
remDr$open()
remDr$navigate("http://www.google.com/ncr")
remDr$setAsyncScriptTimeout(10000)
</code></pre>
Observe:<br />
<pre><code>remDr$executeAsyncScript("setTimeout(function(){ alert('Hello'); arguments[arguments.length -1]('DONE');},5000); ", args = list())
</code></pre>
versus<br />
<pre><code>remDr$executeScript("setTimeout(function(){ alert('Hello');},5000); return 'DONE';", args = list())
</code></pre>
The async version waits until the callback (defined as the last argument) is called.<br />
<h2>
<a href="https://www.blogger.com/null" id="id6">Frames and Windows.</a></h2>
In the context of a web browser, a frame is a part of a web page or browser window which displays content independent of its container, with the ability to load content independently.<br />
<h3>
Frames in Selenium</h3>
We will demonstrate interacting with frames by way of example. The <a href="http://www.r-project.org/">R project</a> conviently contains frames so we shall use <code>RSelenium</code> to interact with it.
Assume we have a remoteDriver opened.<br />
<pre><code>remDr$navigate("http://www.r-project.org/")
> htmlParse(remDr$getPageSource()[[1]])
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>The R Project for Statistical Computing</title>
<link type="image/x-icon" href="favicon.ico" rel="icon">
<link type="image/x-icon" href="favicon.ico" rel="shortcut icon">
<link href="R.css" type="text/css" rel="stylesheet">
</head>
<frameset border="0" cols="1*, 4*">
<frameset rows="120, 1*">
<frame frameborder="0" name="logo" src="logo.html">
<frame frameborder="0" name="contents" src="navbar.html">
</frameset>
<frame frameborder="0" name="banner" src="main.shtml">
<noframes>
&lt;h1&gt;The R Project for Statistical Computing&lt;/h1&gt;
Your browser seems not to support frames,
here is the &lt;A href="navbar.html"&gt;contents page&lt;/A&gt; of the R Project's
website.
</noframes>
</frameset>
</html>
</code></pre>
We can see the content is contained in three frames and we dont appear to have access to the content within a frame. Put in the browser we see all the content:<br />
<pre><code>remDr$maxWindowSize()
remDr$screenshot(display = TRUE)
</code></pre>
<h6 align="center">
RProject front page</h6>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVT4UNsWBCMlMHDOPLrIdaFgkdsNpzt32wRbwLvqzQwUv0i9Qiwq-oIp5YK5knGBnB6eu6vvlO9BzJIyUjddqFhvTHZy1zeDsVS1Ql4Wlwqo4_N_VPvxEfo4s_lwmcD8RPHZgWmXSexMGa/s1600/RProject.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVT4UNsWBCMlMHDOPLrIdaFgkdsNpzt32wRbwLvqzQwUv0i9Qiwq-oIp5YK5knGBnB6eu6vvlO9BzJIyUjddqFhvTHZy1zeDsVS1Ql4Wlwqo4_N_VPvxEfo4s_lwmcD8RPHZgWmXSexMGa/s640/RProject.png" /></a></div>
To access the content we have to switch to a frame using the <code>switchToFrame</code> method of the <code>remoteDriver</code> class. <br />
<pre><code>webElems <- remDr$findElements(using = "tag name", "frame")
# webElems <- remDr$findElements("//frame") # using xpath
> sapply(webElems, function(x){x$getElementAttribute("src")})
[[1]]
[1] "http://www.r-project.org/logo.html"
[[2]]
[1] "http://www.r-project.org/navbar.html"
[[3]]
[1] "http://www.r-project.org/main.shtml"
remDr$switchToFrame(webElems[[2]])
> htmlParse(remDr$getPageSource()[[1]])
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>R Contents</title>
<link href="R.css" type="text/css" rel="stylesheet">
</head>
<body>
<em class="navigation">About R</em><br><a target="banner" href="about.html">What is R?</a><br><a target="banner" href="contributors.html">Contributors</a><br><a target="banner" href="screenshots/screenshots.html">Screenshots</a><br><a target="banner" href="news.html">What's new?</a><br><p>
<em class="navigation">Download, Packages</em><br><a target="banner" href="http://cran.r-project.org/mirrors.html">CRAN</a>
</p>
<p>
<em class="navigation">R Project</em><br><a target="banner" href="foundation/main.html">Foundation</a><br><a target="banner" href="foundation/memberlist.html">Members &amp; Donors</a><br><a target="banner" href="mail.html">Mailing Lists</a><br><a target="_top" href="http://bugs.R-project.org">Bug Tracking</a><br><a target="_top" href="http://developer.R-project.org">Developer Page</a><br><a target="banner" href="conferences.html">Conferences</a><br><a target="banner" href="search.html">Search</a><br></p>
<p>
<em class="navigation">Documentation</em><br><a target="banner" href="http://cran.r-project.org/manuals.html">Manuals</a><br><a target="banner" href="http://cran.r-project.org/faqs.html">FAQs</a><br><a target="_top" href="http://journal.r-project.org">The R Journal</a><br><!--
<a href="doc/Rnews/index.html" target="banner">Newsletter</a><br>
--><a target="_top" href="http://wiki.r-project.org">Wiki</a><br><a target="banner" href="doc/bib/R-books.html">Books</a><br><a target="banner" href="certification.html">Certification</a><br><a target="banner" href="other-docs.html">Other</a><br></p>
<p>
<em class="navigation">Misc</em><br><a target="_top" href="http://www.bioconductor.org">Bioconductor</a><br><a target="banner" href="other-projects.html">Related Projects</a><br><a target="_top" href="http://rwiki.sciviews.org/doku.php?id=rugs:r_user_groups">User Groups</a><br><a target="banner" href="links.html">Links</a><br></p>
</body>
</html>
</code></pre>
Now we see the source code of the navigation sidePanel. Notice how we used a webElement in the method <code>switchToFrame</code>. To further demonstrate we are now “in” this frame lets get all the <code>href</code> attributes:<br />
<pre><code>webElems <- remDr$findElements("css selector", "[href]")
sapply(webElems, function(x){x$getElementAttributes("href")})
> unlist(sapply(webElems, function(x){x$getElementAttribute("href")}))
[1] "http://www.r-project.org/R.css"
[2] "http://www.r-project.org/about.html"
[3] "http://www.r-project.org/contributors.html"
[4] "http://www.r-project.org/screenshots/screenshots.html"
[5] "http://www.r-project.org/news.html"
[6] "http://cran.r-project.org/mirrors.html"
[7] "http://www.r-project.org/foundation/main.html"
[8] "http://www.r-project.org/foundation/memberlist.html"
[9] "http://www.r-project.org/mail.html"
[10] "http://bugs.r-project.org/"
[11] "http://developer.r-project.org/"
[12] "http://www.r-project.org/conferences.html"
[13] "http://www.r-project.org/search.html"
[14] "http://cran.r-project.org/manuals.html"
[15] "http://cran.r-project.org/faqs.html"
[16] "http://journal.r-project.org/"
[17] "http://wiki.r-project.org/"
[18] "http://www.r-project.org/doc/bib/R-books.html"
[19] "http://www.r-project.org/certification.html"
[20] "http://www.r-project.org/other-docs.html"
[21] "http://www.bioconductor.org/"
[22] "http://www.r-project.org/other-projects.html"
[23] "http://rwiki.sciviews.org/doku.php?id=rugs:r_user_groups"
[24] "http://www.r-project.org/links.html"
</code></pre>
Notice if we pass a <code>NULL</code> value to the method <code>switchToFrame</code> we move back to the default view.<br />
<pre><code>remDr$switchToFrame(NULL)
> htmlParse(remDr$getPageSource()[[1]])
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>The R Project for Statistical Computing</title>
<link type="image/x-icon" href="favicon.ico" rel="icon">
<link type="image/x-icon" href="favicon.ico" rel="shortcut icon">
<link href="R.css" type="text/css" rel="stylesheet">
</head>
<frameset border="0" cols="1*, 4*">
<frameset rows="120, 1*">
<frame frameborder="0" name="logo" src="logo.html">
<frame frameborder="0" name="contents" src="navbar.html">
</frameset>
<frame frameborder="0" name="banner" src="main.shtml">
<noframes>
&lt;h1&gt;The R Project for Statistical Computing&lt;/h1&gt;
Your browser seems not to support frames,
here is the &lt;A href="navbar.html"&gt;contents page&lt;/A&gt; of the R Project's
website.
</noframes>
</frameset>
</html>
</code></pre>
Finally we can switch to the main panel using a name<br />
<pre><code>remDr$switchToFrame("banner")
> htmlParse(remDr$getPageSource()[[1]])
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>The R Project for Statistical Computing</title>
<link href="R.css" type="text/css" rel="stylesheet">
</head>
<body>
<h1 align="left">The R Project for Statistical Computing</h1>
<p>
</p>
<center>
<a href="misc/acpclust.R"><img border="0" alt="R Graphics Demo" src="hpgraphic.png"></a>
</center>
<p>
</p>
<table width="100%" border="1"><tbody>
<tr><td>
<h2>Getting Started:</h2>
<ul>
<li>R is a free software environment for statistical
computing and graphics. It compiles and runs on a wide variety
of UNIX platforms, Windows and MacOS. To <strong><a href="http://cran.r-project.org/mirrors.html">download R</a></strong>, please
choose your preferred <a href="http://cran.r-project.org/mirrors.html">CRAN mirror</a>.
</li>
<li>
If you have questions about R like how to download and install
the software, or what the license terms are,
please read our <a href="http://cran.R-project.org/faqs.html">answers to frequently asked questions</a> before you send an email.
</li>
<p>
</p>
</ul>
</td></tr>
<tr><td>
<h2>News:</h2>
<ul>
<li>
<strong>R version 3.0.2</strong> (Frisbee Sailing) has been
released on 2013-09-25.</li>
<li>
<strong><a href="http://www.r-project.org/useR-2013" target="_top">
useR! 2013</a></strong>,
took place at the University of Castilla-La Mancha,
Albacete, Spain, July 10-12 2013.
</li>
<li>
<a target="_top" href="http://journal.r-project.org/current.html">
<strong>The R Journal Vol.5/1</strong></a>
is available.
</li>
<li>
<strong>R version 2.15.3</strong> (Security Blanket) has been
released on 2013-03-01.</li>
<!-- Dead stuff, for later reuse:
<li>
<strong><a href="http://cran.R-project.org/src/base-prerelease">
R 3.0.2 (Frisbee Sailing) prerelease versions</a></strong> will
appear starting
September 15.
Final release is scheduled for September 25, 2013.<br>
Thanks to Erin Hodgess for the idea leading to the name.
</li>
<li>
<strong><a href="http://cran.R-project.org/src/base-prerelease">
R 3.0.1 prerelease versions</a></strong> will appear starting
May 6.
Final release is scheduled for May 16, 2013.<br>
</li>
<li>
The R Foundation as been awarded <a
href="soc11/index.html">
fifteen slots for R projects</a> in the <a
href="http://code.google.com/soc/" target="_top">Google Summer of Code 2011</a>.
<li>
<strong><a target="_top"
href="http://www.r-project.org/dsc-2009"> DSC 2009</a></strong>,
The 6th workshop on Directions in Statistical Computing, has been
held at the Center for Health and Society, University of
Copenhagen, Denmark, July 13-14, 2009. </li>
<li>
<strong><a target="_top" href="http://www.agrocampus-rennes.fr/math/useR-2009/">
useR! 2009</a></strong>, the R user conference,
has been be held at Agrocampus Rennes, France, July 8-10, 2009.
<li>
The R Foundation as been awarded <a
href="soc09/index.html">
four slots for R projects</a> in the <a
href="http://socghop.appspot.com/org/home/google/gsoc2009/rf" target="_top">Google Summer of Code 2009</a>.
<li>We have started to collect ideas for the <a
href="http://www.r-project.org/soc09">Google Summer of Code 2009</a>.
</li>
-->
</ul>
<p>
</p>
</td></tr>
</tbody></table>
<p>
This server is hosted by the <a target="_top" href="http://statmath.wu.ac.at">Institute for Statistics and Mathematics</a> of <a target="_top" href="http://www.wu.ac.at">WU (Wirtschaftsuniversität Wien)</a>.
</p>
</body>
</html>
</code></pre>
<h3>
Windows in Selenium</h3>
The easiest way to illustrate Windows in RSelenium is again by way of example. We will use the R project website.
First we select the <code>download R</code> element in the main frame. <br />
<pre><code>remDr$navigate("http://www.r-project.org")
remDr$switchToFrame("banner")
webElem <- remDr$findElement("partial link text", "download R")
> webElem$getElementText()
[[1]]
[1] "download R"
</code></pre>
We now send a selection of key presses to this element to open the link it points to in a new window. If you did it manually you would move the mouse to the element right click on the link press the down arrow key twice then press enter. We will do the same<br />
<pre><code>loc <- webElem$getElementLocation()
> loc[c('x','y')]
$x
[1] 347
$y
[1] 519
remDr$mouseMoveToLocation(webElement = webElem) # move mouse to the element we selected
remDr$click(2) # 2 indicates click the right mouse button
remDr$sendKeysToActiveElement(list(key = 'down_arrow', key = 'down_arrow', key = 'enter'))
</code></pre>
Notice now that a new windows has opened on the remote browser.<br />
<pre><code>> remDr$getCurrentWindowHandle()
[[1]]
[1] "{573d17e5-b95a-41b9-a65f-04092b6a804b}"
> remDr$getWindowHandles()
[[1]]
[1] "{573d17e5-b95a-41b9-a65f-04092b6a804b}" "{3336c9b3-4c46-4a95-853e-2786a529ba29}"
> remDr$getTitle()
[[1]]
[1] "The R Project for Statistical Computing"
>
> remDr$switchToWindow("{3336c9b3-4c46-4a95-853e-2786a529ba29}")
> remDr$getTitle()
[[1]]
[1] "CRAN - Mirrors"
</code></pre>
So using the code above one can observe how to switch between different windows on the remote browser. <br />
</body>
</html>Anonymoushttp://www.blogger.com/profile/09290563285467003974noreply@blogger.com31