R markdown doesn't find object - r

I am using R markdown to knitter my codes into a word document
I am pretty new to R as well as to R markdown
I have the following code :
{r, eval=TRUE, fig.height=7, fig.width=5, message=FALSE, warning=FALSE}
lifeExpectancy <- rename(lifeExpectancy, Incomegroup= WORLDBANKINCOMEGROUP)
plife <- ggplot(lifeExpectancy, aes(x= LifeExpectancy))
plife1 <- plife + geom_histogram(fill="white", aes(color= Incomegroup))
plife1 <- plife1 +facet_grid(SEX~Incomegroup) + xlab("average life
expectancy")
plife1
this is the warning message it returns :
Quitting from lines 32-38 (Termpaper_Franziska.Rmd)
error in rename_(.data, .dots = lazyeval::lazy_dots(...)) :
Object 'lifeExpectancy' not found
calls : <Anonymous> ... withCallingHandlers -> withVisible -> eval -> eval
> rename -> rename_
stopped`
What I already did :
I set the working directory correctly for R markdown as well as for my original script
I used
{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
knitr::opts_knit$set(root.dir =
"C:/Users/Maxi/Desktop/PrettyPlots2016/Termpaper")
to set the working directory but that didn't help either.
Last but not least I also set
eval=FALSE
but then I didn't get my plot of course, which didn't help me either.
If anyone has a solution for my dilemma I would be very happy to hear it !
Thank you a lot !

Related

R markdown table

I am working on a paper in R Markdown. I'd like to create a table which contain math symbols. Below, it my simple code with a table:
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## R Markdown
```{r echo=FALSE}
library(knitr); library(kableExtra)
df <- data.frame(A=c("Hello!", 100, "$x^2+100$"), L=c(10, "World!", "100+250*1"))
knitr::kable(df, escape = FALSE, booktabs = TRUE,
caption = "An example[note]",
col.names = c("Left", "Right")) %>%
add_indent(c(2, 3), level_of_indent = 1.5, all_cols = TRUE) %>%
add_footnote(c("Source: Book."), notation = "symbol")
```
As a result, every time I knit the file the same error appears:
! Extra alignment tab has been changed to \cr.
<recently read> \endtemplate
I will appreciate if very much if you help me to fix this error.
when I run your example it knits perfectly fine. this is what it looked like in HTML. I haven't seen that error before so I am not sure what the problem is. Perhaps it is a problem with your YAML header, or a line of code previous to the table that is causing problems. Hopefully someone else knows!

How to make Rmarkdown carry over sections of code?

I have a piece of RMarkdown code that runs fine in one block.
```{r}
library(tidyverse)
data("PlantGrowth")
set.seed(1234)
PlantGrowth %>% sample_n_by(group, size = 1)
levels(PlantGrowth$group)
```
However, if I try to break it into pieces of code, which I need to do in order to add explanations, I get the following error in the RMarkdown console.
```{r eval=TRUE}
library(tidyverse)
data("PlantGrowth")
set.seed(1234)
```
Comment the code here
```{r eval=TRUE}
PlantGrowth %>% sample_n_by(group, size = 1)
levels(PlantGrowth$group)
```
Say more stuff...
Error in PlantGrowth %>% sample_n_by(group, size = 1) : could not find function "%>%" Calls: <Anonymous> ...withVisible -> eval_with_user_handlers -> eval -> eval Execution Halted.
Is there a way to make markdown carry over the libraries and variables loaded in previous code blocks up to the end of the document?
Make sure you insatlled tidyverse and rstatix in your env.
```{r eval=TRUE}
library(tidyverse)
library(rstatix)
data("PlantGrowth")
set.seed(1234)
```
Comment the code here
```{r eval=TRUE}
PlantGrowth %>% sample_n_by(group, size = 1)
levels(PlantGrowth$group)
```

invalid connection when using ggplot2 in rmarkdown

even a simple histogram such as I have included in the rmarkdown code below:
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(ggplot2)
mtcars <- mtcars
p <- ggplot(data=mtcars, aes(x=mpg))
p + geom_histogram()
```
returns the histogram in the rmarkdown window, but in the console returns:
Error in isIncomplete(con) : invalid connection
Error in isIncomplete(con) : invalid connection
Error in isIncomplete(con) : invalid connection
this error is returned three times. I am not sure what could be closing the connections, or perhaps it's something different all together. Any thoughts?

Cannot create PDF from R Markdown without template file lines

I am trying to create a long document in R Studio using R Markdown to PDF, and I am getting the following error when I push the "Knit PDF" button:
! Undefined control sequence.
l.124 \begin{center}\includegraphics
pandoc.exe: Error producing PDF from TeX source
Error: pandoc document conversion failed with error 43
In addition: There were 50 or more warnings (use warnings() to see the first 50)
Execution halted
I am able to properly create the PDF if I make a new R Markdown document and I paste the code in from the original, provided I keep the following code at the end of the new document. When I erase this code, I get the above error.
```{r, echo=FALSE}
plot(cars)
```
I have no need or desire for the cars plot to be in my document. I am able to Knit this document into HTML without the undesired code, but not to PDF. Can anyone advise why this error may be happening? (This is my first post to this forum, so if you need more specific information, please advise).
I am running Windows 7 32 bit, R Studio version 0.98.1103, MiKTeX version 2.9.
Edit/Update: Here is my code, including the cars graph which I do not want.
---
title: "Untitled"
date: "Saturday, April 25, 2015"
output: pdf_document
---
```{r echo=FALSE, warning=FALSE, include=FALSE}
require(qcc)
require(data.table)
require(ggplot2)
require(sm)
require(knitr)
require(xtable)
attach(airquality)
airquality$indicator <- ifelse(airquality$Month < 8,'Classic','New')
```
```{r echo=FALSE, warning=FALSE, results='hide', fig.align='center'}
par(mfrow=c(2,1), oma=c(1,1,1,1), cex=.5, mex=.5, ps=8, mgp=c(3,1,0))
airquality <- airquality[complete.cases(airquality),]
# Plot variable on an SPC chart of type xbar.one
imrchart2 <- qcc(airquality$Ozone[airquality$indicator=="Classic"], "xbar.one", std.dev="SD", add.stats=TRUE, ylab="Ozone", title="Xbar.One Chart for Ozone", xlab="", data.name="Classic", restore.par=FALSE, newdata=airquality$Ozone[airquality$indicator=="New"], newdata.name="New", ylim=range(airquality$Ozone))
# Plot a CUSUM chart
cusum(airquality$Ozone[airquality$indicator=="Classic"], sizes=1, add.stats=TRUE, ylab="Ozone", title="CUSUM Chart for Ozone", xlab="", restore.par=FALSE, newdata=airquality$Ozone[airquality$indicator=="New"], data.name="Classic", newdata.name="New")
```
```{r, echo=FALSE}
plot(cars)
```

Render rCharts in slides from Slidify

Recently I have been experimenting with slidify and rCharts. Tutorials for generating simple charts while using slidify are explanatory, but I was unable to find any such tutorial regarding rCharts.
For example, I know that the following generates an interactive plot
data(mtcars)
r1<- rPlot(mpg ~ wt | am + vs, data=mtcars, type="point")
data(iris)
hair_eye = as.data.frame(HairEyeColor)
rPlot(Freq ~ Hair | Eye,color = 'Eye', data = hair_eye, type = 'bar')
However, I have no idea how to incorporate the resulting plot into my slides using slidify.
EDIT - After the helpful comment
I tried the following, having seen it on Ramnath's git:
---
title : Practice
subtitle : makes perfect
author : Noob
job :
framework : io2012 # {io2012, html5slides, shower, dzslides, ...}
highlighter : highlight.js # {highlight.js, prettify, highlight}
hitheme : tomorrow #
widgets : [nvd3] # {mathjax, quiz, bootstrap}
mode : selfcontained # {standalone, draft}
---
```{r setup, message = F, echo = F}
require(rCharts)
options(RCHART_WIDTH = 800, RCHART_HEIGHT = 500)
knitr::opts_chunk$set(comment = NA, results = 'asis', tidy = F, message = F)
```
## NVD3 Scatterplot
```{r echo = F}
data(mtcars)
n1 <- nPlot(mpg ~ wt, group = 'gear', data = mtcars, type = 'scatterChart')
n1$print('chart1')
```
But ended up with this error:
Error in file(con, "r") : cannot open the connection
In addition: Warning message:
In file(con, "r") :
cannot open file 'libraries/widgets/nvd3/nvd3.html': No such file or directory
After which I decided to copy the nvd3 folder from Ramnath's widgets directly into mine, hoping that this would solve the matter. However, this ended up crazily showing Ramnath's git page as well as my slides in the background!
What to do? I would really appreciate any guidelines/pointers/advice on how to accomplish this task. And, I hope this question aids other novices like myself in using the wonderful rCharts.
Note: I am using the standard editor for R, not R-studio. I feel the former is less cluttered.
All instructions below assume that you have the dev branch of the packages installed (slidify, slidifyLibraries and rCharts). You can accomplish this using install_github.
pkgs <- c("slidify", "slidifyLibraries", "rCharts")
devtools::install_github(pkgs, "ramnathv", ref = "dev")
There are two ways to include an rCharts viz in your slidify document, and the deck below illustrates both ways. If you print a plot in a code chunk, as you would do so in the R console, slidify automatically detects that you are running it in a knitr session and as a result save the resulting html to an iframe and embed it in the deck. Alternately, you can specify the chart inline, in which case you have to use n1$show("inline") and also include ext_widgets: {rCharts: libraries/nvd3} in your YAML front matter.
The iframe method is the default and the recommended method to avoid conflicts between various javascript files and css. The inline method does work well for several rCharts libraries, but make sure to check before you use.
---
title : rCharts Integration
ext_widgets : {rCharts: libraries/nvd3}
mode: selfcontained
---
## NVD3 Plot Inline
```{r nvd3plot, results = 'asis', comment = NA, message = F, echo = F}
require(rCharts)
n1 <- nPlot(mpg ~ wt, data = mtcars, type = 'scatterChart')
n1$show('inline')
```
---
## NVD3 Plot Iframe
```{r nvd3plot2, results = 'asis', comment = NA, message = F, echo = F}
require(rCharts)
n1 <- nPlot(mpg ~ wt, data = mtcars, type = 'scatterChart')
n1
```

Resources