ggplotly在HTML文档中无法显示。

huangapple go评论74阅读模式
英文:

ggplotly does not display within an html document

问题

I can help you with the translation of the non-code parts. Here's the translated text:

"I want to write an html_document where the reader can select different versions of a graph. I want to use ggplotly so the reader can hover over points and see their labels and so forth. The ggplotly works when run outside of the html_document but does nothing when run from within the document.

Here is the code (I added a # in front of ``` that are part of the RMD file, so it will look like one set of code, to run please remove.) The second option works but the first does not."

Please note that I've excluded the code sections from the translation as per your request.

英文:

I want to write an html_document where the reader can select different versions of a graph. I want to use ggplotly so the reader can hover over points and see their labels and so forth. The ggplotly works when run outside of the html_document but does nothing when run from within the document.

Here is the code (I added a # in front of ``` that are part of the RMD file, so it will look like one set of code, to run please remove.) The second option works but the first does not.

---
title: "Untitled"
author: "DWD"
date: "2023-05-18"
output: html_document
runtime: shiny
---

#```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
#```

This R Markdown document is made interactive using Shiny. Unlike the more traditional workflow of creating static reports, you can now create documents that allow your readers to change the assumptions underlying your analysis and see the results immediately. 

To learn more, see [Interactive Documents](http://rmarkdown.rstudio.com/authoring_shiny.html).

#```{r eruptions1, echo=FALSE}
library(ggplot2)
library(plotly)
inputPanel(
  selectInput("option2",
    label = "Which Option?",
    choices = c(1, 2), selected = 1
  ),
)

renderPlot({
  thetas <- seq(0, 1, length = 150) * 2 * pi
  decay <- 1:150
  if (input$option2 == 1) {
    dsn <- data.frame(xx = exp(-decay * .01) * sin(thetas), yy = exp(-decay * .01) * cos(thetas))
    plot0 <- ggplot(dsn, aes(x = xx, y = yy)) +
      suppressWarnings(geom_point(size = 1, aes(col = decay, text = decay)))
    g <- ggplotly(plot0)
    g
  } else {
    dsn <- data.frame(xx = exp(-decay * 0) * sin(thetas), yy = exp(-decay * 0) * cos(thetas))
    g <- ggplot(dsn, aes(x = xx, y = yy)) +
      suppressWarnings(geom_point(size = 1, aes(col = decay, text = decay)))
    g
  }
})
#```



答案1

得分: 2

这是你要翻译的内容:

"这是因为渲染 (gg)plotly 图形需要使用 renderPlotly,而不是 renderPlot。您可以按照以下方式操作:


title: "未命名"
author: "DWD"
date: "2023-05-18"
output: html_document
runtime: shiny

knitr::opts_chunk$set(echo = TRUE)

这个 R Markdown 文档使用 Shiny 使其变得交互式。与创建静态报告的传统工作流不同,您现在可以创建允许读者更改分析的假设并立即查看结果的文档。

要了解更多信息,请参阅交互文档

library(ggplot2)
library(plotly)
inputPanel(
  selectInput("option2",
    label = "选择哪个选项?",
    choices = c(1, 2), selected = 1
  ),
)

renderUI({
  thetas <- seq(0, 1, length = 150) * 2 * pi
  decay <- 1:150
  dsn <- data.frame(xx = exp(-decay * .01) * sin(thetas), yy = exp(-decay * .01) * cos(thetas))
  plot0 <- ggplot(dsn, aes(x = xx, y = yy)) +
      suppressWarnings(geom_point(size = 1, aes(col = decay, text = decay)))
  if(input$option2 == 1) {
    renderPlotly({ggplotly(plot0)})
  } else {
    renderPlot({plot0})
  }
})
英文:

That's because rendering a (gg)plotly graphic requires renderPlotly, not renderPlot. You can do as follows :

---
title: &quot;Untitled&quot;
author: &quot;DWD&quot;
date: &quot;2023-05-18&quot;
output: html_document
runtime: shiny
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

This R Markdown document is made interactive using Shiny. Unlike the more traditional workflow of creating static reports, you can now create documents that allow your readers to change the assumptions underlying your analysis and see the results immediately. 

To learn more, see [Interactive Documents](http://rmarkdown.rstudio.com/authoring_shiny.html).

```{r eruptions1, echo=FALSE}
library(ggplot2)
library(plotly)
inputPanel(
  selectInput(&quot;option2&quot;,
    label = &quot;Which Option?&quot;,
    choices = c(1, 2), selected = 1
  ),
)

renderUI({
  thetas &lt;- seq(0, 1, length = 150) * 2 * pi
  decay &lt;- 1:150
  dsn &lt;- data.frame(xx = exp(-decay * .01) * sin(thetas), yy = exp(-decay * .01) * cos(thetas))
  plot0 &lt;- ggplot(dsn, aes(x = xx, y = yy)) +
      suppressWarnings(geom_point(size = 1, aes(col = decay, text = decay)))
  if(input$option2 == 1) {
    renderPlotly({ggplotly(plot0)})
  } else {
    renderPlot({plot0})
  }
})
```

huangapple
  • 本文由 发表于 2023年5月22日 21:30:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76306721.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定