尝试在rmarkdown文档

时间:2017-02-16 17:59:10

标签: r r-markdown plotly

我创建了一个漂亮的饼图。没什么太花哨的。

Source <- plot_ly(Question1_2016, labels = Question1_2016$Source, values = Question1_2016$TotalExpense, type = 'pie')

在我的RMarkdown文件区域,我似乎无法获得正确的编码以将Source嵌入其中。

我的情节块看起来像这样:

```{r source, echo=FALSE}
Source 
```

当我把它编织在一起时,我得到了这个错误

Error in eval(expr, envir, enclos) : object "Source" no found Calls: <Anonymous>...handle -> withCallingHandlers -> withVisible -> eval _. eval Execution Halted

有什么想法吗?

编辑添加:

dput(Question1_2016[1:5, ])
structure(list(FY = c(2016, 2016, 2016, 2016, 2016), Source = c("All Other Sources", 
"Business", "Federal", "Institutional Support", "Institutional Support-Cost Sharing"
), TotalExpense = c(1819424.18, 2850796.2, 47625255.58, 33363659.29, 
6922360.06), Percent = c(1.5, 2.3, 39.2, 27.5, 5.7), Labels = c("All Other Sources 1.5 %", 
"Business 2.3 %", "Federal 39.2 %", "Institutional Support 27.5 %", 
"Institutional Support-Cost Sharing 5.7 %")), .Names = c("FY", 
"Source", "TotalExpense", "Percent", "Labels"), row.names = c(11L, 
22L, 33L, 44L, 55L), class = "data.frame")

编辑添加我的RMarkdown:

标题:“UMD” 作者:“劳拉沃克” 日期:“2017年2月16日”

输出:html_document

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

R Markdown

加载库

library(readxl) library(reshape) library(plotly) library(tidyr)

加载数据集

ExpendData <- read_excel("C:/Users/user/Desktop/ExpendData.xlsx", + na = "empty") SponsorData <- read_excel("C:/Users/user/Desktop/SponsorData.xlsx") ProjectData <- read_excel("C:/Users/user/Desktop/ProjectData.xlsx")

定义UltimateSponsor

在有赞助商的情况下,我开发了代码来创建终极赞助商。当有Prime时,终极赞助商将采用Prime的身份。如果只有赞助商,终极赞助商将采用该身份。 ExpendData$UltimateSponsorID <- ifelse(ExpendData$Prime=="N", ExpendData$PrimeSponsorID, ExpendData$SponsorID)

将支出数据与其他表合并

根据终极赞助商将支出数据与赞助商数据合并 ExpendData <-merge(x=ExpendData, y=SponsorData, by.x="UltimateSponsorID", by.y="SponsorID") 根据项目将支出数据与项目数据合并# ExpendData <-merge(x=ExpendData, y=ProjectData, by.x="ProjectNumber", by.y="PROJECT")

问题1

问题1询问“研发R&amp; D的总支出中有多少来自以下来源” Question1 <-aggregate(TotalExpense~FY+Source, CurrentFY, sum) Question1 <-Question1[order(Question1$FY),c(1:3)] 仅为2016财年提取数据 Question1_2016 = subset(Question1, FY == "2016") 构建2016财年资金饼图来源 ``` Question1_2016 $百分比&lt; - round(Question1_2016 $ TotalExpense / sum(Question1_2016 $ TotalExpense)* 100,digits = 1) Question1_2016 $ Labels&lt; - paste(Question1_2016 $ Source,Question1_2016 $ Percent) Question1_2016 $ Labels&lt; - paste(Question1_2016 $ Labels,“%”,sep =“”)

来源&lt; - plot_ly(Question1_2016,labels = Question1_2016 $ Source,values = Question1_2016 $ TotalExpense,type ='pie')

```

您还可以嵌入图表,例如:

{r plot, echo=FALSE} plot(Source)

1 个答案:

答案 0 :(得分:0)

这不是一个答案,只是说明我们需要能够回答您的问题。下面是一个可重复性最小的例子。它也可以正常运行,这就是为什么我们需要更多的信息来弄清楚你的文档没有编译的原因。

---
output: html_document
---

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

dat = data.frame(x=seq(10,100,length=5), group=LETTERS[1:5])
```

```{r]}
library(plotly)
Source = plot_ly(dat, labels=~group, values=~x, type="pie")
```

```{r source, echo=FALSE}
Source 
```

根据您提供的数据,我仍然不会在下面的文档中收到错误消息。您发布的错误表明,当您运行Source代码时,Source <- plot_ly(...实际上并未创建。您是否可以将一个完整的rmarkdown文档粘贴到您的问题中,该文档在编译时会显示您发布的错误?

---
output: html_document
---

```{r setup, include=FALSE}
library(plotly)
```

```{r, echo=FALSE}
Question1_2016 = structure(list(FY = c(2016, 2016, 2016, 2016, 2016), Source = c("All Other Sources", 
"Business", "Federal", "Institutional Support", "Institutional Support-Cost Sharing"
), TotalExpense = c(1819424.18, 2850796.2, 47625255.58, 33363659.29, 
6922360.06), Percent = c(1.5, 2.3, 39.2, 27.5, 5.7), Labels = c("All Other Sources 1.5 %", 
"Business 2.3 %", "Federal 39.2 %", "Institutional Support 27.5 %", 
"Institutional Support-Cost Sharing 5.7 %")), .Names = c("FY", 
"Source", "TotalExpense", "Percent", "Labels"), row.names = c(11L, 
22L, 33L, 44L, 55L), class = "data.frame")
```

```{r}
Source <- plot_ly(Question1_2016, labels = Question1_2016$Source, values = Question1_2016$TotalExpense, type = 'pie')
```

```{r}
Source
```