r:dprint:表格变更图像的大小

时间:2012-07-26 07:38:56

标签: image r format image-resizing knitr

我正在使用带有dprint的{​​{1}}包,主要是因为我可以突出显示我已经工作的表中的行,但是输出图像为脚注留下了相当大的空间,它占用了不必要的空间。

有没有摆脱它?

另外,因为我对dprint很新,如果有人有更好的想法/建议如何突出表格并使它们看起来漂亮而没有任何脚注......或者整理我的代码的方法会很棒!

Rmd文件代码的示例如下......

knitr

提前致谢!

2 个答案:

答案 0 :(得分: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 

这些是您生成的图像的尺寸,这些信息可以很容易地插入到一个函数中,以使您的图更好。

祝你好运!

答案 1 :(得分:0)

所以这是我的解决方案......有一些例子......

我刚刚复制并粘贴了我的Rmd文件,以演示如何使用它。

您应该只能将其复制并粘贴到空白的Rmd文件中,然后编织成HTML以查看结果......

理想情况下,我本来想要的是将它变成一个很好的整洁功能,而不是将它分成两个(即setup.table&amp; print.table)但是因为块中间选项不能像块一样改变由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
```