2012-02-16 15 views
5

में आर ftable() आउटपुट के रंगीन/हाइलाइट मानों को रंगना/हाइलाइट करना मैं एक वर्णनात्मक रिपोर्ट के लिए ftable() क्रॉसस्टैबल्स उत्पन्न कर रहा हूं। उदाहरण:knitr/स्वीवे रैपॉर्ट्स

   AUS BEL BUL EST FRA GEO GER HUN ITA NET NOR ROM RUS 

30- primary 0.06 0.03 0.07 0.03 0.02 0.03 0.03 0.02 0.05 0.03 0.05 0.04 0.02 
    secondary 0.30 0.09 0.16 0.10 0.10 0.14 0.10 0.16 0.11 0.08 0.08 0.09 0.11 
    tertiary 0.05 0.07 0.04 0.05 0.07 0.06 0.02 0.04 0.02 0.05 0.06 0.02 0.09 
30+ primary 0.07 0.16 0.12 0.07 0.16 0.03 0.05 0.11 0.35 0.21 0.09 0.17 0.03 
    secondary 0.40 0.20 0.30 0.29 0.25 0.35 0.35 0.34 0.27 0.20 0.27 0.34 0.26 
    tertiary 0.13 0.23 0.13 0.18 0.17 0.17 0.18 0.09 0.09 0.23 0.23 0.06 0.24 
60+ primary 0.00 0.12 0.10 0.13 0.14 0.07 0.05 0.12 0.09 0.11 0.06 0.19 0.12 
    secondary 0.00 0.05 0.05 0.08 0.06 0.10 0.14 0.09 0.02 0.04 0.11 0.07 0.06 
    tertiary 0.00 0.05 0.03 0.06 0.03 0.04 0.07 0.03 0.01 0.05 0.06 0.02 0.07 

मैं एक समारोह है कि ftable() या table() उत्पादन ले सकता है, और highligh मानों पंक्ति-मतलब से विचलित रहा हूँ, या मूल्यों का पाठ करता है, उदा करने के लिए एक समग्र ढाल आवंटित 0-100% से मूल्य लाल से हरे रंग के होते हैं।

आउटपुट अब knitr के माध्यम से संसाधित किया गया है, लेकिन मुझे यकीन नहीं है कि टूलचैन में किस बिंदु पर मैं हस्तक्षेप कर सकता हूं और मूल्यों के सापेक्ष आकार के आधार पर रंग जोड़ सकता हूं।

+0

यदि आप यहां अच्छे प्रतिक्रिया प्राप्त नहीं करते हैं तो आप इस प्रश्न को टीएक्स/लाटेक्स एक्सचेंज साइट पर भी पोस्ट कर सकते हैं। Hmisc :: लेटेक्स उदाहरण के लिए –

उत्तर

5

Hmisc पैकेज में आप latex फ़ंक्शन का उपयोग कर सकते हैं।

# Example shamelessly copied from http://www.karlin.mff.cuni.cz/~kulich/vyuka/Rdoc/harrell-R-latex.pdf 
cat(' 
    \\documentclass{article} 
    \\usepackage[table]{xcolor} 
    \\begin{document} 
    <<results=tex>>= 
    library(Hmisc) 
    d <- head(iris) 
    cellTex <- matrix(rep("", nrow(d) * ncol(d)), nrow=nrow(d)) 
    cellTex[2,2] <- "cellcolor{red}" 
    cellTex[2,3] <- "color{red}" 
    cellTex[5,1] <- "rowcolor{yellow}" 
    latex(d, file = "", cellTexCmds = cellTex, rowname=NULL) 
    @ 
    \\end{document}', 
    file="tmp.Rnw") 
Sweave("tmp.Rnw") 
library(utils) 
texi2pdf("tmp.tex") 
+1

1+। आपके पृष्ठों से प्राप्त उपयोगी सामग्री की संख्या को देखते हुए, विन्सेंट, मुझे लगता है कि आपको किसी भी लापरवाह प्रतिलिपि पर एक मुफ्त पास मिलता है। –

2

आर ऑब्जेक्ट्स से लेटेक्स टेबल जेनरेट करने के लिए, आप xtable पैकेज का उपयोग कर सकते हैं। यह available on CRAN है, दस्तावेज़ीकरण पर एक नज़र डालें। तालिका में रंग प्राप्त करने के लिए, color लेटेक्स पैकेज का उपयोग करें। कुछ उदाहरण कोड:

library(xtable) 
n = 100 
cat_country = c("NL","BE","HU") 
cat_prim = c("primary","secondary","tertiary") 
dat = data.frame(country = sample(cat_country, n, replace = TRUE), 
       prim = sample(cat_prim, n, replace = TRUE)) 
ftable_dat = ftable(dat) 

## Make latex table: 
latex_table = xtable(as.table(ftable_dat)) 

आप जो चाहते हैं उसे प्राप्त करने के लिए मैंने निम्नलिखित हैक (बदसूरत एक) बनाया है। चाल xtable वस्तु प्रिंट और करने के लिए है संपादित की तुलना में है कि:

latex_table = within(latex_table, { 
# browser() 
    primary = ifelse(primary > 12, sprintf("\\textbf{%s}", primary), primary) 
    #primary = sub("\\{", "{", primary) 
}) 
printed_table = print(latex_table) 
printed_table = sub("backslash", "\\", printed_table) 
printed_table = sub("\\\\}", "}", printed_table) 
printed_table = sub("\\\\\\{", "{", printed_table) 
printed_table = sub("\\$", "\\", printed_table) 
printed_table = sub("\\$", "\\", printed_table) 
cat(printed_table) 

कौन सा की ओर जाता है: \ अंत \ अंत {केंद्र} \ अंत {सारणीबद्ध}

% latex table generated in R 2.14.1 by xtable 1.6-0 package 
% Thu Feb 16 13:10:55 2012 
\begin{table}[ht] 
\begin{center} 
\begin{tabular}{rrrr} 
    \hline 
& primary & secondary & tertiary \\ 
    \hline 
BE & 10 & 5 & 11 \\ 
    HU & \textbf{13} & 13 & 8 \\ 
    NL & 11 & 17 & 12 \\ 
    \hline 

{TABLE}

यह उदाहरण प्राथमिक श्रेणी बोल्ड में एक संख्या बनाता है, लेकिन यह आसानी से रंगीनकरण के लिए काम कर सकता है। शायद किसी और के पास एक और अधिक सुरुचिपूर्ण समाधान है?