动态切换到 flexdashboard 中的下一个选项卡
Posted
技术标签:
【中文标题】动态切换到 flexdashboard 中的下一个选项卡【英文标题】:Dynamically switch to next tab in flexdashboard 【发布时间】:2022-01-14 09:54:37 【问题描述】:我有大约 20 个样本,我需要为这些样本绘制图形,例如直方图、箱线图等……我想将所有这些图组织在一个 flexdashboard 中,每个样本都有一个选项卡。所以每个选项卡都有一个直方图、一个箱线图等。
以下模板仅生成一个选项卡。我将数据集翻了一番并添加了一列,因此它有两个 type
、“first_sample”和“second_sample”(第一块代码)。
有没有一种简单的方法可以循环这些类型,以便在每个样本的单独选项卡上生成图?
谢谢!
编辑:我也找到了这篇文章,但我无法让它工作:Dynamicly increasing amount of tabs and pages in flexdashboards
---
title: "ggplotly geoms"
author: "Carson Sievert"
output:
flexdashboard::flex_dashboard:
orientation: rows
social: menu
source_code: embed
---
```r setup, include=FALSE
library(ggplot2)
library(plotly)
library(plyr)
library(flexdashboard)
# Make some noisily increasing data
set.seed(955)
dat1 <- data.frame(cond = rep(c("A", "B"), each=10),
xvar = 1:20 + rnorm(20,sd=3),
yvar = 1:20 + rnorm(20,sd=3))
dat1$type <- "first_sample"
dat2 <- data.frame(cond = rep(c("A", "B"), each=10),
xvar = 1:20 + rnorm(20,sd=3),
yvar = 1:20 + rnorm(20,sd=3))
dat2$type <- "second_sample"
dat <- rbind(dat1, dat2)
```
geom_point
=======================================================================
Row
-----------------------------------------------------------------------
### Scatter Chart with geom_point
```r
p <- ggplot(dat, aes(x=xvar, y=yvar)) +
geom_point(shape=1) # Use hollow circles
ggplotly(p)
```
### geom_smooth Linear Regression
```r
p <- ggplot(dat, aes(x=xvar, y=yvar)) +
geom_point(shape=1) + # Use hollow circles
geom_smooth(method=lm) # Add linear regression line
ggplotly(p)
```
Row
-----------------------------------------------------------------------
### geom_smooth with Loess Smoothed Fit
```r
p <- ggplot(dat, aes(x=xvar, y=yvar)) +
geom_point(shape=1) + # Use hollow circles
geom_smooth() # Add a loess smoothed fit curve with confidence region
ggplotly(p)
```
### Constraining Slope with stat_smooth
```r
n <- 20
x1 <- rnorm(n); x2 <- rnorm(n)
y1 <- 2 * x1 + rnorm(n)
y2 <- 3 * x2 + (2 + rnorm(n))
A <- as.factor(rep(c(1, 2), each = n))
df <- data.frame(x = c(x1, x2), y = c(y1, y2), A = A)
fm <- lm(y ~ x + A, data = df)
p <- ggplot(data = cbind(df, pred = predict(fm)), aes(x = x, y = y, color = A))
p <- p + geom_point() + geom_line(aes(y = pred))
ggplotly(p)
```
【问题讨论】:
【参考方案1】:要做到这一点,我必须结合起来(我引用了这篇文章的一些内容):
Use loop to generate section of text in rmarkdown
sprintf
通过数据的types
准备模板文本和名称选项卡
results = "asis"
,rmarkdown 块参数“防止 knitr 向输出添加格式”
cat
防止 R 添加额外的东西,如引号和元素编号”
print
在for
循环中绘图
以下代码为dat
中的每个样本生成一个带有两个选项卡和两个绘图的 flexdashboard
---
title: "test"
author: "Paul Endymion"
output:
flexdashboard::flex_dashboard:
orientation: rows
social: menu
source_code: embed
---
```r setup, include=FALSE
library(ggplot2)
library(flexdashboard)
library(data.table)
# Make some noisily increasing data
set.seed(955)
dat1 <- data.frame(cond = rep(c("A", "B"), each=10),
xvar = 1:20 + rnorm(20,sd=3),
yvar = 1:20 + rnorm(20,sd=3))
dat1$type <- "first_sample"
dat2 <- data.frame(cond = rep(c("A", "B"), each=10),
xvar = 1:20 + rnorm(20,sd=3),
yvar = 1:20 + rnorm(20,sd=3))
dat2$type <- "second_sample"
dat <- rbind(dat1, dat2)
setDT(dat)
```
```r echo = FALSE, results = "asis"
template <- "
%s
=======================================================================
### Scatter Chart with geom_point
" # dont't forget the newline
template2 <- "
Row
-----------------------------------------------------------------------
### geom_smooth Linear Regression
"
for (i in unique(dat$type))
cat(sprintf(template, i))
p<-ggplot(dat[type == i], aes(x=xvar, y=yvar)) +
geom_point(shape=1) # Use hollow circles
print(p)
cat(template2)
p2 <- ggplot(dat[type == i], aes(x=xvar, y=yvar)) +
geom_point(shape=1) + # Use hollow circles
geom_smooth(method=lm) # Add linear regression line
print(p2)
```
它仍然需要调整,但它做了我想做的事情。
【讨论】:
以上是关于动态切换到 flexdashboard 中的下一个选项卡的主要内容,如果未能解决你的问题,请参考以下文章
使用 shinyjs 和 flexdashbord 动态显示/隐藏输入
如何修复 flexdashboard 中的下载按钮侧边栏问题