Why does runtime shiny in R markdown ot show ggplot stat_summary geom with text correctly?

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

Why does runtime shiny in R markdown ot show ggplot stat_summary geom with text correctly?

问题

我正在为您翻译以下内容:

在一个简单的Rmarkdown HTML文档中绘制的图形结果与在相应的RShiny版本中绘制的图形结果不同,即通过在yaml头部添加runtime: shiny来实现。

下面是每个版本的代码(但唯一的区别是yaml头部的一行)和生成的图像。

运行为Rmarkdown文档时的结果:

---
title: "test"
date: "2023-08-08"
output:
  html_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning = FALSE)
library(readr);library(dplyr);library(magrittr); library(ggplot2);
library(tibble);

set.seed(1)
data_df <- bind_rows(tibble(type = "type A", y = rnorm(150, 10, 1)),
                    tibble(type = "type B", y = rnorm(100, 5, 1)))
data_df$type <- factor(data_df$type)
n_mean_fun <- function(x) {
  return(data.frame(y = mean(x),
                    label = paste0("mean = ", round(mean(x),2), "\n",
                                   "n = ", length(x))
                    ))
}
ggplot(data_df, 
       aes(x = type, y = y)) + 
  geom_point() +
stat_summary(fun.data = "n_mean_fun", geom = "text", 
             color = "black")

