For loop 如何使用动态创建的选项卡和for循环显示ggplotly打印?
我有一个R标记文档,我想动态创建带有ggplotly图形的选项卡For loop 如何使用动态创建的选项卡和for循环显示ggplotly打印?,for-loop,r-markdown,plotly,For Loop,R Markdown,Plotly,我有一个R标记文档,我想动态创建带有ggplotly图形的选项卡 --- output: html_document --- ```{r setup, include=FALSE} knitr::opts_chunk$set(echo = TRUE) ``` ```{r} library(ggplot2) library(plotly) ``` ```{r} fig=ggplot(cars)+geom_point(aes(speed, dist)) ``` # level 1 ## le
---
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(ggplot2)
library(plotly)
```
```{r}
fig=ggplot(cars)+geom_point(aes(speed, dist))
```
# level 1
## level 2{.tabset .tabset-pills}
```{r echo=FALSE, results='asis'}
for (h in 1:3){
cat("###", h,'{-}', '\n\n')
ggplotly(fig)
cat( '\n\n')
}
```
我知道它不同于普通的ggplot
图形,我在这里查看了解决方案:但它不适用于我,下面可以这样实现:
编辑:在此之后,我添加了两个函数,将fig.width
和fig.height
传递给ggplotly
编辑2:添加了代码,以便另外使用plotly::subplot
s
---
title: test
date: "20 5 2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(ggplot2)
library(plotly)
```
```{r, echo=FALSE}
# Get the current figure size in pixels:
get_w <- function() {
with(knitr::opts_current$get(c("fig.width", "dpi", "fig.retina")),
fig.width*dpi/fig.retina)
}
get_h <- function() {
with(knitr::opts_current$get(c("fig.height", "dpi", "fig.retina")),
fig.height*dpi/fig.retina)
}
```
```{r}
fig <- ggplot(cars) +
geom_point(aes(speed, dist))
```
# level 1
## level 2 {.tabset .tabset-pills}
```{r, include=FALSE}
htmltools::tagList(ggplotly(fig))
```
```{r echo=FALSE, results='asis', fig.width=4, fig.height=4}
fig <- ggplotly(fig, width = get_w(), height = get_h())
for (h in 1:3) {
cat("###", h, '{-}', '\n\n')
print(htmltools::tagList(plotly::subplot(fig, fig, nrows=2, heights = c(0.1, 0.9))))
cat( '\n\n')
}
```
---
标题:测试
日期:“2020年5月20日”
输出:html\u文档
---
```{r设置,include=FALSE}
knitr::opts_chunk$set(echo=TRUE)
```
```{r}
图书馆(GG2)
图书馆(绘本)
```
```{r,echo=FALSE}
#获取当前图形大小(以像素为单位):
get_w也从中找到了解决方案,这不需要HTML,只需要降价即可
---
date: "20 5 2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(ggplot2)
library(plotly)
```
```{r}
fig <- ggplot(cars) +
geom_point(aes(speed, dist))
```
## Results {.tabset}
### 1
We show a scatter plot in this section.
```{r}
ggplotly(fig)
```
### 2
We show the data in this tab.
```{r}
ggplotly(fig)
```
---
日期:“2020年5月20日”
输出:html\u文档
---
```{r设置,include=FALSE}
knitr::opts_chunk$set(echo=TRUE)
```
```{r}
图书馆(GG2)
图书馆(绘本)
```
```{r}
这个答案与我的问题无关。手动添加选项卡不是问题。我问的是动态生成它们。在那种情况下ggplotly不行Stephan,非常感谢!有没有办法设置图形的大小以使其更大?我试着在:``{r echo=FALSE,results='asis'}中添加fig.width和height,但不起作用。我不想在全局范围内这样做,因为每个plotHi@yuliaUU有不同的图形大小。我刚刚编辑了我的帖子。我借用了使用fig.width和fig.height调整大小的解决方案,因为有这么多问题,我试图提供一个可复制的示例,但现在当我尝试将其调整到我自己的代码时,它无法正常工作。我真的在策划:ff Hi@yuliaUU。你必须反过来做。首先,我很激动。第二个plotly::子地块。并将plotly::子地块放在HtmlTolls::标记列表中,而不是ggplotly。我相应地编辑了我的代码。我想知道您是否可以在这里帮助我解决类似的问题:。我试着做与你在这里提供的相同的事情,但它不起作用