1

I want to embed a table from external website, such as investing.com but I'm finding difficult embedding a specific div, in current example the exchange rate table has id = cr1. How can I only embed the table itself?

rm(list = ls())
library(shiny)

ui <- fluidPage(titlePanel("Getting A specific div from external website"), 
                mainPanel(fluidRow(                    
                  tags$iframe(seamless="seamless",src="http://www.investing.com/quotes/streaming-forex-rates-%E2%80%93-majors", height=600, width=1000)
                )
        )
)
server <- function(input, output) {}
shinyApp(ui, server)

I want table in Red only

enter image description here

Pork Chop
  • 28,528
  • 5
  • 63
  • 77

1 Answers1

0

I think the cross-origin policy block you from hiding parts of the iframe (at-least it seems to do it for me) but you can download the table and display it,

rm(list = ls())
library(shiny)

ui <- shinyUI(fluidPage(
  tags$head(
    tags$script(
      HTML(
        '
      $(document).ready(function(){
        // Should work if not blocked
        $("#myiframe").attr("src","http://www.investing.com/quotes/streaming-forex-rates-%E2%80%93-majors");

        $("#myiframe").load( function(){
          console.log("UPDATED");
          var iframe   = $("#myiframe").contents(),
            tbl = iframe.find("#cr1").clone();
          iframe.find("html").replaceWidth(tbl);
        });
      });
        '
      )
    )
  ),
  titlePanel("Getting A specific div from external website"), 
    mainPanel(fluidRow(   
      tags$iframe(id="myiframe",seamless="seamless",src="", height=600, width=1000),
      dataTableOutput("tbl1")
    )
  )
))

library(httr)
library(XML)
server <- shinyServer(function(input, output) {
  url <- "http://www.investing.com/quotes/streaming-forex-rates-%E2%80%93-majors"
  page.DOM <- content(GET(url))
  tables   <- readHTMLTable(page.DOM)
  exchng.tbl <- tables$cr1

  # cr1
  output$tbl1 <- renderDataTable({
    exchng.tbl
  })

})
shinyApp(ui, server)
RmIu
  • 4,357
  • 1
  • 21
  • 24
  • That still takes in the whole page – Pork Chop Nov 05 '15 at 14:09
  • There's at-least a table at the bottom of the page, when I run the Javascript I get an error. Check [this](http://stackoverflow.com/questions/6170925/get-dom-content-of-cross-domain-iframe) link. I don't think it's possible to edit out parts in the iframe unless you proxy it. – RmIu Nov 05 '15 at 14:18