Knitr PDF ok, but no content and error messages

101 views
Skip to first unread message

OFish

unread,
Feb 3, 2014, 6:38:24 AM2/3/14
to kn...@googlegroups.com
Hi guys, posted this in stackoverflow, but still haven't got an answer and this is driving me nuts. Any help would be great!

Thanks.


Appologies from the go, still getting my head around all the functions in R and RStudio, so some of you might think I'm a total idiot for asking this. BTW: I've translated some of the stuff produced by R from German to english.

I'm running RStudio Version 0.97.551 on a MAC OS X, Version 10.8.5.

I'm trying to compile a PDF using knitr. I installed knitr, ran it by using library() and set my working directory to the same directory as my default working directory, which is also the same folder that contains the .txt files I use as my datasets. And have chosen knitr as the default for weaving Rnw files. I then installed MacTex basic from http://tug.org/mactex/morepackages.html to use for my LaTex which is a subset from TeXLive (2013).

So far so good.

I then open a new Sweave file, RStudio has recognized that I'm using some form of TeX program and then plug in the following:

\documentclass{article}

\begin{document}
\title{My first Knitr Output File}
\author{Oliver M. Fisher, MD}
\maketitle{BMI Category Crosstabulation}
<<>>=
attach(MIC1ELISANEW)
tab1=table(BMICat, Diagnosis)
tab1
summary(tab1)
@

<<>>=
tab1=table(MIC1ELISANEW$BMICat, MIC1ELISANEW$Diagnosis)
summary(tab1)
@

\end{document}

Hit "compile PDF" which gives me this log

grDevices::pdf.options(useDingbats = FALSE); require(knitr); opts_knit$set(concordance =TRUE);     knit('Example.Rnw')
Opening packet: knitr
Warning:
Packet ‘knitr’ was developed under R Version 3.0.2


processing file: Example.Rnw
  |.............                                                    |  20%
  ordinary text without R code

  |..........................                                       |  40%
  label: unnamed-chunk-1
  |.......................................                          |  60%
  ordinary text without R code

  |....................................................             |  80%
  label: unnamed-chunk-2
  |.................................................................| 100%
  ordinary text without R code


output file: Example.tex

[1] "Example.tex"
> 
> 
Running pdflatex on Example.tex...completed

Created PDF: ~/Documents/Australia/AMR : Gastro/Papers/Oliver/MIC1/Datasets/ELISA/Example.pdf

And opens a PDF, that looks the following ("nicht gefunden" means "not found" in English):

My first Knitr Output File

Oliver M. Fisher, MD February 3, 2014

BMI Category Crosstabulation

attach(MIC1ELISANEW)

Error:  Objekt ’MIC1ELISANEW’ nicht gefunden

tab1 = table(BMICat, Diagnosis)

Error:  Objekt ’BMICat’ nicht gefunden

tab1

Error:  Objekt ’tab1’ nicht gefunden

summary(tab1)

Error:  Objekt ’tab1’ nicht gefunden

tab1 = table(MIC1ELISANEW$BMICat, MIC1ELISANEW$Diagnosis)

Error:  Objekt ’MIC1ELISANEW’ nicht gefunden

summary(tab1)

Error:  Objekt ’tab1’ nicht gefunden

I've tried inputing the R code by either a) copy&pasting it from an R script file, copy&pasting it from the RStudio console and also sending it to the "source" by clicking on that icon in the RStudio history window. Nothing works. If anyone could tell me what I'm doing wrong, I'd be very grateful.

Thanks,

Oliver


baptiste auguie

unread,
Feb 3, 2014, 12:55:17 PM2/3/14
to OFish, knitr
attach() does not load data from text files, try ?read.table or ?load for Rdata files.

HTH,

baptiste


--
You received this message because you are subscribed to the Google Groups "knitr" group.
To unsubscribe from this group and stop receiving emails from it, send an email to knitr+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

OFish

unread,
Feb 3, 2014, 8:57:10 PM2/3/14
to kn...@googlegroups.com
Hi mate,

thanks for that input. I tried incorporating the load() or read.table() function into the .Rnw file, but it won't compute either. I just get the error message, that "XXX not found".

It's weird. I've put all the files into one place. But when compiling it doesn't access the specified file.

Any other ideas?

OFish

unread,
Feb 5, 2014, 7:09:38 PM2/5/14
to kn...@googlegroups.com
Ok guys, after getting some help on stackoverflow and playing around myself, I think I've found the solution....for any newbies like myself here's what you have to do, because knitr basically requires you to reproduce everything, you've done to get to your results and it allows you to work with relative paths, better than absolute paths.



\begin{document}
\title{My first Knitr Output File}
\author{Oliver M. Fisher, MD}
\maketitle{BMI Category Crosstabulation}


