在magrittr管道中使用闪亮的渲染函数,例如html_notebook内的renderTable renderPlot,带有闪亮的运行时

时间:2018-08-07 20:26:08

标签: r shiny tidyverse

是否可以在流水线shiny::render*的末尾使用%>%函数族?具体来说,当将html_notebookruntime: shiny一起使用时。

可以代替renderTable({mtcars[1:5, ]})来做:mtcars[1:5, ] %>% renderTable()吗?

示例

---
title: "R Notebook"
output: html_notebook
runtime: shiny
---

This is an [R Markdown](http://rmarkdown.rstudio.com) Notebook. When you execute code within the notebook, the results appear beneath the code. 

```{r echo=FALSE, warning=FALSE, message=FALSE}
library(tidyverse)

renderTable({
  mtcars[1:5, ]
})    
```

```{r echo=FALSE}
renderTable({
  mtcars %>%
    filter(cyl == 4) %>%
    group_by(am) %>%
    summarise(avg_gear = mean(gear),
              sd_carb = sd(carb))
})

# NOTE: This does not work!
mtcars %>%
  filter(cyl == 4) %>%
  group_by(am) %>%
  summarise(avg_gear = mean(gear),
            sd_carb = sd(carb)) %>%
  renderTable()
```

1 个答案:

答案 0 :(得分:0)

mtcars %>%
  filter(cyl == 4) %>%
  group_by(am) %>%
  summarise(avg_gear = mean(gear),
            sd_carb = sd(carb)) %>%
  renderTable(quoted = TRUE) # quoted defaults to FALSE