2012-07-26 78 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