0

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)

Laura Walker
  • 307
  • 2
  • 6
  • 16
  • Where are you creating the pie chart? Why don't you just create the chart inside of the markdown chunk? – GarAust89 Feb 16 '17 at 18:04
  • Thank you @GarAust89 I created it in another chunk and it doesn't appear either – Laura Walker Feb 16 '17 at 18:10
  • 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 Feb 16 '17 at 18:11
  • 2
    Please make your question reproducible so that we can run your code on our systems. – eipi10 Feb 16 '17 at 18:12
  • When you run the code to create `Source` interactively, do you get an error? – eipi10 Feb 16 '17 at 18:13
  • @eipi10 How does one create the item interactively? – Laura Walker Feb 16 '17 at 18:16
  • In your console paste in the following code and press Enter: `Source <- plot_ly(Question1_2016, labels = Question1_2016$Source, values = Question1_2016$TotalExpense, type = 'pie')` If your code works, that should create an object called `Source` containing the pie chart. Then if you type `Source` in the console, the pie chart should be displayed in a viewer window. Or you can just paste `plot_ly(Question1_2016, labels = Question1_2016$Source, values = Question1_2016$TotalExpense, type = 'pie')` into the console and see if that produces a plot or returns an error. – eipi10 Feb 16 '17 at 18:18
  • @eipi10 of course! Yes. I do get a pie chart in my plot area. I just don't know how to embed them in my Rmarkdown file. – Laura Walker Feb 16 '17 at 18:20
  • Well, to figure out why your code is not working, we need a reproducible example: That is, a complete rmarkdown document (complete but minimal--the smallest document needed to illustrate your problem) that includes a small data sample (use `dput` for this) to try out your code on. See [here](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for more on providing a reproducible example. – eipi10 Feb 16 '17 at 18:24
  • can i share it with you via google dirve? – Laura Walker Feb 16 '17 at 18:31
  • Much better to run `dput(Question1_2016[1:5, ])` and paste the output of that into your question (not into a comment). – eipi10 Feb 16 '17 at 18:34
  • @eipi10 Thank you! i've done it above – Laura Walker Feb 16 '17 at 18:36
  • Thanks for providing a full `rmarkdown` document. However, you need to remove all the data loading and processing code and instead provide the actual data for `Question1_2016` in a code chunk. Otherwise we can't run it. Follow the example in my answer for how to do this. Then `knit` the document and make sure it still gives the same error before posting it here. – eipi10 Feb 16 '17 at 18:49
  • Also, the last line of your rmarkdown document has a few errors: (1) period after fig.width=8 instead of a comma. (2) `source` instead of `Source` and (3) `plot(Source)`, which will give an error with a plot_ly object, instead of just `Source`. – eipi10 Feb 16 '17 at 18:52
  • @eipi10 thank you for your patience. I think I extracted all the extraneous stuff. I'm still getting errors though :-( – Laura Walker Feb 16 '17 at 19:32

1 Answers1

0

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
```
eipi10
  • 91,525
  • 24
  • 209
  • 285