0
votes

I created a nice pie chart. Nothing too fancy.

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

in the area of my RMarkdown file, I can not seem to get the correct coding to get Source embedded in it.

My plot chunk looks like this:

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

and when I go to knit it together I get this error

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

Any idea?

Edited to add:

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")

Edited to Add my RMarkdown:


title: "UMD" author: "Laura Walker" date: "February 16, 2017"

output: html_document

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

R Markdown

Loading the Libraries

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

Loading the Data Sets

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")

Defining UltimateSponsor

In the cases where there is a Prime Sponsor, I've developed code to create an Ultimate Sponsor. When there is a Prime, the Ultimate Sponsor will take the identity of the Prime. When there is just a sponsor, the Ultimate Sponsor will take that identity. ExpendData$UltimateSponsorID <- ifelse(ExpendData$Prime=="N", ExpendData$PrimeSponsorID, ExpendData$SponsorID)

Merging expend data with other tables

Merging expend data with sponsor data based on Ultimate Sponsor ExpendData <-merge(x=ExpendData, y=SponsorData, by.x="UltimateSponsorID", by.y="SponsorID") Merging expend data with project data based on project# ExpendData <-merge(x=ExpendData, y=ProjectData, by.x="ProjectNumber", by.y="PROJECT")

Question 1

Question 1 asks "How much of your total expenditures for research and development R&D came from the following sources" Question1 <-aggregate(TotalExpense~FY+Source, CurrentFY, sum) Question1 <-Question1[order(Question1$FY),c(1:3)] Extracting data for only FY 2016 Question1_2016 = subset(Question1, FY == "2016") Constructing an FY16 Source of Funding Pie Chart ``` Question1_2016$Percent <- round(Question1_2016$TotalExpense/sum(Question1_2016$TotalExpense)*100, digits = 1) Question1_2016$Labels <- paste(Question1_2016$Source, Question1_2016$Percent) Question1_2016$Labels <- paste(Question1_2016$Labels, "%",sep = " ")

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

```

You can also embed plots, for example:

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

1
Where are you creating the pie chart? Why don't you just create the chart inside of the markdown chunk?GarAust89
Thank you @GarAust89 I created it in another chunk and it doesn't appear eitherLaura Walker
Constructing an FY16 Source of Funding Pie Chart ``` Question1_2016$Percent <- round(Question1_2016$TotalExpense/sum(Question1_2016$TotalExpense)*100, digits = 1) Question1_2016$Labels <- paste(Question1_2016$Source, Question1_2016$Percent) Question1_2016$Labels <- paste(Question1_2016$Labels, "%",sep = " ") Source <- plot_ly(Question1_2016, labels = Question1_2016$Source, values = Question1_2016$TotalExpense, type = 'pie') ```Laura Walker
Please make your question reproducible so that we can run your code on our systems.eipi10
When you run the code to create Source interactively, do you get an error?eipi10

1 Answers

0
votes

This isn't an answer, but just an illustration of what we need to be able to answer your question. Below is a minimal reproducible example. It also runs without error, which is why we need more information to figure out why your document isn't compiling.

---
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 
```

Based on the data you provided, I still don't get an error in the document below. The error you posted suggests that Source isn't actually being created when you run the Source <- plot_ly(... code. Can you paste into your question a complete rmarkdown document that, when compiled, exhibits the error you posted?

---
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
```