78237

r: dprint: size of image of table alteration

Question:

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!

Answer1:

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

<ul><li>The start of your code chunk has defined the image width and height, which dprint seems to be trying to use. </li> <li>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 <em>both</em> height and width, but only the one that seems to make most sense, in this case, width).</li> </ul>

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!

Answer2:

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 <a href="https://stackoverflow.com/questions/11668904/how-to-change-knitr-options-mid-chunk" rel="nofollow">Yihui</a>, 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 ```

Recommend

  • Puppeteer opening chrome instance for each file at once
  • How to get an event from button in jquery mobile dynamic page
  • Why Open Redirect URLS?
  • $(“body”).ready() works but $(“body”).load() does not work?
  • How to resize image if the image is bigger than Textbox.But it not increase scale if smaller
  • It is possible use the same sql azure instance from two different cloud service of two different sub
  • Serve file to user over http via php
  • Microsoft Excel Pivot miscalculation in Sum for positive and negative numbers
  • How to merge keras sequential models with same input?
  • Prevent page break in text block with iText, XMLWorker
  • Retaining data after updating application
  • C# List of Panels
  • Creating PDF from TIFF image using iText
  • How to get data from **Realm database** using **date object**?
  • Hide HTML elements without javascript, only CSS
  • Can I read an iPhone beacon with Windows.Devices.Bluetooth.Advertisement.BluetoothLEManufacturerData
  • How to access culture data in globalize.js V1.0.0
  • List images(01.png) and descriptions(01.txt) from directory
  • Is it possible to open regedit and navigate to straight to a specific key using process.start?
  • Does it make sense to call System.gc() and Thread.sleep() when working on Bitmaps?
  • MongoDB in PHP using aggregate to group by _id is null not working
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Display issues when we change from one jquery mobile page to another in firefox
  • Problems to linebreak with an int in JLabel
  • Knitr HTML Loop - Some HTML output, some R output
  • To display the title for the current loaction in map in iphone
  • Delete MySQLi record without showing the id in the URL
  • R: gsub and capture
  • Unanticipated behavior
  • Comma separated Values
  • trying to dynamically update Highchart column chart but series undefined
  • Benchmarking RAM performance - UWP and C#
  • Trying to get generic when generic is not available
  • Android Google Maps API OnLocationChanged only called once
  • sending mail using smtp is too slow
  • Busy indicator not showing up in wpf window [duplicate]
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • Net Present Value in Excel for Grouped Recurring CF
  • How can I use threading to 'tick' a timer to be accessed by other threads?