运行R Shiny文档时,无法在命名块中显示ggplot

时间:2016-10-05 17:04:18

标签: r ggplot2 shiny

我发现当运行一个闪亮的文档时,如果我们将其命名为块,则无法显示常规的“不闪亮”的ggplot (参见下面的代码)。我当然可以保留所有“不闪亮”的ggplot块未命名,但如果闪亮的文档很长并且包含大量“不闪亮的”ggplots,那么代码会很烦人。

因此,我想知道是否有办法让我们能够命名所有的块,无论它是闪亮的还是“无闪亮的”图形。

谢谢!

---
title: ""
author: "" 
date: ""
runtime: shiny
output: 
  html_document:
    fig_height: 3
    fig_width: 6
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(collapse=TRUE, prompt=TRUE, eval=TRUE, message=F,include=T,comment=NULL, echo=TRUE)
```

```{r problematic chunk}
#HAS NAME FOR THIS CHUNK! CANNOT DISPLAY

library(ggplot2)
library(dplyr)
library(babynames)
head(babynames)

#Let's do male David (and alternative spelling: Dave, Davis, Davi) line plot
babynames %>% filter(name %in% c("David","Dave","Davis","Davi"),sex=="M") %>%
  ggplot(aes(x=year)) + geom_line(aes(y=n, color=name)) +
  xlab("Year") + ylab("Count") + ggtitle("Distribution of these four male 'David-like' names over years")
```

```{r}
#NO NAME FOR THIS CHUNK! NO Problem displaying

library(ggplot2)
library(dplyr)
library(babynames)
head(babynames)

#Let's do male David (and alternative spelling: Dave, Davis, Davi) line plot
babynames %>% filter(name %in% c("David","Dave","Davis","Davi"),sex=="M") %>%
  ggplot(aes(x=year)) + geom_line(aes(y=n, color=name)) +
  xlab("Year") + ylab("Count") + ggtitle("Distribution of these four male 'David-like' names over years")
```

```{r Shiny No problem}
library(mosaicData)
head(Galton)

inputPanel(
  checkboxInput("color_or_not",label="Color by sex?",value = FALSE, width = NULL)
)

renderPlot({
  if(input$color_or_not)
  {
    ggplot(Galton,aes(x=father,y=height,color=sex)) + geom_point() + 
      xlab("Father's height") + ylab("Person's height") + ggtitle("Person's height against father's height conditioned on sex")}
  else{
      ggplot(Galton,aes(x=father,y=height)) + geom_point() + 
      xlab("Father's height") + ylab("Person's height") + ggtitle("Person's height against father's height")
  }
})
```

0 个答案:

没有答案