Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在RMarkdown文档的循环中绘制sunburst图?_R_R Markdown_Sunburst Diagram - Fatal编程技术网

如何在RMarkdown文档的循环中绘制sunburst图?

如何在RMarkdown文档的循环中绘制sunburst图?,r,r-markdown,sunburst-diagram,R,R Markdown,Sunburst Diagram,我的目标是创建一个HTMLR降价报告,我想在其中合并多个sunburst图,每个我感兴趣的实体一个。使用sunburstR绘制太阳余辉。每个图表都应位于其各自的选项卡中 问题是我不知道如何在选项卡中实际显示图形。我尝试将图形指定给一个变量,并打印或绘图对其进行处理,但没有效果。我也尝试过跳过作业,对对象进行调用,但也没有成功。由于sunburstR对象也是一个htmlwidget,我也在手册中查找了该软件包,但即使在那里,我也找不到帮助。问题似乎是我在循环中执行此操作,为了实现我的目标,我需要在

我的目标是创建一个HTMLR降价报告,我想在其中合并多个sunburst图,每个我感兴趣的实体一个。使用
sunburstR
绘制太阳余辉。每个图表都应位于其各自的选项卡中

问题是我不知道如何在选项卡中实际显示图形。我尝试将图形指定给一个变量,并
打印
绘图
对其进行处理,但没有效果。我也尝试过跳过作业,对对象进行调用,但也没有成功。由于sunburstR对象也是一个
htmlwidget
,我也在手册中查找了该软件包,但即使在那里,我也找不到帮助。问题似乎是我在循环中执行此操作,为了实现我的目标,我需要在标记为
results='asis'
的单元格中运行代码,这会妨碍图形的显示

下面的问题有点简单,但完美地展示了我正在努力解决的问题(示例是:)的修改版本。一段自我维持的降价代码,用于重现问题(请注意,下面的所有内容都是R降价文件(.Rmd扩展名),应该这样运行):


---
标题:“使用sunburst图进行降价-示例”
作者:“用户”
日期:“`r粘贴('date:',Sys.date())`”
输出:html\u文档
---
```{r加载包,include=FALSE}
图书馆(sunburstR)
图书馆(dplyr)
图书馆(knitr)
```
```{r数据准备}
df%变异(路径=粘贴(季度、月份、九月=“-”)
打印(可打印(头(df_年,5),格式=“降价”))
sunburst(data.frame(xtabs(count~path,df#year))#在这里如何显示图形?
}
```


我还包含了一个
kable
对象作为参考,因为它实际上在浏览器中打开的文档中是可见的(sunburst graph不是)。你对如何让它工作有什么建议吗?

