2012-07-26 83 views
3

我使用的dprint包與knitr,主要是爲了能突出顯示錶格中的行,我已經工作,但輸出圖像留下了相當大的空間腳註,它佔用了不必要的空間。r:dprint:表格更改的圖像大小

有沒有擺脫它?

此外,由於我對dprint相當陌生,如果有人對如何突出顯示錶並使它們看起來漂亮而沒有任何腳註有更好的想法/建議......或者整理我的代碼的方法將會非常棒!

的RMD文件代碼的下面是一個例子...

```{r fig.height=10, fig.width=10, dev='jpeg'} 
library("dprint") 
k <- data.frame(matrix(1:100, 10,10)) 
CBs <- style(frmt.bdy=frmt(fontfamily="HersheySans"), frmt.tbl=frmt(bty="o", lwd=1), 
     frmt.col=frmt(fontfamily="HersheySans", bg="khaki", fontface="bold", lwd=2, bty="_"), 
     frmt.grp=frmt(fontfamily="HersheySans",bg="khaki", fontface="bold"), 
     frmt.main=frmt(fontfamily="HersheySans", fontface="bold", fontsize=12), 
     frmt.ftn=frmt(fontfamily="HersheySans"), 
     justify="right", tbl.buf=0) 

x <- dprint(~., data=k,footnote=NA, pg.dim=c(10,10), margins=c(0.2,0.2,0.2,0.2), 
       style=CBs, row.hl=row.hl(which(k[,1]==5), col='red'), 
       fit.width=TRUE, fit.height=TRUE, 
       showmargins=TRUE, newpage=TRUE, main="TABLE TITLE") 

``` 

提前感謝!

+0

也許應該糾正這個標題。這不完全是關於「腳註移除」,因爲這不是那個空間。實際上,它與圖形/圖像大小有關。 – A5C1D2H2I1M1N2O1R2T1 2012-07-26 08:59:38

回答

0

因此,這裏是我的解決方案......一些例子...

我只是複製和粘貼我的RMD文件來演示如何使用它。

,你應該能夠把剛纔複製並粘貼到空白的RMD文件,然後編織成HTML看到的結果...

理想是什麼我也希望會一直使這一切一個美好整潔的功能,而不是將它分成兩部分(即setup.table & print.table),但由於chunk選項不能像Yihui建議的那樣在中間區塊中更改,所以它必須分成兩個功能......

`dprint` + `knitr` Examples to create table images 
=========== 

```{r} 
library(dprint) 
# creating the sytle object to be used 
CBs <- style(frmt.bdy=frmt(fontfamily="HersheySans"), 
      frmt.tbl=frmt(bty="o", lwd=1), 
     frmt.col=frmt(fontfamily="HersheySans", bg="khaki", 
         fontface="bold", lwd=2, bty="_"), 
     frmt.grp=frmt(fontfamily="HersheySans",bg="khaki", 
         fontface="bold"), 
     frmt.main=frmt(fontfamily="HersheySans", fontface="bold", 
         fontsize=12), 
     frmt.ftn=frmt(fontfamily="HersheySans"), 
     justify="right", tbl.buf=0) 

# creating a setup function to setup printing a table (will probably put this function into my .Rprofile file) 
setup.table <- function(df,width=10, style.obj='CBs'){ 
    require(dprint) 
    table.style <- get(style.obj) 
    a <- tbl.struct(~., df) 
    b <- char.dim(a, style=table.style) 
    p <- pagelayout(dtype = "rgraphics", pg.dim = NULL, margins = NULL) 
    f <- size.simp(a[[1]], char.dim.obj=b, loc.y=0, pagelayout=p) 
    # now to work out the natural table width to height ratio (w.2.h.r) GIVEN the style 
    w.2.h.r <- as.numeric(f$tbl.width/(f$tbl.height +b$linespace.col+ b$linespace.main)) 
    height <- width/w.2.h.r 

    table.width <- width 
    table.height <- height 

    # Setting chunk options to have right fig dimensions for the next chunk 
    opts_chunk$set('fig.width'=as.numeric(width+0.1)) 
    opts_chunk$set('fig.height'=as.numeric(height+0.1)) 

    # assigning relevant variables to be used when printing 
    assign("table.width",table.width, envir=.GlobalEnv) 
    assign("table.height",table.height, envir=.GlobalEnv) 
    assign("table.style", table.style, envir=.GlobalEnv) 
} 

# function to print the table (will probably put this function into my .Rprofile file as well) 
print.table <- function(df, row.2.hl='2012-04-30', colour='lightblue',...) { 
    x <-dprint(~., data=df, style=table.style, pg.dim=c(table.width,table.height), ..., newpage=TRUE,fit.width=TRUE, row.hl=row.hl(which(df[,1]==row.2.hl), col=colour)) 
} 
``` 

