Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

knitr and car::recode #413

Closed
mk9y opened this issue Oct 29, 2012 · 11 comments
Closed

knitr and car::recode #413

mk9y opened this issue Oct 29, 2012 · 11 comments
Labels
bug Bugs
Milestone

Comments

@mk9y
Copy link

mk9y commented Oct 29, 2012

The following code:

<<getHowell>>=
add <- read.table('http://www.uvm.edu/~dhowell/methods7/DataFiles/Add.dat', header = TRUE)
add$CaseNum <- factor(add$CaseNum)
library(car)
add$Gender <- Recode(add$Gender, " 1 = 'Male'; else = 'Female' ", as.factor.result = TRUE)
add$Repeat <- Recode(add$Repeat, " 1 = 'RepeatedGrade'; else = 'DidNotRepeatGrade' ", as.factor.result = TRUE)
add$EngL <- Recode(add$EngL, " 1 = 'CollegePrep'; 2 = 'General'; else = 'Remedial' ", as.factor.result = TRUE)
add$EngG <- Recode(add$EngG, " 4 = 'A'; 3 = 'B'; 2 = 'C'; 1 = 'D'; else = 'F' ", as.factor.result = TRUE)
add$SocProb <- Recode(add$SocProb, " 0 = 'No'; else = 'Yes' ", as.factor.result = TRUE)
add$Dropout.f <- Recode(add$Dropout, " 1 = 'Dropped'; else = 'DidNotDrop' ", as.factor.result = TRUE)
@

produces:

\begin{knitrout}
\definecolor{shadecolor}{rgb}{0.749, 0.937, 1}\color{fgcolor}\begin{kframe}
\begin{alltt}
> add <- \hlfunctioncall{read.table}(\hlstring{"howell.txt"}, header = TRUE)
> add$CaseNum <- \hlfunctioncall{factor}(add$CaseNum)
> \hlfunctioncall{library}(car)
> add$Gender <- \hlfunctioncall{recode}(add$Gender, \hlstring{" 1 = \hlstring{'Male'}; else = \hlstring{'Female'} "}, as.factor.result = TRUE)
> add$Repeat <- \hlfunctioncall{recode}(add$Repeat, \hlstring{" 1 = \hlstring{'RepeatedGrade'}; else = \hlstring{'DidNotRepeatGrade'} "}, 
       as.factor.result = TRUE)
> add$EngL <- \hlfunctioncall{recode}(add$EngL, \hlstring{" 1 = \hlstring{'CollegePrep'}; 2 = \hlstring{'General'}; else = \hlstring{'Remedial'} "}, 
       as.factor.result = TRUE)
> add$EngG <- \hlfunctioncall{recode}(add$EngG, \hlstring{" 4 = \hlstring{'A'}; 3 = \hlstring{'B'}; 2 = \hlstring{'C'}; 1 = \hlstring{'D'}; else = \hlstring{'F'} "}, 
       as.factor.result = TRUE)
> add$SocProb <- \hlfunctioncall{recode}(add$SocProb, \hlstring{" 0 = \hlstring{'No'}; else = \hlstring{'Yes'} "}, as.factor.result = TRUE)
> add$Dropout.f <- \hlfunctioncall{recode}(add$Dropout, \hlstring{" 1 = \hlstring{'Dropped'}; else = \hlstring{'DidNotDrop'} "}, 
       as.factor.result = TRUE)
\end{alltt}
\end{kframe}
\end{knitrout}

with throws the followeing error in the console:

! Undefined control sequence.
<argument> '
            Male'
l.1431 ...g{'Male'}; else = \hlstring{'Female'} "}
                                                  , as.factor.result = TRUE)
@yihui
Copy link
Owner

yihui commented Oct 29, 2012

I see the line number is 1431, so it must be a very long document. I cannot reproduce the error with the following minimal document:

