3

I am using the dprint package with knitr , mainly so that I can highlight rows from a table, which I have got working, but the output image leaves a fairly large space for a footnote, and it is taking up unnecessary space.

Is there away to get rid of it?

Also since I am fairly new to dprint, if anybody has better ideas/suggestions as to how to highlight tables and make them look pretty without any footnotes... or ways to tidy up my code that would be great!

An example of the Rmd file code is below...

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

```

Thanks in advance!

h.l.m
  • 13,015
  • 22
  • 82
  • 169
  • Perhaps the title of this should be corrected. This isn't exactly about "footnote removal", since that's not what that space is. In fact, it is related to the figure/image size. – A5C1D2H2I1M1N2O1R2T1 Jul 26 '12 at 08:59

2 Answers2

1

I haven't used dprint before, but I see a couple of different things that might be causing problems:

  • The start of your code chunk has defined the image width and height, which dprint seems to be trying to use.
  • You are setting both fit.height and fit.width. I think only one of those is used (in other words, the resulting image isn't stretched to fit both height and width, but only the one that seems to make most sense, in this case, width).

After tinkering around for a minute, here's what I did that minimizes the footnote. However, I don't know if there is a more efficient way to do this.

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

```

Update

Playing around to determine the sizes of the images is a total drag. But, if you run the code in R and look at the structure of x, you'll find the following:

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

Or, simply:

x$cord2

# 3.420247 4.782485 

These are the dimensions of your resulting image, and this information can probably easily be plugged into a function to make your plots better.

Good luck!

A5C1D2H2I1M1N2O1R2T1
  • 190,393
  • 28
  • 405
  • 485
  • hmm, the bottom looks good but now there is a large space at the top...but if you use `fig.height=4.5` before the `dev='jpeg'`, you also get rid of the top...I wonder if there is a more elegant answer, as my table sizes change a lot and so having to play around with these widths and heights all the time is a bit annoying...once a programatic method that accurately produces the correct size images is produced, I will probably just wrap it into a function... – h.l.m Jul 26 '12 at 08:18
  • Thanks for your help so far though! – h.l.m Jul 26 '12 at 08:19
  • @h.l.m, no problem. As I mentioned, I've never used this package before. It seems interesting and useful though, so perhaps if I have some time, I'll explore further. – A5C1D2H2I1M1N2O1R2T1 Jul 26 '12 at 08:33
  • @h.l.m, I couldn't quite resist looking at this some more. Look at the update to my post. I think with this information, you should be able to put a function together to solve your problem. – A5C1D2H2I1M1N2O1R2T1 Jul 26 '12 at 08:54
  • @mrdwad hmm, getting closer...do you know how to work it out prior to running the `dprint` function? – h.l.m Jul 26 '12 at 09:43
  • I do not have much time looking into this problem and I have never used `dprint` either, but my gut feeling is that it might be related to this example: https://gist.github.com/2790922 where you can specify `fig.width` and `fig.height` from the calculation of R instead of making them fixed numbers. – Yihui Xie Jul 26 '12 at 15:20
  • @h.l.m, it seems like adding a `dev.off()` *before* your `x <- dprint...` line will suppress the drawing. Maybe that would help. – A5C1D2H2I1M1N2O1R2T1 Jul 26 '12 at 15:25
0

So here's my solution...with some examples...

I've just copied and pasted my Rmd file to demonstrate how to use it.

you should be able to just copy and paste it into a blank Rmd file and then knit to HTML to see the results...

Ideally what I would have liked would have been to make it all one nice neat function rather than splitting it up into two (i.e. setup.table & print.table) but since chunk options can't be changed mid chunk as suggested by Yihui, it had to be split up into two functions...

`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
```
Community
  • 1
  • 1
h.l.m
  • 13,015
  • 22
  • 82
  • 169