R Markdown: the code chunks run, but getting errors when knitting - r

I'm using RStudio Cloud. I'm running an RMarkdown doc, when I run the code chunks in the doc they work fine, but when I'm trying to knit I get errors. Before I used the code "knitr::opts_chunk$set(error = TRUE)" the doc wouldn't knit at all (error message was saying objects couldn't be found); now it knits BUT I get the error message "## Error in .External2(C_dataviewer, x, title): unable to start data viewer" when I try to View(dataframes) that I've created (the data frames I created show up in the global environment but not in my file path directory, which is /cloud/project). I'm guessing that RMarkdown can't find these since they are in a different file path? I'm also assuming that files in the global directory aren't available for RMarkdown to knit, so how do I get them to be available for RMarkdown to knit? Thank you!

Instead of doing View you can just do this.
```{r}
df # assuming df is your data.frame object name
```
this will print your whole dataframe df in the knitted document.

Related

Output Lines get out of bounds when I knit my Rmarkdown

when trying to knit my Rmd to output it as pdf document, some of the outputs get out of bounds
example of the output that gets out of bounds:
The YAML and settings are this:
.
Also when I "knit on save", the rstudio doesn't stop the process untill i give up waiting, idk if it's because it is trying to knit a big pdf(140 pages), but when i press stop in the Render it aborts and somehow the document shows up in my files.
Although I don't show it, i have the knitr packages installed. I've tried many forms of output, the only YAML format that i can generate a pdf with is the one from the picture. Idk if there is something that could prevent the outputs to go out of bounds. Help is appreciated

Can't Knit R Markdown File When I Use ggplot with it

I'm currently doing an R Markdown File of an analysis I recently finished using R. With that, whenever I add details, I click Knit to ensure everything I added goes well on the HTML knit file.
I noticed that when I used ggplot on my R Markdown file, the file doesn't knit anymore and it says:
Error while opening file - No Such File or directory.
But when I deleted the ggplot command, it gets knitted again.
But this time, without the visualization, it gets knitted again.
UPDATE: I see this error message on the console tab everytime I use ggplot on my .rmd file.
So with that, I call on install.packages("ggplot2") & library("ggplot2") on my .rmd file. However, I get this error instead.
How will I be able to retain the ggplot command and at the same time be able to knit the file? As an added reference, the ggplot command executes on the .rmd file, it just doesn't knit. Can someone help me please?

When knitting a markdown file I have an error "object not found" [duplicate]

I have an uncleaned dataset. So, I have imported it to my R studio.Then when I run nrow(adult) in the rmarkdown file and press ctrl+Enter it works, but when i press the knit the following error appears:'
When you knit something it gets executed in a new environment.
The object adult is in your environment at the moment, but not in the new one knit creates.
You probably did not include the code to read or load adult in the knit.
If you clear your workspace, as per #sebastian-c comment, you will see that even ctrl+enter does not work.
You have to create the adult object inside your knit. For example, if your data in from a csv add
adult <- read.csv2('Path/to/file')
in the first chunk.
Hope this is clear enough.
Another option, in the same way as the previous, but really useful in case you have a lot of diferent data
Once you have all your data generated from your R scripts, write in your "normal code" ( any of your R scripts):
save.image (file = "my_work_space.RData")
And then, in your R-Markdown script, load the image of the data saved previously and the libraries you need.
```{r , include=FALSE}
load("my_work_space.RData")
library (tidyverse)
library (skimr)
library(incidence)
```
NOTE: Make sure to save your data after any modification and before running knitr.
Because usually I've a lot of code that prepares the data variables effectively used in the knitr documents my workaround use two steps:
In the global environment, I save all the objects on a file using
save()
In the knitr code I load the objects from the file using load()
Is no so elegant but is the only one that I've found.
I've also tried to access to the global environment variables using the statement get() but without success
If you have added eval = FALSE the earlier R code won't execute in which you have created your object.
So when you again use that object in a different chunk it will fail with object not found message.
When knitting to PDF
```{r setup}
knitr::opts_chunk$set(cache =TRUE)
```
Worked fine.
But not when knitting to Word.
I am rendering in word. Here's what finally got my data loaded from the default document directory. I put this in the first line of my first chunk.
load("~/filename.RData")

Knit error. Object not found

I have an uncleaned dataset. So, I have imported it to my R studio.Then when I run nrow(adult) in the rmarkdown file and press ctrl+Enter it works, but when i press the knit the following error appears:'
When you knit something it gets executed in a new environment.
The object adult is in your environment at the moment, but not in the new one knit creates.
You probably did not include the code to read or load adult in the knit.
If you clear your workspace, as per #sebastian-c comment, you will see that even ctrl+enter does not work.
You have to create the adult object inside your knit. For example, if your data in from a csv add
adult <- read.csv2('Path/to/file')
in the first chunk.
Hope this is clear enough.
Another option, in the same way as the previous, but really useful in case you have a lot of diferent data
Once you have all your data generated from your R scripts, write in your "normal code" ( any of your R scripts):
save.image (file = "my_work_space.RData")
And then, in your R-Markdown script, load the image of the data saved previously and the libraries you need.
```{r , include=FALSE}
load("my_work_space.RData")
library (tidyverse)
library (skimr)
library(incidence)
```
NOTE: Make sure to save your data after any modification and before running knitr.
Because usually I've a lot of code that prepares the data variables effectively used in the knitr documents my workaround use two steps:
In the global environment, I save all the objects on a file using
save()
In the knitr code I load the objects from the file using load()
Is no so elegant but is the only one that I've found.
I've also tried to access to the global environment variables using the statement get() but without success
If you have added eval = FALSE the earlier R code won't execute in which you have created your object.
So when you again use that object in a different chunk it will fail with object not found message.
When knitting to PDF
```{r setup}
knitr::opts_chunk$set(cache =TRUE)
```
Worked fine.
But not when knitting to Word.
I am rendering in word. Here's what finally got my data loaded from the default document directory. I put this in the first line of my first chunk.
load("~/filename.RData")

knitr: object cannot be found when converting markdown file into html

Hi I am using R studio and the "knitHTML" button to convert my Rmd file into a html file. However, even thought the code runs fine, when using knitHTML it cannot find any of my objects previously created:
## Error: object 'cbt_2010' not found
however if I type cbt_2010 at the terminal - it is there. Bascially knit cannot find any of the objects in the workspace.
what am I doing wrong? it just seems any data produced in each chunk is lost in memory when using knit!
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")
Maybe I am missing something in your question, but if you create object 'cbt_2010' in your .Rmd file, knitr will have that object to work with.
Stated differently, you can find it when you type object 'cbt_2010' at the console because you created that object and it is available to R. You need to do the same in the .Rmd file.

Resources