What is the process for utilizing R to extract data from a pie chart on a website that uses JavaScript?

Here's my code:

library(XML)

my_URL <- "http://www.velocitysharesetns.com/viix"

tables <- readHTMLTable(my_URL)

https://i.sstatic.net/Vadsw.png

The code above fetches only the table at the top of the webpage. The pie chart seems to be overlooked due to its Javascript nature. Any easy methods available to extract the two percentage values from the chart?

I've explored using RSelenium, but encountering some errors for which I haven't found solutions yet.

> RSelenium::startServer()
Error in if (file.exists(file) == FALSE) if (!missing(asText) && asText ==  : 
  argument is of length zero
In addition: Warning messages:
1: startServer is deprecated.
Users in future can find the function in file.path(find.package("RSelenium"), "example/serverUtils").
The sourcing/starting of a Selenium Server is a users responsiblity. 
Options include manually starting a server see vignette("RSelenium-basics", package = "RSelenium")
and running a docker container see  vignette("RSelenium-docker", package = "RSelenium") 
2: running command '"java" -jar "\\med-fs01/Home/Alex.Badoi/R/win-library/3.3/RSelenium/bin/selenium-server-standalone.jar" -log "\\med-fs01/Home/Alex.Badoi/R/win-library/3.3/RSelenium/bin/sellog.txt"' had status 127 
3: running command '"wmic" path win32_process get Caption,Processid,Commandline /format:htable' had status 44210 
> 

Following Phillip's advice, here is my solution:

library(XML)



# extarct HTML

doc.html = htmlTreeParse('http://www.velocitysharesetns.com/viix',
                         useInternal = TRUE)


# convert to text

htmltxt <- paste(capture.output(doc.html, file=NULL), collapse="\n")

# get location of string

pos = regexpr('CBOE SHORT-TERM VIX FUTURE', htmltxt)

# extarct from "pos" to nchar to end of string 

keep = substr(htmltxt, pos, pos+98)

Output:

> keep
[1] "CBOE SHORT-TERM VIX FUTURE DEC 2016', 81.64],\n\n    ['CBOE SHORT-TERM VIX FUTURE JAN 2017', 18.36],\n"

Answer №1

RSelenium Method

I found success with this approach using RSelenium on Windows 7 after inspecting the webpage source. I specifically utilized chromedriver.exe for this solution.

library(RSelenium)
checkForServer(update = TRUE)

#### Chromedriver Implementation
startServer(args = c("-Dwebdriver.chrome.driver=C:/Stuff/Scripts/chromedriver.exe")) 

remDr <- remoteDriver(remoteServerAddr = "localhost", browserName="chrome", port=4444)

### Launch Chrome
remDr$open()

remDr$navigate("http://www.velocitysharesetns.com/viix")

b <- remDr$findElements(using="class name", value="jqplot-pie-series")

sapply(b, function(x){x$getElementAttribute("outerHTML")})

The final command provides the following output:

[[1]]
[1] "<div class=\"jqplot-pie-series jqplot-data-label\" style=\"position: absolute; left: 100px; top: 106px;\"><div style=\"color:white;font-weight:bold;\">82%</div></div>"

[[2]]
[1] "<div class=\"jqplot-pie-series jqplot-data-label\" style=\"position: absolute; left: 159px; top: 67px;\"><div style=\"color:white;font-weight:bold;\">18%</div></div>"

These results clearly display the percentage numbers which can be easily extracted.

Direct HTML Retrieval

Further data extraction can be achieved by analyzing the raw html source, as the necessary information is already present within it. You will likely locate the relevant details within the script below:

<script type="text/javascript" language="javascript">
$(document).ready(function(){
var data = [


['CBOE SHORT-TERM VIX FUTURE DEC 2016', 81.64],

['CBOE SHORT-TERM VIX FUTURE JAN 2017', 18.36],

];

This script contains the desired data, with the figures rounded before being displayed graphically.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I apply conditional styles in React using Sass?

My SCSS file includes the following classes: .errorNotice { display: none; font-size: 12px; color: #D85B5F; background: white; padding: 5px; .error & { display: inline-block; } } Within my render function, {this.state && t ...

Is it appropriate to delete the comma in the Ghost Handlebars Template?

While navigating through the tags, I unexpectedly encountered a comma. This could potentially have an unwanted impact. I attempted to remove the comma, but is there a specific method to eliminate it completely? I referred to the Ghost blog Document for gui ...

Serve different files using Node.js socket.io webserver, not just index.html

I recently started delving into socket.io and decided to use this chat example as a reference. As I navigate to ip:8080/public/index.html, I realize the need to access other files, such as additional JS scripts that will be used on the client side in the ...

Experiencing difficulties with implementing both Bootstrap NAV TABS and Grid System on a single webpage

I'm experiencing difficulty getting both Bootstrap NAV TABS and the Grid System to work on the same page. When I try to include both, only one of them works depending on which Bootstrap reference is commented out. It seems like there may be an issue w ...

Customizing Colors in ggplot2: How to Specify Unique Colors for Two geom_point Layers

When working with a data frame as shown below, I am generating line plots using the ggplot function: df <- data.frame(a=seq(1.1,9.9,1.1), b=seq(0.1,0.9,0.1), c=rev(seq(10.1, 99.9, 11.1))) p1 <- ggplot(df, aes_string(x=names(df)[1] ...

Using ggplot2 to create smooth curves for histograms or densities

Is there a way to visually represent the distribution of variable x along a smooth (x,y) curve using violin plots or histograms? This method can be useful in displaying the marginal distribution of x when dealing with multiple groups (such as different cur ...

Click to solve the equation

I want to build a basic HTML page with three input variables (A,B,C). After the user inputs these values, they will click "calculate", and JavaScript will execute the following equations: ((A + B)/2) * 3 = X and (C * 2) = Y The results will be displaye ...

Customizing the directory for server files in Next.js

I am looking to organize all my backend related files in a folder called 'server' instead of the root directory. However, this is causing an issue with the frontend loading properly as it cannot locate the 'pages' directory. I vaguely r ...

ReactJs - Organize your data with the sort method

In my React application, I have a table that fetches its data from an API. I need to implement sorting for one of the columns. The values in this column are usually strings of numbers but sometimes can be equal to "-" (Dash). Below is the sort function I ...

Is there a way for me to initialize a PHP array using data from an AJAX call?

Trying to fetch data from an ajax request and potentially set a php array seems challenging as one operates on the client side while the other on the server side. Below is the code: HTML page: <?php foreach ($products as $product): ?> <li> ...

React's PrivateRouter component ensures secure routing for authorized users

I have a private router function component that I need to convert into a class component. My goal is to be able to access the Home component along with all its params using this route: <PrivateRouteComponent path="/home" component={Home} /> Below ...

Preserving measurements of variables in R

I am currently working on adapting complex code to suit my specific data requirements. The main issue I am facing seems to be related to variable dimensions. When starting with a two-dimensional matrix, some variables lose their original dimension and I n ...

Pattern matching algorithm designed to eliminate background-color attributes

Looking to strip out any "background-color:[whatever];" styles from the text within a div. The plan is to eliminate all inline background-color styles completely. I've been eyeing JavaScript's string.replace(regex,str) as a potential solution ...

Encountering a "Module not found" error while trying to run npm start in a Create React App

I'm attempting to initiate a React project using create-react-app. Encountered an error when running npm start: Failed to compile. multi ./node_modules/react-scripts/config/polyfills.js ./node_modules/react-dev-utils/webpackHotDevClient.js ./src/i ...

When collapsing an accordion, Bootstrap radio buttons fail to properly select

I have attempted various methods to achieve the desired functionality of having the accordion content close and open accordingly when checking a radio button, while also having the button visually appear as 'checked'. For a more in-depth example, ...

Streamlining the process of running a "for loop" in R across the entire dataset while also keeping track of counts

I'm currently working on an analysis in R, but I'm not very advanced in the language and I've hit a snag in my progress. I would greatly appreciate any assistance you can provide. Within the dataset (data1), I have 7 columns (a, b, c, d, e, ...

Node.js & Express: Bizarre file routes

It's quite strange how my local paths are functioning. Let me show you an example of my directory structure: public > css > bootstrap.css public > js > bootstrap.js templates > layout > page.ejs (default template for any page) tem ...

Knockout Mapping is causing a complete re-render of all elements

Utilizing the Knockout mapping plug-in to update the UI with JSON data fetched from the server every 3 seconds. The UI contains nested foreach bindings. However, it appears that all elements within the foreach bindings are completely erased and re-rendered ...

Using a Javascript while loop to iterate through an array and display the elements in the .innerHTML property

As a newcomer to Javascript, I am in the process of teaching myself. My current task is to develop a function that will display each element of the array AmericanCars with a space between them. I have successfully been able to show an individual element u ...

The size of the card image and content area will remain constant based on the specified percentage

Looking for some guidance as I am still learning. I need the card below to have a fixed width of 38% for the image area and 62% for the content area, regardless of the size of the content (the height can increase if there is more text). In the image above ...