2017-10-05 23 views
3

Excelにはデータバーと呼ばれるこの機能があり、それぞれの長さのセル値に基づいて条件付き書式を設定できます。この関数は、formatterとcolor_barを使ってRの "formattable"を使って行うことができます。しかし、これはpdfで表示できないhtmlウィジェットです。R Markdown pdf部分的な色のセルの背景(データバー)

は、ここで私が試してみましたいくつかのオプションがあります:

  1. webshotは:ウィジェットのスクリーンショットを取り、その後、私はイメージとしてP​​DFファイルにインポートすることができます。

:私は条件付き書式に

  • KABLE +を追加することはできませんkableExtra - フォーマット可能また、非常に効率的ではないが、それは、ネストされたテーブル

    ため
  • xtable /パンダーを許可しないよう最善の選択肢ではありません

    これは私が最も成功したものです。このコード(how can xtable do cell coloring)から、私は条件付き書式設定を行うことができましたが、カラーバーオプションではなく、pdfでHTMLとラテックスのハイブリッドになるので、カラムの幅を変更したり、変更したりすることはできませんdoc。 https://tex.stackexchange.com/questions/81994/partially-coloring-cell-background-with-histograms

    は基本的に私はそれが基づいていますネストした表の機能+データバーを許可するテーブルを表示しますpdfドキュメントを持つことができるようにしたい:

    しかし、私が本当にやりたいことはこれですセルの値、およびテーブルの列幅を調整することができます。または、テーブルを1ページに収まるように縮小できます。

    これは私が今他のコードからの回答に基づいたものである: 私はそれが2番目のリンクに答えのようになりますようにlapply関数にLaTeXコードを挿入する方法を知りたい〜

    library(knitr) 
    library(tidyr) 
    library(kableExtra) 
    #options(knitr.table.format="latex") 
    data(mtcars) 
    tab =mtcars 
    tab$mpg<-tab$mpg/100 
    
    f <- function(x) cut(x, c(0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7,0.8,0.9,1.0), labels=c("white","green!10", "green!20", "green!30", "green!40", "green!50", "green!60", "green!70","green!80","green!90"), 
            include.lowest = FALSE, right = TRUE) 
    
    tab["mpg"] <- lapply(tab["mpg"], function(x) 
                 paste0("\\cellcolor{", f(x), "}", x)) 
    kable(tab) 
    
  • +0

    私はkableをxtableで置き換えることでコードを再試行しました。これにより、print行のscaleboxを使ってテーブル幅を再調整することができました。 "print(xtable(tab)、sanitize.text。 function = identity、scale = '0.75')。しかし、まだcolor_barを行う方法を探しています – Ship

    答えて

    1

    このようなきめ細かな質問を投稿していただきありがとうございます。 get_databar関数は新しい値を使用していずれかの列を置き換えるために使用され、インスタンス

    単にあなたのコメントを以下の
    > get_databar(1:5) 
    [1] "\\databar[white]{1}" "\\databar[green!30]{2}" "\\databar[green!50]{3}" 
    [4] "\\databar[green!70]{4}" "\\databar[green!90]{5}" 
    

    のために、私はそれだけで素晴らしいですと、Databarのhttps://tex.stackexchange.com/questions/81994/partially-coloring-cell-background-with-histogramsを適応、およびビット私のラテックスコーディングを超えています能力。私はデータバー機能の使用例を2つ作成してい

    two examples with bars

    スタンドアロンのSweave/knitrスクリプトが

    \documentclass{article} 
    \usepackage[table,dvipsnames]{xcolor}% http://ctan.org/pkg/xcolor 
    \usepackage[nomessages]{fp}% http://ctan.org/pkg/ 
    \begin{document} 
    <<load_libraries, echo = FALSE, eval = TRUE, results ="hide">>= 
    library(knitr) 
    library(xtable) 
    @ 
    <<get_databar, echo = FALSE, eval = TRUE, results ="hide">>= 
    #' @title get_databar creates labels for xtable 
    #' @description colors labels and assigns max_value to .GlobalEnv 
    #' @param values the vector of values to cut 
    #' @param color one color that is interpretable by the xcolor dvips 
    #' one of 68 standard colors known to dvips \link{https://en.wikibooks.org/wiki/LaTeX/Colors}, Default: 'green' 
    #' @param min_value min value in the vector, Default: NULL 
    #' @param max_value max value in the vector, Default: NULL 
    #' @param column_width the with of the colum to produce 
    #' @param transparent, do you want transparent labels for low values, Default: TRUE 
    #' @return A vector to replace orignial column 
    get_databar <- function(values, 
        color = "green", 
        min_value=NULL, 
        max_value=NULL, 
        column_width=10, 
        transparent=TRUE) 
    { 
        if (!is.numeric(values)) stop("values should be a numeric") 
        if (is.null(min_value)) min_value <- min(values,na.rm=TRUE)-diff(range(values, na.rm=TRUE))/10 
        if (is.null(max_value)) max_value <- max(values,na.rm=TRUE) 
        # assign max_value in .GlobalEnv 
        maxnum <<- max_value 
        # sequence of breaks 
        mybreaks <- seq(min_value, max_value, length.out=10) 
        if (transparent){ 
         cols <- c(paste0(color,"!",seq(10, 90, by=10))) 
         color_cut_factor <- cut(x=values, 
           breaks = mybreaks, 
           labels = cols, 
           include.lowest = FALSE, 
           right = TRUE) 
         color_cut <- as.character(color_cut_factor) 
        } else { 
         color_cut=rep(color, length(values)) 
        } 
        edited_values <- paste0("\\databar[", color_cut,"]{", values,"}") 
        return(as.character(edited_values)) 
    } 
    @ 
    <<test, echo = FALSE, eval = TRUE, results ="hide">>= 
    data(mtcars) 
    tab = mtcars 
    tab0<-tab[1:10,1:3] 
    tab2<-tab[1:10,1:3] 
    tab0[,1]<-get_databar(tab0[,1]) 
    tab2[,1]<-get_databar(tab2[,1], 
        color="BlueViolet", 
        min_value=0, 
        max_value=max(tab2[,1]), 
        transparent = FALSE) 
    print(xtable(tab0, 
         align = c("l","l","l","l"), 
         caption = "standard example"), 
        sanitize.text.function = identity, 
        file="table0_with_bar_colors.tex") 
    print(xtable(tab2, 
         align = c("l","l","l","l"), 
         caption = "example with dvips color BlueViolet and transparent = FALSE"), 
        sanitize.text.function = identity, 
        file="table2_with_bar_colors.tex") 
    @ 
    %-------------------------------- 
    % This is the new commands blocks 
    % The first \maxnum will read from R to get the maximum number in the table 
    % The second creates the databar command, with two parameters, the first default 
    % parameter is the color, set to green!25 
    %-------------------------------------- 
    \newcommand{\maxnum} 
    {% 
        \Sexpr{maxnum} 
    } 
    \newlength{\maxlen} 
    % databar[color]{value} 
    \newcommand{\databar}[2][green!25] 
    {% 
        \settowidth{\maxlen}{\maxnum}% 
        \addtolength{\maxlen}{\tabcolsep}% 
        \FPeval\result{round(#2/\maxnum:4)}% 
        \rlap{\color{#1}\hspace*{-.5\tabcolsep}\rule[-.05\ht\strutbox]{\result\maxlen}{.95\ht\strutbox}}% 
        \makebox[\dimexpr\maxlen-\tabcolsep][r]{#2}% 
    } 
    %-------------------------------------- 
    \input{table0_with_bar_colors.tex} 
    \input{table2_with_bar_colors.tex} 
    \end{document} 
    

    は、あなたがして、2つのテーブルを配置する必要がある場合、ここでI次のチャンクの後に、テーブルの\inputの前に次のコマンド(の代わりに\newcommand)を追加する必要があります。

    \renewcommand{\maxnum} 
    {% 
        \Sexpr{maxnum} 
    } 
    
    関連する問題