<<>>=
read.delim("~/relativepathtofile")
MIC1ELISANEW<-read.table("~/relativepathtofile", header=T)
@


<<>>= attach(MIC1ELISANEW) tab1=table(BMICat, Diagnosis) tab1 summary(tab1) @ <<>>= tab1=table(MIC1ELISANEW$BMICat, MIC1ELISANEW$Diagnosis) summary(tab1) @ \end{document}

This'll get it to work.

Cheers,

O.



On Monday, 3 February 2014 22:38:24 UTC+11, OFish wrote:

Michael Nelson

unread,
Feb 5, 2014, 8:26:56 PM2/5/14
to OFish, kn...@googlegroups.com
Hi Ofish,

I wrote the Stack overflow answer.

Please note,

read.delim("~/relativepathtofile")

does not assign the results to anything (therefore is useless).

My suggestion of `read.table`, was to prompt you to read in the data (the same way you would read it into an new R session)

In

MIC1ELISANEW<-read.table("~/relativepathtofile", header=T)

replace read.table with the appropriate "read" function (delim / csv whatever you used originally), and replace 'relativepathtofile' with
the path to the file which contains the data (relative to the directory in which the .rnw script is stored).


Michael


From: kn...@googlegroups.com [kn...@googlegroups.com] on behalf of OFish [ofis...@gmail.com]

Sent: Thursday, 6 February 2014 11:09 AM

To: kn...@googlegroups.com

Subject: [knitr] Re: Knitr PDF ok, but no content and error messages

\end{document}

Cheers,

O.

Thanks.

\end{document}


output file: Example.tex

Created PDF: ~/Documents/Australia/AMR : Gastro/Papers/Oliver/MIC1/Datasets/ELISA/Example.pdf

OFish

unread,
Feb 6, 2014, 6:42:10 PM2/6/14
to kn...@googlegroups.com, OFish
Hi Michael,

Thanks for your help, both here and on stack overflow.

the read.delim is what R states, when I load in the file and if I don't add it to the .Rnw file, it doesn't work.

Now that it's running smoothely, I'm more than happy! So thanks again.

What I also posted on stackoverflow - but the admin's took it out of my edited message, was that if I add <<include=FALSE>>= , as in:

\documentclass{article}
\usepackage[left=2cm,top=2cm,right=2cm,bottom=2cm]{geometry}
\begin{document}

\title{This is a title}

\author{Oliver M. Fisher, MD}
\maketitle

<<include=false>>=
this is my file <- read.delim("~/relative path to file")
this is my file <- read.table("~/relative path to file")
@

to the code chunk, knitr won't compile the PDF. I'm not sure if it could have to do with the TeX package I installed: I use MacTex basic from http://tug.org/mactex/morepackages.html which is a subset from TeXLive (2013). I'll try installing the whole LaTex package eventhough it's 2.5G and see if that helps.

Thanks for your assistance. You can't imagine how happy I am to finally be able to output all my stats into nice PDFs. The sink() option was just too little for me, especially when you need to send other people your stats.

Cheers,

Oliver

R. Michael Weylandt

unread,
Feb 6, 2014, 9:41:06 PM2/6/14
to OFish, kn...@googlegroups.com
On Thu, Feb 6, 2014 at 6:42 PM, OFish <ofis...@gmail.com> wrote:
> Hi Michael,
>
> Thanks for your help, both here and on stack overflow.
>
> the read.delim is what R states, when I load in the file and if I don't add
> it to the .Rnw file, it doesn't work.
>
> Now that it's running smoothely, I'm more than happy! So thanks again.
>
> What I also posted on stackoverflow - but the admin's took it out of my
> edited message, was that if I add <<include=FALSE>>= , as in:
>
> \documentclass{article}
> \usepackage[left=2cm,top=2cm,right=2cm,bottom=2cm]{geometry}
> \begin{document}
>
> \title{This is a title}
> \author{Oliver M. Fisher, MD}
> \maketitle
>
> <<include=false>>=
> this is my file <- read.delim("~/relative path to file")
> this is my file <- read.table("~/relative path to file")
> @

"false" is not the same as "FALSE" ... not sure if that's a true typo or not.

OFish

unread,
Feb 7, 2014, 12:30:14 AM2/7/14
to kn...@googlegroups.com, OFish
Hi Michael,

sorry it was a typo.

If include that command in the first chunk of knitr, it won't compute, I can however add <<include=FALSE>>= in chunks further down and it will be processed correctly.

Is there a bizarre reason for that?

Cheers,

O.

R. Michael Weylandt

unread,
Feb 17, 2014, 1:49:03 PM2/17/14
to OFish, kn...@googlegroups.com
Sorry Oliver,

Not much I can way without a minimal reproducible example.

Michael
Reply all
Reply to author
Forward
0 new messages