R 在基于HTML模板的闪亮应用程序中使用Plotly失败

R 在基于HTML模板的闪亮应用程序中使用Plotly失败,r,shiny,plotly,shinyjs,r-plotly,R,Shiny,Plotly,Shinyjs,R Plotly,我正在基于HTML模板构建一个闪亮的应用程序,我想在图表中使用plotly。我正在努力将图表插入模板中 以下代码可以正常工作: library(shiny) library(plotly) shinyApp( ui <- fluidPage(plotlyOutput("plot1")), server <- function(input, output) { p <- plot_ly(mtcars, x = ~mpg, y = ~wt, type = '

我正在基于HTML模板构建一个闪亮的应用程序,我想在图表中使用plotly。我正在努力将图表插入模板中

以下代码可以正常工作:

library(shiny)
library(plotly)

shinyApp(

  ui <- fluidPage(plotlyOutput("plot1")),

  server <- function(input, output) {

    p <- plot_ly(mtcars, x = ~mpg, y = ~wt, type = 'scatter', mode = 'markers')

    output$plot1 <- renderPlotly({p})

  }
)
应用程序R

库(闪亮)
图书馆(绘本)
shinyApp(

ui尝试使用此模板:

<!doctype html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <script type="application/shiny-singletons"></script>
  <script type="application/html-dependencies">json2[2014.02.04];jquery[1.12.4];shiny[1.0.5];htmlwidgets[1.0];plotly-binding[4.7.1.9000];bootstrap[3.3.7]</script>
  <script src="shared/json2-min.js"></script>
  <script src="shared/jquery.min.js"></script>
  <link href="shared/shiny.css" rel="stylesheet" />
  <script src="shared/shiny.min.js"></script>
  <script src="htmlwidgets-1.0/htmlwidgets.js"></script>
  <script src="plotly-binding-4.7.1.9000/plotly.js"></script>
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <link href="shared/bootstrap/css/bootstrap.min.css" rel="stylesheet" />
  <script src="shared/bootstrap/js/bootstrap.min.js"></script>
  <script src="shared/bootstrap/shim/html5shiv.min.js"></script>
  <script src="shared/bootstrap/shim/respond.min.js"></script>
</head>

<body>

  <h1>HTML Template UI</h1>
  <div class="container-fluid">
  <div id="plot1" class="plotly html-widget html-widget-output" style="width: 100%; height: 300px; border: 1px solid red"></div>
  <div id="plot2" class="shiny-html-output" style="width: 100%; height: 300px; border: 1px solid blue"></div>
  </div>

</body>
</html>

json2[2014.02.04];jquery[1.12.4];Shining[1.0.5];htmlwidgets[1.0];plotly binding[4.7.1.9000];bootstrap[3.3.7]
HTML模板用户界面

有一种更优雅、更简单的方法来实现这一点。本研究对此进行了详细描述

要包含必要的HTML和JS依赖项,请在htmlTemplate的标题中包含
headContent()
。如果需要引导组件(actionButtons、tabsetPanel),还必须在标题中包含
bootstrapLib()
。确保这些命令位于双花括号内,如
{{bootstrapLib()}}

您还可以将生成输入和输出(如plotly)的代码放在这样的花括号中,这将自动创建html组件并包含JavaScript依赖项

这使得template.html更加干净

template.html

<!doctype html>
<html lang="en">
<head>

  {{ headContent() }}

</head>
<body>
  <h1>HTML Template UI</h1>
  <div class="container-fluid">

    {{plotlyOutput("plot1") }}
    {{uiOutput("plot2") }}

  </div>
</body>
</html>

{{headContent()}}
HTML模板用户界面
{{plotlyOutput(“plot1”)}
{{uiOutput(“plot2”)}
app.R

library(shiny)
library(plotly)

p <- plot_ly(mtcars, x = ~mpg, y = ~wt, type = 'scatter', mode = 'markers')

ui <- htmlTemplate("template.html")

server <- function(input, output) {
  output$plot1 <- renderPlotly({p})
  output$plot2 <- renderUI(HTML(paste(htmltools::tagList(list(p)))))
}
库(闪亮)
图书馆(绘本)

p看看这个:@MLavoie谢谢,我已经从这个例子开始了,但问题是他们没有在那里详细使用。谢谢你的回答。我还没有从你的标题(html小部件等)中获得这些依赖项,所以它在我这边还不起作用,但看起来很有希望。我现在正在安装:-)尽管了解一种更“自然”的方式将绘图嵌入模板中会很好,即在具有class=“shinny-x-output”的对象中。@mattino请运行上面的第一个R代码,单击浏览器中的右键,选择“可视化源代码”(或类似内容)并发布结果,好吗?谢谢。带有plot1和plot2 divs的整个html正文:我终于在我的机器上生成了它。正如您所暗示的,这是HTML模板中缺少链接的问题。解决方案是首先使用fluidPage()生成一个页面,然后将其标题复制到模板:-)谢谢你,SeGa,这听起来是一个非常好的提示,特别是我现在的模板中有2000多行。
<!doctype html>
<html lang="en">
<head>

  {{ headContent() }}

</head>
<body>
  <h1>HTML Template UI</h1>
  <div class="container-fluid">

    {{plotlyOutput("plot1") }}
    {{uiOutput("plot2") }}

  </div>
</body>
</html>
library(shiny)
library(plotly)

p <- plot_ly(mtcars, x = ~mpg, y = ~wt, type = 'scatter', mode = 'markers')

ui <- htmlTemplate("template.html")

server <- function(input, output) {
  output$plot1 <- renderPlotly({p})
  output$plot2 <- renderUI(HTML(paste(htmltools::tagList(list(p)))))
}