运行为Rmarkdown HTML输出时的图形:
![Plot when running as Rmarkdown html output](https://i.stack.imgur.com/vXEFc.png)

### 运行为Rshiny文档时的结果:


title: "test"
author: "Ronan Brady"
date: "2023-08-08"
output:
html_document: default
runtime: shiny

knitr::opts_chunk$set(echo = TRUE, warning = FALSE)
library(readr);library(dplyr);library(magrittr); library(ggplot2);
library(tibble);

set.seed(1)
data_df <- bind_rows(tibble(type = "type A", y = rnorm(150, 10, 1)),
                    tibble(type = "type B", y = rnorm(100, 5, 1)))
data_df$type <- factor(data_df$type)
n_mean_fun <- function(x) {
  return(data.frame(y = mean(x),
                    label = paste0("mean = ", round(mean(x),2), "\n",
                                   "n = ", length(x))
                    ))
}
ggplot(data_df, 
       aes(x = type, y = y)) + 
  geom_point() +
stat_summary(fun.data = "n_mean_fun", geom = "text", 
             color = "black")

运行为Rshiny文档时的图形:
![Plot when runing as rshiny doc](https://i.stack.imgur.com/rj63S.png)

有人能帮忙解释为什么会出现这种情况,以及如何使shiny版本与文档版本保持一致,如预期的那样吗?

谢谢,R

<details>
<summary>英文:</summary>

I&#39;m getting different plot results between a plot in an simple Rmarkdown HTML doc and the same plot produced in an RShiny equivalent, i.e. by adding adding **runtime: shiny** to the yaml header.

Below is the code for each version (but the only difference is one line in the yaml header) and the resulting image

### Result when running as Rmarkdown doc:


title: "test"
date: "2023-08-08"
output:
html_document: default

knitr::opts_chunk$set(echo = TRUE, warning = FALSE)
library(readr);library(dplyr);library(magrittr); library(ggplot2);
library(tibble);

set.seed(1)
data_df &lt;- bind_rows(tibble(type = &quot;type A&quot;, y = rnorm(150, 10, 1)),
                    tibble(type = &quot;type B&quot;, y = rnorm(100, 5, 1)))
data_df$type &lt;- factor(data_df$type)
n_mean_fun &lt;- function(x) {
  return(data.frame(y = mean(x),
                    label = paste0(&quot;mean = &quot;, round(mean(x),2), &quot;\n&quot;,
                                   &quot;n = &quot;, length(x))
                    ))
}
ggplot(data_df, 
       aes(x = type, y = y)) + 
  geom_point() +
stat_summary(fun.data = &quot;n_mean_fun&quot;, geom = &quot;text&quot;, 
             color = &quot;black&quot;)

Plot when running as Rmarkdown html output:
![Plot when running as Rmarkdown html output](https://i.stack.imgur.com/vXEFc.png)

### Result when running as Rshiny doc:




title: "test"
author: "Ronan Brady"
date: "2023-08-08"
output:
html_document: default
runtime: shiny

knitr::opts_chunk$set(echo = TRUE, warning = FALSE)
library(readr);library(dplyr);library(magrittr); library(ggplot2);
library(tibble);

set.seed(1)
data_df &lt;- bind_rows(tibble(type = &quot;type A&quot;, y = rnorm(150, 10, 1)),
                    tibble(type = &quot;type B&quot;, y = rnorm(100, 5, 1)))
data_df$type &lt;- factor(data_df$type)
n_mean_fun &lt;- function(x) {
  return(data.frame(y = mean(x),
                    label = paste0(&quot;mean = &quot;, round(mean(x),2), &quot;\n&quot;,
                                   &quot;n = &quot;, length(x))
                    ))
}
ggplot(data_df, 
       aes(x = type, y = y)) + 
  geom_point() +
stat_summary(fun.data = &quot;n_mean_fun&quot;, geom = &quot;text&quot;, 
             color = &quot;black&quot;)

Plot when runing as rshiny doc:
![Plot when runing as rshiny doc](https://i.stack.imgur.com/rj63S.png)


Can anyone help explain why this might occur and how the shiny version can be made to be consisent with the doc version as would be expected?

Thanks, R


</details>


# 答案1
**得分**: 0

`stat_summary()`函数中的`fun.data`参数不应该加引号。

以下是正确的代码:

```
---
title: "test"
author: "Ronan Brady"
date: "2023-08-08"
output:
  html_document: default
runtime: shiny 
---

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

```{r}
library(reader)
library(dplyr)
library(magrittr)
library(ggplot2)
library(tibble)
```

```{r}

set.seed(1)
data_df <- bind_rows(tibble(type = "type A", y = rnorm(150, 10, 1)),
                    tibble(type = "type B", y = rnorm(100, 5, 1)))
data_df$type <- factor(data_df$type)
n_mean_fun <- function(x) {
  return(data.frame(y = mean(x),
                    label = paste0("mean = ", round(mean(x),2), "\n",
                                   "n = ", length(x))
                    ))
}
ggplot(data_df, 
       aes(x = type, y = y)) + 
  geom_point() +
stat_summary(fun.data = n_mean_fun, geom = "text", 
             color = "black")

```
```

我还将`library()`调用移到了一个单独的代码块中。在设置代码块中加载库不是一个好的做法。在RStudio中处理Rmd文件时,每当运行其他代码块时,设置代码块都会被执行。因此,设置代码块应该只包括`knitr::opts_chunk$set(...)`部分。

<details>
<summary>英文:</summary>

The `fun.data` argument in `stat_summary()` should not be in quotes. 

Here is the correct code:


title: "test"
author: "Ronan Brady"
date: "2023-08-08"
output:
html_document: default
runtime: shiny

knitr::opts_chunk$set(echo = TRUE, warning = FALSE)
library(reader)
library(dplyr)
library(magrittr)
library(ggplot2)
library(tibble)

set.seed(1)
data_df &lt;- bind_rows(tibble(type = &quot;type A&quot;, y = rnorm(150, 10, 1)),
                    tibble(type = &quot;type B&quot;, y = rnorm(100, 5, 1)))
data_df$type &lt;- factor(data_df$type)
n_mean_fun &lt;- function(x) {
  return(data.frame(y = mean(x),
                    label = paste0(&quot;mean = &quot;, round(mean(x),2), &quot;\n&quot;,
                                   &quot;n = &quot;, length(x))
                    ))
}
ggplot(data_df, 
       aes(x = type, y = y)) + 
  geom_point() +
stat_summary(fun.data = n_mean_fun, geom = &quot;text&quot;, 
             color = &quot;black&quot;)


I also moved the `library()` calls into a chunk of their own. It is not good practice to load libraries in the setup chunk. When working on an Rmd file in RStudio, the setup chunk is executed whenever you run any other chunk. Therefore the setup chunk should only include the `knitr::opts_chunk$set(...)` part.

</details>



huangapple
  • 本文由 发表于 2023年8月8日 23:18:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76860959.html
匿名

发表评论

匿名网友

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

确定