[R] How to select the column header with \Sexpr{}
Ista Zahn
izahn at psych.rochester.edu
Tue Jul 13 16:13:39 CEST 2010
Hi Felipe,
The problem has nothing to do with Sweave or \Sexpr. The problem is
that by the time you call \Sexpr report is a matrix, and you cannot
access the column names of a matrix with names(). You need to use
colnames() or convert the matrix to a data.frame.
Perhaps a true useR can write R code in a Sweave file without checking
it, but for mere mortals it is best to evaluate the R code in an
interactive session to make sure it works before asking Sweave to
insert it into your .tex file. If you had tried to evaluate
names(report)[1] in an interactive session you would have discovered
your problem immediately.
Best,
Ista
On Tue, Jul 13, 2010 at 4:15 AM, Felipe Carrillo
<mazatlanmexico at yahoo.com> wrote:
> I had tried that earlier and didn't work either, I probably have \Sexpr in the
> wrong place. See example:
> Column one header gets blank:
>
> \documentclass[11pt]{article}
> \usepackage{longtable,verbatim,ctable}
> \usepackage{longtable,pdflscape}
> \usepackage{fmtcount,hyperref}
> \usepackage{fullpage}
> \title{United States}
> \begin{document}
> \setkeys{Gin}{width=1\textwidth}
> \maketitle
> <<echo=F,results=hide>>=
> report <- structure(list(Date = c("3/12/2010", "3/13/2010", "3/14/2010",
> "3/15/2010"), Run1 = c("33 (119 ? 119)", "n (0 ? 0)", "893 (110 ? 146)",
> "140 (111 ? 150)"), Run2 = c("33 (71 ? 71)", "n (0 ? 0)",
> "337 (67 ? 74)", "140 (68 ? 84)"), Run3 = c("890 (32 ? 47)",
> "n (0 ? 0)", "10,602 (32 ? 52)", "2,635 (34 ? 66)"), Run4 = c("0 ( ? )",
> "n (0 ? 0)", "0 ( ? )", "0 ( ? )"), Run4 = c("0 ( ? )", "n (0 ? 0)",
> "0 ( ? )", "0 ( ? )")), .Names = c("ID_Date", "Run1", "Run2",
> "Run3", "Run4", "Run5"), row.names = c(NA, 4L), class = "data.frame")
> require(stringr)
> report <- t(apply(report, 1, function(x) {str_replace(x, "\\?", "-")}))
> #report
> #latex(report,file="")
> @
> \begin{landscape}
> \begin{table}[!tbp]
> \begin{center}
> \begin{tabular}{llllll}\hline\hline
> \multicolumn{1}{c}{\Sexpr{names(report)[1]}} # Using \Sexpr here
> &\multicolumn{1}{c}{Run1}
> &\multicolumn{1}{c}{Run2}
> &\multicolumn{1}{c}{Run3}
> &\multicolumn{1}{c}{Run4}
> &\multicolumn{1}{c}{Run5}\tabularnewline
> \hline
> 1&3/12/2010&33 (119 ? 119)&33 (71 ? 71)&890 (32 ? 47)&0 ( ? )&0 ( ?
> )\tabularnewline
> 2&3/13/2010&n (0 ? 0)&n (0 ? 0)&n (0 ? 0)&n (0 ? 0)&n (0 ? 0)\tabularnewline
> 3&3/14/2010&893 (110 ? 146)&337 (67 ? 74)&10,602 (32 ? 52)&0 ( ? )&0 ( ?
> )\tabularnewline
> 4&3/15/2010&140 (111 ? 150)&140 (68 ? 84)&2,635 (34 ? 66)&0 ( ? )&0 ( ?
> )\tabularnewline
> \hline
> \end{tabular}
> \end{center}
> \end{table}
> \end{landscape}
> \end{document}
>
> Felipe D. Carrillo
> Supervisory Fishery Biologist
> Department of the Interior
> US Fish & Wildlife Service
> California, USA
>
>
>
> ----- Original Message ----
>> From: David Winsemius <dwinsemius at comcast.net>
>> To: Felipe Carrillo <mazatlanmexico at yahoo.com>
>> Cc: Duncan Murdoch <murdoch.duncan at gmail.com>; r-help at stat.math.ethz.ch
>> Sent: Mon, July 12, 2010 3:14:49 PM
>> Subject: Re: [R] How to select the column header with \Sexpr{}
>>
>>
>> On Jul 12, 2010, at 5:45 PM, Felipe Carrillo wrote:
>>
>> > Thanks for the quick reply Duncan.
>> > I don't think I have explained myself well, I have a dataset named "report"
>>and
>> > my column headers are run1,run2,run3,run4 and so on.
>> >
>> > I know how to access the data below those columns with \Sexpr{report[1,1]} &
>> > &\Sexpr{report[1,2]} and so on, but I can't access my column headers
>> > with \Sexpr{} because I can't find the way to reference run1,run2,run3 and
>>run4.
>> > Sorry if I am not explain myself really well.
>>
>> Wouldn't this just be:
>>
>> \Sexpr{names(report)} # ? or perhaps you want specific items in that vector?
>>
>> Sexpr{names(report)[1]}, Sexpr{names(report)[2]}, etc
>>
>> --David.
>> >
>> >
>> >
>> >
>> > ----- Original Message ----
>> >> From: Duncan Murdoch <murdoch.duncan at gmail.com>
>> >> To: Felipe Carrillo <mazatlanmexico at yahoo.com>
>> >> Cc: r-help at stat.math.ethz.ch
>> >> Sent: Mon, July 12, 2010 2:18:15 PM
>> >> Subject: Re: [R] How to select the column header with \Sexpr{}
>> >>
>> >> On 12/07/2010 5:10 PM, Felipe Carrillo wrote:
>> >>> Hi:
>> >>> Since I work with a few different fish runs my column headers change
>> >> everytime
>> >>> I start a new Year. I have been using \Sexpr{} for my row and columns and
>>now
>> >> I am trying to use with my report column headers. \Sexpr{1,1} is row 1
>>column 1,
>> >>> what can I use for headers? I tried \Sexpr{0,1} but sweave didn't like
>> >> it..Thanks in advance
>> >>> for any hints
>> >>>
>> >>
>> >> \Sexpr takes an R expression, and inserts the first element of the result
>>into
>> >> your text. Using just "0,1" (not including the quotes) is not a valid R
>> >> expression.
>> >>
>> >> You need to use paste() or some other function to construct the label you
>>want
>> >> to put in place, e.g. \Sexpr{paste(0,1,sep=",")} will give you "0,1".
>> >>
>> >> Duncan Murdoch
>> >>
>> >
>> >
>> >
>> >
>> > ______________________________________________
>> > R-help at r-project.org mailing list
>> > https://stat.ethz.ch/mailman/listinfo/r-help
>> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> > and provide commented, minimal, self-contained, reproducible code.
>>
>> David Winsemius, MD
>> West Hartford, CT
>>
>>
>
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org
More information about the R-help
mailing list