marcusory
marcusory

Reputation: 25

Error with knitr when knitting Rmd to PDF, but code runs fine?

I'm trying to knit an Rmd file, and the code runs fine, but when I attempt to knit I get back and error once it gets to my first chunk of R code:

Error in as.data.frame.default(x) : 
  cannot coerce class '"function"' to a data.frame
Calls: <Anonymous> ... merge.default -> merge -> as.data.frame -> as.data.frame.default
Execution halted

If I delete my first chunk of code, a similar error pops up for whatever is the new first chunk of code.

Edit: Here are my first

 ---
title: "[title]"
author: "[Name]"
output:
  pdf_document: default
  word_document:
    fig_caption: yes
---


library(knitr)  
library(png) 

#The code book of the two data sets can be found in these two links:


wwwn.cdc.gov/Nchs/Nhanes/2015-2016/DEMO_I.htm  
wwwn.cdc.gov/Nchs/Nhanes/2015-2016/CBQ_I.htm  




install.packages("Hmisc")


library(Hmisc)

demo <- sasxport.get("https://wwwn.cdc.gov/Nchs/Nhanes/2015-2016/DEMO_I.XPT")

cbq <- sasxport.get("https://wwwn.cdc.gov/Nchs/Nhanes/2015-2016/CBQ_I.XPT")


`str(demo)`
`str(cbq)`


```{r}
eat <- merge(demo, cbq, by="seqn")
```



```{r}
dim(eat)
# = 52 columns
# = 9,971 rows

# OR can use
ncol(eat)
nrow(eat)
```

Upvotes: 0

Views: 1158

Answers (1)

r2evans
r2evans

Reputation: 161110

Your problem here is reproducibility and undefined variables. Much of your code in your question is not within a code chunk, so it will print (albeit not as code) but will not create variables, load libraries, etc. With that in mind, you may realize then that demo and cqb are never defined within the document.

That means that if either exists as an object in the base R environment, then they exist but are not what you think they are. In this case, demo is in fact a function utils::demo and is available, so merge tries to do something with it. Unfortunately, as you might expect, there is no logic to be able to do merge-like operations on a function.

I can reproduce you error on my console:

ls()
# character(0)

# 'demo' not defined as a frame, so still utils::demo
# 'cbq' does not matter, because 'merge' cannot get past the problem with 'demo'

merge(demo, cbq)
# Error in as.data.frame.default(x) : 
#   cannot coerce class '"function"' to a data.frame

The fix might be to put your initial code within a code block.

I don't know what you intend to do with the URLs in there, but here is a less malformed Rmd:

---
title: "[title]"
author: "[Name]"
output:
  pdf_document: default
  word_document:
    fig_caption: yes
---

The code book of the two data sets can be found in these two links:

- wwwn.cdc.gov/Nchs/Nhanes/2015-2016/DEMO_I.htm  
- wwwn.cdc.gov/Nchs/Nhanes/2015-2016/CBQ_I.htm  

```{r}
library(knitr)  
library(png) 
install.packages("Hmisc")
library(Hmisc)
demo <- sasxport.get("https://wwwn.cdc.gov/Nchs/Nhanes/2015-2016/DEMO_I.XPT")
cbq <- sasxport.get("https://wwwn.cdc.gov/Nchs/Nhanes/2015-2016/CBQ_I.XPT")
```

`str(demo)`

`str(cbq)`

```{r}
eat <- merge(demo, cbq, by="seqn")
```

```{r}
dim(eat)
# = 52 columns
# = 9,971 rows

# OR can use
ncol(eat)
nrow(eat)
```

Some notes about this:

  • Unless this is intended to always run in a sterile R environment (e.g., docker container), then I find install.packages to be not only poor design but also not fair to anybody else that might use this Rmd document: perhaps I'm intending to stay on a stable older package version (reproducibility!), but just by rendering your document, my package is revocably upgraded.

  • You `str(demo)` is printing the literal string "str(demo)" in fixed-width font, but running nothing. This might either go in a code chunk, or you can use inline-code by using `r str(demo)` (that's backtick, "r", space, code, backtick).

Upvotes: 2

Related Questions