3

私は主に、作業中のテーブルの行を強調表示できるようにdprintパッケージを使用していますknitrが、出力画像は脚注用にかなり大きなスペースを残しており、不要なスペースを占めています。

それを取り除くために離れていますか?

また、私は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")

```

前もって感謝します!

4

2 に答える 2

1

私はこれまで使用dprintしたことがありませんが、問題を引き起こしている可能性のあるいくつかの異なることがわかります。

  • dprintコードチャンクの先頭で、使用しようとしているように見える画像の幅と高さが定義されています。
  • との両方fit.heightを設定していfit.widthます。そのうちの1つだけが使用されていると思います(つまり、結果の画像は高さと幅の両方に合うように引き伸ばされていませんが、最も意味があると思われるもの、この場合は幅だけです)。

少しいじった後、脚注を最小限に抑えるために私がしたことは次のとおりです。ただし、これを行うためのより効率的な方法があるかどうかはわかりません。

```{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 

これらは結果の画像の寸法であり、この情報はおそらく関数に簡単にプラグインしてプロットを改善することができます。

幸運を!

于 2012-07-26T08:06:10.763 に答える
0

これが私の解決策です...いくつかの例を示します...

Rmdファイルをコピーして貼り付け、使用方法を示しました。

コピーして空のRmdファイルに貼り付け、HTMLに編んで結果を確認できるはずです...

理想的には、2つに分割するのではなく、すべて1つの優れた関数にすることでしたが(つまり、setup.tableとprint.table)、Yihuiが提案するように、チャンクオプションをチャンクの途中で変更することはできませ。それは2つの機能に分割されなければなりませんでした...

`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-07-27T01:59:10.303 に答える