knitr: object cannot be found when converting markdown file into html
As already mentioned by @BenBolker , you can use knit2html
( Note that it is different from the Rstudio button, Rstudio use its own function to process document) from knitr
:
x <- 10
writeLines(c("# hello markdown",
"```{r hello-random, echo=TRUE}",
"rnorm(x)", "```"), ## note the use of x
"test.Rmd")
library(knitr)
knit2html("test.Rmd")
Knitting a Rmd document with errors without showing them
Put this code chunk in the beginning of your Rmd
document and it will do what you want:
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, eval = FALSE)
```
Alternatively, you could do eval = FALSE
in each of the code chunk that you do not want to get an error message from. However, the first approach will save you time.
Compile error of TeX in .rmd file to create .html
Most raw LaTeX and raw HTML is just passed through Pandoc without changes. There are some exceptions: math in dollar signs (e.g. $x^2$
) will be handled by MathJax, which can handle a subset of LaTeX.
Even if you're dealing with this subset, you will see what looks like raw LaTeX if MathJax can't run. This could happen if you have Javascript turned off in your web browser (maybe by NoScript), or if you can't connect to the MathJax website and haven't asked to reference a local copy of MathJax.
Error with knitr when knitting Rmd to PDF, but code runs fine?
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).
Related Topics
Does Converting Character Columns to Factors Save Memory
Multiple Histograms in Ggplot2
Creating Legend with Circles Leaflet R
Multiple Functions in a Single Tapply or Aggregate Statement
Daily Time Series with Ts.. How to Specify Start and End
How to Display the Median Value in a Boxplot in Ggplot
Does Roxygen2 Automatically Write Namespace Directives for "Imports:" Packages
Format Date to Year-Month in R
Interpolate Zoo Object with Missing Dates
How to Get Ggplot to Order Facets Correctly
Using R to "Click" a Download File Button on a Webpage
One-Class Classification with Svm in R
Why Is Expand.Grid Faster Than Data.Table 's Cj
How to Read a Password Protected Excel File into R
How to Insert (Add) a Row to a SQLite Db Table Using Dplyr Package
Replace Character at Certain Location Within String