```{r} 
# Giving it a go! 
# Setting up two differnt size tables 
small.df <- data.frame(matrix(1:100, 10,10)) 
big.df <- data.frame(matrix(1:800,40,20)) 
``` 


```{r} 
# Using the created setup.table function 
setup.table(df=small.df, width=10, style.obj='CBs') 
``` 

```{r} 
# Using the print.table function 
print.table(small.df,4,'lightblue',main='table title string') # highlighting row 4 
``` 

```{r} 
setup.table(big.df,13,'CBs') # now setting up a large table 
``` 

```{r} 
print.table(big.df,38,'orange', main='the big table!') # highlighting row 38 in orange 
``` 

```{r} 
d <- style() # the default style this time will be used 
setup.table(big.df,15,'d') 
``` 

```{r} 
print.table(big.df, 23, 'indianred1') # this time higlihting row 23 
``` 
1

我以前沒有使用過dprint,但我看到了幾個不同的東西,可能會導致問題:

  • 代碼塊的開始定義了圖像的寬度和高度,這dprint似乎嘗試使用。
  • 您正在設置fit.heightfit.width。我認爲只有其中一個被使用(換句話說,最終的圖像沒有被拉伸到適合兩個的高度和寬度,但只有最有意義的一個,在這種情況下,寬度)。

經過一段時間的修補後,這是我做的最小化腳註。但是,我不知道是否有更有效的方法來做到這一點。

```{r dev='jpeg'} 
library("dprint") 
k <- data.frame(matrix(1:100, 10,10)) 
CBs <- style(frmt.bdy=frmt(fontfamily="HersheySans"), 
      frmt.tbl=frmt(bty="o", lwd=1), 
     frmt.col=frmt(fontfamily="HersheySans", bg="khaki", 
         fontface="bold", lwd=2, bty="_"), 
     frmt.grp=frmt(fontfamily="HersheySans",bg="khaki", 
         fontface="bold"), 
     frmt.main=frmt(fontfamily="HersheySans", fontface="bold", 
         fontsize=12), 
     frmt.ftn=frmt(fontfamily="HersheySans"), 
     justify="right", tbl.buf=0) 

x <- dprint(~., data=k, style=CBs, pg.dim = c(7, 4.5), 
      showmargins=TRUE, newpage=TRUE, 
      main="TABLE TITLE", fit.width=TRUE) 

``` 

更新

周圍玩,確定圖像的大小是一個總阻力。但是,如果R中運行代碼,並看看x結構,你會發現如下:

str(x) 
# List of 3 
# $ cord1 : num [1:2] 0.2 6.8 
# $ cord2 : Named num [1:2] 3.42 4.78 
# ..- attr(*, "names")= chr [1:2] "" "" 
# $ pagenum: num 2 

或者,乾脆:

x$cord2 

# 3.420247 4.782485 

這些都是你造成圖像的尺寸,而且這些信息可能很容易被插入到一個函數中,使你的情節更好。

祝你好運!

+0

嗯,底部看起來不錯,但現在頂部有一個很大的空間......但是如果你在'dev ='jpeg''之前使用'fig.height = 4.5',你也可以擺脫頂部.. 。我不知道是否有更優雅的答案,因爲我的桌子尺寸變化很大,所以不得不一直玩這些寬度和高度有點煩人......一旦一個準確生成正確尺寸圖像的程序化方法是生產,我可能會把它包裝成一個功能... – 2012-07-26 08:18:06

+0

雖然迄今爲止感謝您的幫助! – 2012-07-26 08:19:45

+0

@ h.l.m,沒問題。正如我所提到的,我以前從未使用過這個軟件包。雖然這看起來很有趣也很有用,但如果我有一些時間,我會進一步探索。 – A5C1D2H2I1M1N2O1R2T1 2012-07-26 08:33:09