Problems with output=pdf_document

I am really new to R and I am currently having issues with exporting my .Rmd file to pdf. When my output is set to html_notebook, it works perfectly. Unfortunately, as soon as I change it to pdf_document, I get this message of error:

processing file: FL_SNPs211.Rmd
Quitting from lines 8-20 (FL_SNPs211.Rmd)
Error in eval(expr, envir, enclos) :
objet 'FetalFrac_155bp_SCIP211_consensus_modified' introuvable
Calls: ... handle -> withCallingHandlers -> withVisible -> eval -> eval

The 4th line is saying that the file 'FetalFrac_155bp_SCIP211_consensus_modified' can't be found. However, this is a dataset I have imported, and works perfectly fine when I run it or when the output is html_notebook. So how come it is not working when my output is pdf?
Any ideas on how to change this?

Also, is it possible to get rid of the "code" button when the output is html_notebook?

Thanks in advance.

Your last question is probably why markdown -> pdf doesn't work. If you are looking at a file with "*.nb.html" file extension and everything works there, then it is created slightly differently from when you actually knit your document to pdf/html. Notebook files will take current output that you have in your document. That means that some of the things can be done interactively and it'll still work. For example, if you loaded your data into environment interactively (from "Import dataset", for example), all the cells in notebook will be able to see it and process it.

However, when you knit a document to pdf/html, it'll start from completely new R session and try to render everything you've written. Therefore, if your code that exports data is not correct, then R won't be able to find it and that is why you get the error.

So, solution would be to take a look at how FetalFrac_155bp_SCIP211_consensus_modified object is created and make sure that it is in fact done in reproducible manner.

1 Like

Thank you for your reply, it does make sense.
However, the file I am loading into the environment is quite big, and I cannot manually input it in the form of a data set, do you have any idea how I can create this document so that it can be read when creating a pdf?

How do you load it in in the first place? You can put any function you are using for that in the first code chunk and it'll be executed. For example:

FetalFrac_155bp_SCIP211_consensus_modified <- readr::read_csv(...)

This would make it available for any function later in the chunks.

I am not sure I understand what you mean. I have imported it by doing "environment"->"import dataset" and then imported it as a text file. As soon as I added it to my code, it read it.
I tried to use the code you just gave but it is saying "Error, (...) used incorrectly" and when I replace it by my file it is saying Error : file must be a string, raw vector or a connection.

One of the reasons R is a bit more reliable to work with than Excel, for example, is that even if you use GUI elements (like "Import dataset") you still have a command that can be used to do the same thing programatically.

So, when you've imported the dataset in your console there was a command that produced the result that you got (I've assumed that it was something like readr::read_csv("path_to_file.csv") or similar). Is that the case? If so, you can put this command inside of your document so that when you knit it R will know where to look for this dataset.


This worked, thank you so much!