这段代码有点混乱,没有经过优化,但应该提供一个解决方案。通常,在循环中创建
htmlwidget
的解决方案是
tagList(lapply(…create htmlwidget…
),如中所述。但是,由于我们试图将每个图表包装在选项卡中,解决方案会变得混乱。我们需要手动创建选项卡,然后单独创建作为选项卡内容的图表

---
title: "Markdown with sunburst graphs - example"
author: "User"
date: "`r paste('Date: ', Sys.Date())`"
output: 
  html_document: 
    keep_md: yes
    self_contained: no
---

```{r load-packages, include=FALSE}
library(sunburstR)
library(dplyr)
library(knitr)
library(htmltools)
```

```{r data-preparation}
df <- data.frame(
  date = seq.Date(
    as.Date('2014-01-01'),
    as.Date('2016-12-31'),
    by = "days"
  ),
  stringsAsFactors = FALSE
)

df$year = format(df$date, "%Y")
df$quarter = paste0("Q", ceiling(as.numeric(format(df$date,"%m"))/3))
df$month = format(df$date, "%b")
df$count = rep(1, nrow(df))
```

# Graphs per year

```{r tabs-per-year, results='asis', warning=FALSE, echo=FALSE}
htmltools::tags$ul(
  class = "nav nav-pills",
  role = "tablist",
  lapply(unique(df$year), function(year_) {
    if(year_ == unique(df$year)[1]) {
      class = "active"
    } else {
      class = ""
    }
    htmltools::tags$li(
      role = "presentation",
      class = class,
      htmltools::tags$a(
        role="tab",
        "data-toggle" = "tab",
        href = paste0("#", "year-", year_),
        year_
      )
    )
  })
)
```

```{r charts-per-year, results='asis', warning=FALSE, echo=FALSE}
htmltools::tags$div(
  class = "tab-content",
  lapply(unique(df$year), function(year_) {
    if(year_ == unique(df$year[1])) {
      class = "active in"
    } else {
      class = ""
    }
    df_year <- df %>% filter(year == year_) %>% mutate(path=paste(quarter, month, sep="-"))
    htmltools::tags$div(
      id = paste0("year-", year_),
      role = "tabpanel",
      class = paste0("tab-pane tabbed-pane fade ", class),
      sunburst(data.frame(xtabs(count~path,df_year))) # what to do here to display the graph?
    )
  })
)  
```
---
标题:“使用sunburst图进行降价-示例”
作者:“用户”
日期:“`r粘贴('date:',Sys.date())`”
输出:
html_文件:
保持冷静。md:是的
自给自足:没有
---
```{r加载包,include=FALSE}
图书馆(sunburstR)
图书馆(dplyr)
图书馆(knitr)
图书馆(htmltools)
```
```{r数据准备}
df%变异(路径=粘贴(季度、月份、九月=“-”)
htmltools::tags$div(
id=粘贴0(“年-”,年u0),
role=“tabpanel”,
类=粘贴0(“选项卡式窗格选项卡式窗格淡入淡出”,类),
sunburst(data.frame(xtabs(count~path,df#year))#在这里如何显示图形?
)
})
)  
```

这段代码有点凌乱,没有经过优化,但应该提供一个解决方案。通常,在循环中创建
htmlwidget
的解决方案是
tagList(lapply(…create htmlwidget…
),如中所述。但是,由于我们试图将每个图表包装在选项卡中,解决方案会变得混乱。我们需要手动创建选项卡,然后单独创建作为选项卡内容的图表

---
title: "Markdown with sunburst graphs - example"
author: "User"
date: "`r paste('Date: ', Sys.Date())`"
output: 
  html_document: 
    keep_md: yes
    self_contained: no
---

```{r load-packages, include=FALSE}
library(sunburstR)
library(dplyr)
library(knitr)
library(htmltools)
```

```{r data-preparation}
df <- data.frame(
  date = seq.Date(
    as.Date('2014-01-01'),
    as.Date('2016-12-31'),
    by = "days"
  ),
  stringsAsFactors = FALSE
)

df$year = format(df$date, "%Y")
df$quarter = paste0("Q", ceiling(as.numeric(format(df$date,"%m"))/3))
df$month = format(df$date, "%b")
df$count = rep(1, nrow(df))
```

# Graphs per year

```{r tabs-per-year, results='asis', warning=FALSE, echo=FALSE}
htmltools::tags$ul(
  class = "nav nav-pills",
  role = "tablist",
  lapply(unique(df$year), function(year_) {
    if(year_ == unique(df$year)[1]) {
      class = "active"
    } else {
      class = ""
    }
    htmltools::tags$li(
      role = "presentation",
      class = class,
      htmltools::tags$a(
        role="tab",
        "data-toggle" = "tab",
        href = paste0("#", "year-", year_),
        year_
      )
    )
  })
)
```

```{r charts-per-year, results='asis', warning=FALSE, echo=FALSE}
htmltools::tags$div(
  class = "tab-content",
  lapply(unique(df$year), function(year_) {
    if(year_ == unique(df$year[1])) {
      class = "active in"
    } else {
      class = ""
    }
    df_year <- df %>% filter(year == year_) %>% mutate(path=paste(quarter, month, sep="-"))
    htmltools::tags$div(
      id = paste0("year-", year_),
      role = "tabpanel",
      class = paste0("tab-pane tabbed-pane fade ", class),
      sunburst(data.frame(xtabs(count~path,df_year))) # what to do here to display the graph?
    )
  })
)  
```
---
标题:“使用sunburst图进行降价-示例”
作者:“用户”
日期:“`r粘贴('date:',Sys.date())`”
输出:
html_文件:
保持冷静。md:是的
自给自足:没有
---
```{r加载包,include=FALSE}
图书馆(sunburstR)
图书馆(dplyr)
图书馆(knitr)
图书馆(htmltools)
```
```{r数据准备}
df%变异(路径=粘贴(季度、月份、九月=“-”)
htmltools::tags$div(
id=粘贴0(“年-”,年u0),
role=“tabpanel”,
类=粘贴0(“选项卡式窗格选项卡式窗格淡入淡出”,类),
sunburst(data.frame(xtabs(count~path,df#year))#在这里如何显示图形?
)
})
)  
```