【问题标题】:r: dprint: size of image of table alterationr: dprint: 表格变更图像的大小
【发布时间】:2012-07-24 18:14:34
【问题描述】:

我正在使用带有 knitrdprint 包,主要是这样我可以突出显示表格中的行,我已经开始工作了,但是输出图像为脚注留下了相当大的空间,而且它正在占用增加不必要的空间。

有办法摆脱它吗?

另外,由于我对 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")

```

提前致谢!

【问题讨论】:

  • 也许应该更正这个标题。这不完全是关于“脚注删除”,因为那不是那个空间。其实和图/图大小有关。

标签: image r format image-resizing knitr


【解决方案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 

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

祝你好运!

【讨论】:

  • 嗯,底部看起来不错,但现在顶部有很大的空间......但是如果你在dev='jpeg'之前使用fig.height=4.5,你也可以摆脱顶部......我想知道是否有更优雅的答案,因为我的桌子尺寸变化很大,所以不得不一直玩这些宽度和高度有点烦人......一旦一种能够准确生成正确尺寸图像的编程方法是产生了,我可能只是把它包装成一个函数......
  • 感谢您迄今为止的帮助!
  • @h.l.m,没问题。正如我所提到的,我以前从未使用过这个包。不过,这似乎很有趣也很有用,所以如果我有时间,我会进一步探索。
  • @h.l.m,我忍不住多看几眼。看我帖子的更新。我认为有了这些信息,您应该能够将一个函数组合在一起来解决您的问题。
  • @mrdwad 嗯,越来越近了……你知道在运行dprint 函数之前如何解决吗?
【解决方案2】:

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

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

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

理想情况下,我希望将它变成一个很好的简洁功能,而不是将其分成两部分(即 setup.table 和 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
```

【讨论】:

    猜你喜欢
    • 2012-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-05
    • 2018-02-17
    • 1970-01-01
    • 2021-04-17
    • 1970-01-01
    相关资源
    最近更新 更多