情节html嵌入闪亮

问题描述 投票:0回答:1

我使用plotly生成了几个图并将它们保存为离线html(我不想生成它们,因为它需要很长时间才能在后台生成它们)。以下是从plotly网站拍摄的两个地块,我将它们保存为html。

#Graph 1
Animals <- c("giraffes", "orangutans", "monkeys")
SF_Zoo <- c(20, 14, 23)
LA_Zoo <- c(12, 18, 29)
data <- data.frame(Animals, SF_Zoo, LA_Zoo)

p <- plot_ly(data, x = ~Animals, y = ~SF_Zoo, type = 'bar', name = 'SF Zoo') %>%
  add_trace(y = ~LA_Zoo, name = 'LA Zoo') %>%
  layout(yaxis = list(title = 'Count'), barmode = 'group')

htmlwidgets::saveWidget(p, file="zoo.html")


#Graph 2
x <- c('Product A', 'Product B', 'Product C')
y <- c(20, 14, 23)
text <- c('27% market share', '24% market share', '19% market share')
data <- data.frame(x, y, text)

p <- plot_ly(data, x = ~x, y = ~y, type = 'bar', text = text,
             marker = list(color = 'rgb(158,202,225)',
                           line = list(color = 'rgb(8,48,107)',
                                       width = 1.5))) %>%
  layout(title = "January 2013 Sales Report",
         xaxis = list(title = ""),
         yaxis = list(title = ""))
htmlwidgets::saveWidget(p, file="product.html")

我写了一些shiny代码,可以显示Rmarkdown的html输出,但不是我从上面的plotly生成的html。请注意,selectInput()中的第一个选项(示例)是我从默认的Rmarkdown html生成的,并且有效。我还生成了多个rmarkdown html,我也可以在shiny应用程序中的htmls之间切换,但不能在plotly html之间切换。

ui= fluidPage(
  titlePanel("opening web pages"),
  sidebarPanel(
    selectInput(inputId='test',label=1,choices=c("sample","zoo","product"))
  ),
  mainPanel(
    htmlOutput("inc")
  )
)
server = function(input, output) {
  getPage<-function() {
    return(includeHTML(paste0("file:///C:/Users/home/Documents/",input$test,".html")))
  }
  output$inc<-renderUI({getPage()})
}
shinyApp(ui, server)
html r shiny plotly
1个回答
0
投票

您可以使用iframe - 还可以查看addResourcePath

ui = fluidPage(
  titlePanel("opening web pages"),
  sidebarPanel(selectInput(
    inputId = 'test',
    label = 1,
    choices = c("sample", "zoo", "product")
  )),
  mainPanel(htmlOutput("inc"))
)

server = function(input, output) {
  myhtmlfilepath <- getwd() # change to your path
  addResourcePath('myhtmlfiles', myhtmlfilepath)

  getPage <- function() {
    return(tags$iframe(src = paste0("myhtmlfiles/", input$test, ".html"), height = "100%", width = "100%", scrolling = "yes"))
  }

  output$inc <- renderUI({
    req(input$test)
    getPage()
  })
}

shinyApp(ui, server)
© www.soinside.com 2019 - 2024. All rights reserved.