动态切换到 flexdashboard 中的下一个选项卡
Dynamically switch to next tab in flexdashboard
我有大约 20 个样本需要绘制直方图、箱线图等图形...我想在 flexdashboard 中组织所有这些图,每个样本都有一个选项卡。所以每个选项卡都有一个直方图,一个箱线图等
下面的模板只生成一个选项卡。我将数据集加倍并添加一列,因此它有两个 type
、“first_sample”和“second_sample”(第一段代码)。
是否有一种简单的方法可以在这些类型上循环,以便为每个样本在单独的选项卡上生成图表?
谢谢!
编辑:我也找到了这个 post 但我无法让它工作:
---
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)
```
为了做到这一点,我不得不结合(我引用了其中的一些post):
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)
}
```
它仍然需要调整,但它完成了我想做的事情。
我有大约 20 个样本需要绘制直方图、箱线图等图形...我想在 flexdashboard 中组织所有这些图,每个样本都有一个选项卡。所以每个选项卡都有一个直方图,一个箱线图等
下面的模板只生成一个选项卡。我将数据集加倍并添加一列,因此它有两个 type
、“first_sample”和“second_sample”(第一段代码)。
是否有一种简单的方法可以在这些类型上循环,以便为每个样本在单独的选项卡上生成图表?
谢谢!
编辑:我也找到了这个 post 但我无法让它工作:
---
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)
```
为了做到这一点,我不得不结合(我引用了其中的一些post):
Use loop to generate section of text in rmarkdown
sprintf
根据数据 的results = "asis"
,rmarkdown 块参数“以防止 knitr 向输出添加格式”cat
以防止 R 添加引号和元素编号等额外内容print
在for
循环中绘制
types
准备模板文本和名称标签
以下代码为 dat
---
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)
}
```
它仍然需要调整,但它完成了我想做的事情。