How to debug a Rsweave - r

I have an rsweave file that I run almost twice a week. Last time I used it a change a couple of things and when I run it to compile to pdf I got the following errors:
The pdf compiles complitly, and the only thing I notice that the error did is that the the pdf output has a extra page (the first one) all blank. I don't know how to make a reproducible example of the errors because I don't know whats the cause of it. But any way I just want to know generally how to debug a rsweave file when getting latex error like the ones in the picture

You don't say how you are running Sweave, but that looks like RStudio. To debug something like this, just run Sweave explicitly in the R console, e.g. if your input file is source.Rnw, run
Sweave('source.Rnw')
This will produce source.tex. Open that file in a text editor and look at the start of it. You will see that \Schunk is used on line 27, but \begin{document} doesn't occur until sometime later.
My guess is that you added some text or a code chunk to the header. All text belongs after \begin{document}.
Edited to add: It turns out from the comments below that you were using print(...) in a code chunk before \begin{document}. In Sweave, print output goes into the document. If you want a message to show up in the console log but not the document, use message("some text"). You'll also need to suppress the echoing of the command if you want to do this in the document header. For example,
<<echo=FALSE,results=hide>>=
message("Started at ", Sys.time())
#
will result in something like this in your console log:
1 : keep.source term hide (Untitled.Rnw:6)
Started at 2017-09-27 08:28:33
and nothing in the 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

"The system cannot find the file specified whenever I knit"

Whenever I knit to a PDF in RStudio, the error "The system cannot find the file specified."
Here is the code that I'm using:
##importing data
library(readr)
Quiz1data_2 <- read_csv("C:/Users/erinp/Downloads/Quiz1data-2.csv")
I have restarted RStudio multiple times and I have copied and pasted the exact link that my file is saved to and it's still not working.
What am I not seeing or what am I not thinking?
Some suggestions/questions:
Without knitting, when you run the line reading in the csv, does it work?
Also, are you sure that the error is referring to the data csv? Could it be referring to the (I'm assuming) markdown file you are writing your code in? Have you moved that file since you started working in it?
Are you able to knit other documents to pdf? You need MiKTeX on a windows machine. Does knitting to html work?
I've found R to be a little tricky reading in files. I usually use the base function like this:go to the environment tab>import dataset>from text(base)> (select the file you want, hit open)>(select settings so that the dataframe preview looks right>import. Code that does this will run in the console, and I copy it into my markdown file so that every time it knits, it replicates that successful process.
I figured it out. It's because I forgot to assign a value to an object so it wouldn't knit into a PDF. I made a comment earlier, but it's small so I thought I would add this to the answer section.

Rmarkdown Error: Paragraph ended before \text# was complete. Unable to find source

When trying to knit an rmarkdown file to a PDF, I am encountering the following error. It is referencing a line number with no text reference and the line number is out of range of the number of lines of the file.
Rmarkdown Error:
! Paragraph ended before \text# was complete.
<to be read again>
\par
l.375
If anyone has encountered a similar error or has an idea about how to identify the location of the error in the file it'd be appreciated.
I have encountered this a couple times. It can be frustrating because you are likely finishing a lot of work and just creating the report so it can take some time to knit to find what is causing it.
The first thing you likely want to do is to heed that next line that has:
... See <your_output_file>.log for more info. ...
However because of the confluence of using all of TeX, Markdown and R's text generator, the line numbers are off a bit in this log file, so you may need to just start hunting.
I tend to use $\color{blue}{\text{, however likely searching for \text{ throughout your .Rmd file should be enough.
Likely you will need to escape some character as described in this answer. For me it is often parentheses.
For example I just got this error on a line that had:
$\color{blue}{\text{..., (if provided)...}}$
I was able to resolve with:
$\color{blue}{\text{..., \(if provided\)...}}$

Redirect Output to Message r

Is it possible to capture output that is printed by a function and instead output it via message? I am running a report using knitr that deals with very large tables (millions of rows) and I have message statements in the code so I can have an idea of where I am. This works fine as I simply set the global chunk option to message = F and I can see the message in the console and it does not show up in the final pdf. However, I use fread to read the data from a file, and the showProgress option does not print a message! Setting the chunk option to results='hide' prevents the progress from being printed to the final document, but it also prevents me from seeing it at all, so that is unhelpful. Any help would be appreciated, thanks!

How to include output of help() in sweave pdf

I would like to include function documentation from the help file in a sweave document. I tried the following sweave block
<<>>=
?lm
#
but I get error messages when calling Sweave on the Rnw file. How can I include the entire help message in the document?
The key to this is really figuring out how to get the information you desire as a character string.
help("lm") opens up the help file for the relevant function, but not in the console.
utils:::.getHelpFile gives you the Rd version of that file.
From there, you can use tools:::Rd2txt to convert it to text...
Which can be "captured" using capture.output.
Those are essentially the steps contained in the first few lines of helpExtract from my "SOfun" package. That function, however, captures just the requested section.
Instead, if you can settle for just the text, you can do something along the lines of:
gsub("_\b", "",
capture.output(tools:::Rd2txt(
utils:::.getHelpFile(utils::help("lm")))))

Resources