\documentclass{article}
\begin{document}
<<getHowell>>=
add <- read.table('http://www.uvm.edu/~dhowell/methods7/DataFiles/Add.dat', header = TRUE)
add$CaseNum <- factor(add$CaseNum)
library(car)
add$Gender <- Recode(add$Gender, " 1 = 'Male'; else = 'Female' ", as.factor.result = TRUE)
add$Repeat <- Recode(add$Repeat, " 1 = 'RepeatedGrade'; else = 'DidNotRepeatGrade' ", as.factor.result = TRUE)
add$EngL <- Recode(add$EngL, " 1 = 'CollegePrep'; 2 = 'General'; else = 'Remedial' ", as.factor.result = TRUE)
add$EngG <- Recode(add$EngG, " 4 = 'A'; 3 = 'B'; 2 = 'C'; 1 = 'D'; else = 'F' ", as.factor.result = TRUE)
add$SocProb <- Recode(add$SocProb, " 0 = 'No'; else = 'Yes' ", as.factor.result = TRUE)
add$Dropout.f <- Recode(add$Dropout, " 1 = 'Dropped'; else = 'DidNotDrop' ", as.factor.result = TRUE)
@
\end{document}

Perhaps that is a LaTeX problem from elsewhere...

@mk9y
Copy link
Author

mk9y commented Oct 29, 2012

It is indeed a huge document, but the reason I thought I had found the culprit is that the problem goes away when I comment out this block of knitr-generated LaTeX.
In any event, thanks so much for the helpful hint. I was also unable to reproduce using your example. I'll check further.

On Oct 29, 2012, at 3:54 PM, Yihui Xie notifications@github.com wrote:

I see the line number is 1431, so it must be a very long document. I cannot reproduce the error with the following minimal document:

\documentclass{article}
\begin{document}
<>=
add <- read.table('http://www.uvm.edu/~dhowell/methods7/DataFiles/Add.dat', header = TRUE)
add$CaseNum <- factor(add$CaseNum)
library(car)
add$Gender <- Recode(add$Gender, " 1 = 'Male'; else = 'Female' ", as.factor.result = TRUE)
add$Repeat <- Recode(add$Repeat, " 1 = 'RepeatedGrade'; else = 'DidNotRepeatGrade' ", as.factor.result = TRUE)
add$EngL <- Recode(add$EngL, " 1 = 'CollegePrep'; 2 = 'General'; else = 'Remedial' ", as.factor.result = TRUE)
add$EngG <- Recode(add$EngG, " 4 = 'A'; 3 = 'B'; 2 = 'C'; 1 = 'D'; else = 'F' ", as.factor.result = TRUE)
add$SocProb <- Recode(add$SocProb, " 0 = 'No'; else = 'Yes' ", as.factor.result = TRUE)
add$Dropout.f <- Recode(add$Dropout, " 1 = 'Dropped'; else = 'DidNotDrop' ", as.factor.result = TRUE)
@
\end{document}
Perhaps that is a LaTeX problem from elsewhere...


Reply to this email directly or view it on GitHub.

@mk9y
Copy link
Author

mk9y commented Oct 29, 2012

The conflict is with \usepackage{lmodern}

The following fails:

\documentclass{article}
\usepackage{lmodern}
\begin{document}
<>=
add <- read.table('http://www.uvm.edu/~dhowell/methods7/DataFiles/Add.dat', header = TRUE)
add$CaseNum <- factor(add$CaseNum)
library(car)
add$Gender <- Recode(add$Gender, " 1 = 'Male'; else = 'Female' ", as.factor.result = TRUE)
add$Repeat <- Recode(add$Repeat, " 1 = 'RepeatedGrade'; else = 'DidNotRepeatGrade' ", as.factor.result = TRUE)
add$EngL <- Recode(add$EngL, " 1 = 'CollegePrep'; 2 = 'General'; else = 'Remedial' ", as.factor.result = TRUE)
add$EngG <- Recode(add$EngG, " 4 = 'A'; 3 = 'B'; 2 = 'C'; 1 = 'D'; else = 'F' ", as.factor.result = TRUE)
add$SocProb <- Recode(add$SocProb, " 0 = 'No'; else = 'Yes' ", as.factor.result = TRUE)
add$Dropout.f <- Recode(add$Dropout, " 1 = 'Dropped'; else = 'DidNotDrop' ", as.factor.result = TRUE)
@
\end{document}

A request: knitr() add a lot of LaTeX code in the preamble. How about writing a file with a different suffix that is included at the beginning of the .tex file.
And a question: as you saw, I'm writing a large document (with chapters). I would like to compile the Rnw one file at the time. Would a \knitrincludeonly{} command be possible? This would also insure that there are no inter-chapter dependencies.

@yihui
Copy link
Owner

yihui commented Oct 29, 2012

Thanks for digging out the culprit and the suggestions.

You can probably try opts_knit$set(self.contained = FALSE) so the large preamble goes to a file knitr.sty.

For the second question, I think you can use the set_parent() function in your chapters; see http://yihui.name/knitr/demo/child/

