Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
671 views
in Technique[技术] by (71.8m points)

r - Longtable in a knitr (PDF) document: using xtable (or kable)

I am new to knitr and I have had some very basic latex knowledge in the past, so I googled already hoping to find a solution that was already posted somewhere. However, I was not able to solve my problem. I am hoping someone will be kind enough to provide help.

I have a data frame of 14 columns and many rows, let's say 60. Using the data I need to produce a PDF report in landscape layout and present this data frame as a table there.

The closest solution I found is here at tex.stackexchange.com: LaTex Longtable spanning several pages

I used some of the hints there. However, the table is not placed properly. The rightmost column(s) are cut-off at the right edge of the page. The table does not have "Continued" word at the end of the page. I am posting my code and the picture here.

I am after a solution to place the longtables properly on the page, if I am missing anything obvious please do not shoot :) I am really new to this.

documentclass[a4paper, landscape]{article}
usepackage[a4paper, margin=1in, hmarginratio=1:1, landscape]{geometry}
usepackage{longtable}
usepackage{graphicx}
usepackage{xcolor}
definecolor{myblue}{RGB}{24,57,121}
usepackage{lipsum}
usepackage{booktabs}
usepackage{colortbl}
usepackage{array}
usepackage{rotating}
usepackage{fancyhdr}
pagestyle{fancy}
fancyhead{}
fancyfoot{}

enewcommand{headrulewidth}{0.5pt}
setlengthheadheight{40mm} 
egin{document}

ewcolumntype{L}[1]{>{
aggedrightarraybackslash}p{#1}}

ewcolumntype{C}[1]{>{centeringarraybackslash}p{#1}}

ewcolumntype{R}[1]{>{
aggedleftarraybackslash}p{#1}}

enewcommand*{arraystretch}{1.0}
%
section{My Long Table}
%egin{center}
%egin{small}
%setlongtables
%egin{longtable}
<<echo=FALSE, eval=TRUE, results='asis'>>=
library(knitr)
library(xtable)
df <- data.frame(replicate(13, sample(1000000:9000000, 60,replace=TRUE)))
df$Sum <- rowSums(df)
totals <- colSums(df)
df <- rbind(df, totals)
names(df) <- c("Jan 2014", "Feb 2014", "Mar 2014", "Apr 2014", "May  2014", "Jun 2014", "Jul 2014",
            "Aug 2014", "Sep 2014", "Oct 2014", "Nov 2014", "Dec 2014", "Jan 2015", "Sum")
#
dtable <- xtable( x = df)
print ( dtable
          #, table.placement = "H"
          , table.placement = "!htp"
          , caption.placement = "top"
          , include.rownames = TRUE
          , include.colnames = TRUE
          , size = "footnotesize"
          , tabular.environment = 'longtable'
          , floating = FALSE
          #, scalebox = 0.7
          #, width = 0.8
          , add.to.row = list(pos = list(0),command = 
                  paste("\hline  \endfirsthead"  ,                          # First caption
                  "\caption[]{My Caption should be here} \label{tab:The Table} \\ \hline", # Additional captions
                  paste("&", names(df), collapse=" "),
                  "\\ \hline ",
                  "\endhead", 
                  "\hline \multicolumn{11}{r}{\textit{Continued}} \                    
                  \endfoot
                  \endlastfoot",collapse=" ")))
@
%end{longtable}
%end{small}

%end{center}
end{document}

enter image description here enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think I basically solved this problem in the dev version of kableExtra.

library(knitr)
library(kableExtra)
kable(df, "latex", longtable = T, booktabs = T) %>%
  kable_styling(latex_options = c("repeat_header"), font_size = 7) %>%
  landscape()

since longtable doesn't support resizebox, you cannot use the "scale_down" option in latex_options. I tried to reduce the font size to 7 and it looks pretty well.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...