如何将外部网站的特定div嵌入Rshiny Iframe

时间:2015-11-05 11:41:30

标签: iframe shiny

我想从外部网站嵌入一个表格,例如investing.com,但我发现难以嵌入特定的div,在当前示例中,汇率表有 id = cr1。我怎么才能自己嵌入桌子呢?

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)

我只想用红色表

enter image description here

1 个答案:

答案 0 :(得分:0)

我认为跨域策略会阻止你隐藏iframe的部分内容(至少它似乎是为我做的)但你可以下载表并显示它,

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)