@yihui
Copy link
Owner

yihui commented Oct 30, 2012

\usepackage{lmodern} works fine for me with TeXLive 2012 under Ubuntu 12.10.

@mk9y
Copy link
Author

mk9y commented Oct 30, 2012

Strange. I keep my LaTeX updated weekly (OS X 10.8.2).

MK

On Oct 29, 2012, at 9:51 PM, Yihui Xie notifications@github.com wrote:

\usepackage{lmodern} works fine for me with TeXLive 2012 under Ubuntu 12.10.


Reply to this email directly or view it on GitHub.

@mk9y
Copy link
Author

mk9y commented Nov 1, 2012

I have another question. Can I put some of the knitr code into a boxedminipage (e.g., for exercises). The LaTeX compilation fails:

\documentclass{article}
\usepackage{boxedminipage}
\begin{document}
<>=
add <- read.table('http://www.uvm.edu/~dhowell/methods7/DataFiles/Add.dat', header = TRUE)
add$CaseNum <- factor(add$CaseNum)
library(car)
add$Gender <- Recode(add$Gender, " 1 = 'Male'; else = 'Female' ", as.factor.result = TRUE)
add$Repeat <- Recode(add$Repeat, " 1 = 'RepeatedGrade'; else = 'DidNotRepeatGrade' ", as.factor.result = TRUE)
add$EngL <- Recode(add$EngL, " 1 = 'CollegePrep'; 2 = 'General'; else = 'Remedial' ", as.factor.result = TRUE)
add$EngG <- Recode(add$EngG, " 4 = 'A'; 3 = 'B'; 2 = 'C'; 1 = 'D'; else = 'F' ", as.factor.result = TRUE)
add$SocProb <- Recode(add$SocProb, " 0 = 'No'; else = 'Yes' ", as.factor.result = TRUE)
add$Dropout.f <- Recode(add$Dropout, " 1 = 'Dropped'; else = 'DidNotDrop' ", as.factor.result = TRUE)
@
\boxedminipage{
<>=
add <- read.table('http://www.uvm.edu/~dhowell/methods7/DataFiles/Add.dat', header = TRUE)
add$CaseNum <- factor(add$CaseNum)
library(car)
add$Gender <- Recode(add$Gender, " 1 = 'Male'; else = 'Female' ", as.factor.result = TRUE)
add$Repeat <- Recode(add$Repeat, " 1 = 'RepeatedGrade'; else = 'DidNotRepeatGrade' ", as.factor.result = TRUE)
add$EngL <- Recode(add$EngL, " 1 = 'CollegePrep'; 2 = 'General'; else = 'Remedial' ", as.factor.result = TRUE)
add$EngG <- Recode(add$EngG, " 4 = 'A'; 3 = 'B'; 2 = 'C'; 1 = 'D'; else = 'F' ", as.factor.result = TRUE)
add$SocProb <- Recode(add$SocProb, " 0 = 'No'; else = 'Yes' ", as.factor.result = TRUE)
add$Dropout.f <- Recode(add$Dropout, " 1 = 'Dropped'; else = 'DidNotDrop' ", as.factor.result = TRUE)
@
}
\end{document}

On Oct 29, 2012, at 9:51 PM, Yihui Xie notifications@github.com wrote:

\usepackage{lmodern} works fine for me with TeXLive 2012 under Ubuntu 12.10.


Reply to this email directly or view it on GitHub.

@yihui
Copy link
Owner

yihui commented Nov 2, 2012

I guess it is a bad idea in general to put complicated content in \command{} due to the potential problem of } in the content, which can confuse LaTeX. The safer way is to use environments. I just tested boxedminipage environment and it works fine:

\begin{boxedminipage}{\linewidth}
<<>>=
# code here
@
\end{boxedminipage}

@yihui
Copy link
Owner

yihui commented Nov 13, 2012

Do you have further questions on this issue? If not, I'll close it. Thanks!

@mk9y
Copy link
Author

mk9y commented Nov 15, 2012

Please close.

MK

On Nov 13, 2012, at 1:02 AM, Yihui Xie notifications@github.com wrote:

Do you have further questions on this issue? If not, I'll close it. Thanks!


Reply to this email directly or view it on GitHub.

@yihui yihui closed this as completed Nov 15, 2012
@github-actions
Copy link

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Bugs
Projects
None yet
Development

No branches or pull requests

2 participants