R packages

The htmlTable, forestplot, Gmisc, and Greg packages contain useful functions for graphics, tables etc that I’ve created/adapted for my own research. As of 2016-03-29 they’re all on CRAN.

The htmlTable-package

The htmlTable() has moved as of 1.0 to a separate package in December 2014. The Gmisc will depend on this new package and the change should have minimal impact on user experience. There are plenty of new features and a few API-changes that may be good to be aware of (although the old API will work for 1-2 years only with warnings):

  • rowname -> rnames
  • headings -> header
  • halign -> align.header
  • cgroup.just -> align.cgroup
  • rgroupCSSstyle -> css.rgroup
  • rgroupCSSseparator -> css.rgroup.sep
  • tspannerCSSstyle -> css.tspanner
  • tspannerCSSseparator -> css.tspanner.sep
  • rgroup.padding -> padding.rgroup
  • rowlabel.pos -> pos.rowlabel
  • caption.loc -> pos.caption
  • altcol -> col.rgroup
  • tableCSSclass -> css.table.class

The htmlTable-package is all about generating pretty tables, you can find some examples from my blog-posts below:

If you are interested in adding conditional formatting the condformat allows you to add this, developed by Sergio Oller. Works very similar to Excel’s conditional formatting and has borrowed from the awesome ggplot2 syntax.

The forestplot-package

The forestplot() has moved as of 1.0 to a separate package in December 2014. The Gmisc will have a deprecated call but the link between the package will be removed in the future (2017?). There are plenty of new features and a few API-changes that may be good to be aware of (although the old API will work for 1-2 years only with warnings):

  • legend.* -> legend_args and takes input from fpLegend()
  • confintNormalFn -> fn.ci_norm
  • confintSummaryFn -> fn.ci_sum
  • legendMarkerFn -> fn.legend
  • main -> Title

The package also contains a vignette that you may want to look into: vignette("forestplot")

The Gmisc-package

The Gmisc package 1.0 was releast in December 2014 on CRAN. Apart from minor API-changes there is the new mergeDesc() that I recommend looking into. Compiling a descriptive table has never been easier.

The Greg package

The package was finally released on CRAN 2016-03-29. It contains popular functions such as printCrudeAndAdjusted and also a bunch of other stuff that I use both for describing and manipulating models. See the Readme for details.

Development versions on GitHub

As CRAN limits the update frequency to 1-2 months you can download the most recent package from GitHub. You can download them using the devtools package and downloading the develop branch:

?View Code RSPLUS
# If you haven't installed devtools before, 
# now is the time by uncommenting next line:
# install.packages("devtools")
# Install the htmlTable package using:
devtools::install_github("gforge/htmlTable", ref="develop")
# Install the forestplot package using:
devtools::install_github("gforge/forestplot", ref="develop")
# Install the Gmisc package using:
devtools::install_github("gforge/Gmisc", ref="develop")
# Install the Greg package using:
devtools::install_github("gforge/Greg", ref="develop")

The comorbidities.icd10 package (not yet on CRAN)

As I’ve worked a lot with calculating comorbidities from ICD codes I’ve adapted the previous comorbidities package by Paul Gerrard. It has been thoroughly tested and is reasonably fast but I haven’t yet released it on CRAN. You can find it on GitHub.

Please help out

Maintaining and updating these packages takes a lot of time so any help is always appreciated. Use GitHub for filing issues, forking and doing pull requests. By filing issues everyone benefits from your questions.

Some of the functionality that I’ve blogged about:

  • getDescriptionsStatsBy – for generating a table 1 this is a very useful function that makes this much easier.
  • transitionPlot – for generating a transition plot
  • bezierArrowSmpl/bezierArrowSmplGradient – nicer alternatives to the default bezier arrow.
  • printCrudeAndAdjusted – a useful function for generating a table with a models crude and adjusted estimates.
  • plotHR – this is based upon r-forge Reinhard Seifert’s plotHR function]. The function allows multiple models to be plotted on top of eachother, when used together with transparency (the alpha channel of the rgb) it is very convenient for comparing different subgroups, time-spans or adjusted/unadjusted regressions.

I hope you find the packages as useful as I do. And in case you were wondering – Yes, the Gmisc name was inspired by Frank Harrell’s Hmisc-package.


Flattr this!

22 Responses to R packages

  1. Fr. says:

    Unfortunately, the current installation code does fail on Macs: here’s its output on R 3.0.0.

    • Max Gordon says:

      Hmm… seems to be some issue with the rms-package. Have you tried installing without the dependencies?

      • Fr. says:

        It works if you install “rms” separately:

        reps = c("http://ftp.sunet.se/pub/lang/CRAN",
        install.packages("Gmisc", repos = reps, dependencies = FALSE, type = "source")

  2. Pingback: Some Results | Shige's Results

  3. Peter says:


    I am currently playing around with your transition plots and I like it very much.

    Features missing I think are options for setting headers and subtitles as well as enabling the user to give all boxes individual texts. I tried the latter in order to also give an information about reaction times in a pre-post-measurement design, but now I get the error:

    Error: could not find function “getGridVal”

    while I have a help file available on that function, it is neither in my workspace after loading Gmisc, nor can I find in your Gmisc github repository.

    What happended here?

    • Max Gordon says:

      The latest version allows for creating your own layout around the plot, just push a grid viewport(), plot using the newpage=FALSE into that viewport. Also, see if you still get the error after setting the newpage=TRUE – there may be some old plot info that causes issues. I’m planning on adding the header option, unfortunately I haven’t had time to implement it yet but you can have a look at the forestplot2() that has the header option working in a similar fashion.

      You can put any text you want in your boxes, I’m currently writing an article on Charnley classification A, B and C before and one year after surgery. In the transitionPlot() call I simply have:

      box_txt=cbind(c("Charnley\nclass\nA", "B", "C\nPre-op."), c("Charnley\nclass\nA", "B", "C\nPost-op."))

  4. Abhinaba Roy says:

    Hi Max,

    I really liked ‘transitionPlot’ function in Gmisc package.
    However I would like to know if we can have a transition plot like
    A >> B >> C

    • Max Gordon says:

      Hi Abhinaba,

      I’ve had this request posted previously but I haven’t had the time to implement it yet. It is not incredibly difficult, but it would require some rethinking. If you want to try to adapt the code you are welcome to write a suggestion for the package. Just fork the Github-repository and write a suggestion. You probably don’t need to get down to the details as the major sections are ready-to-use building blocks. I will try to help you the best I can.


  5. Jay Rotella says:

    Thanks for all your great efforts on your packages and tutorials. They’re really helpful. However, when I try to run the last R code chunk, I get an error that reads as below and halts execution:

    Error in missing(xlab) : ‘missing’ can only be used for arguments
    Calls: … withCallingHandlers -> withVisible -> eval -> eval -> forestplotRegrObj
    In addition: Warning message:
    In fix_options(params) :
    You must not set both chunk options out.width and fig.retina

    • Max Gordon says:

      Ok, fixed it. In the version 1.0 I’m trying to make the functions cleaner and I happened to overdo the cleaning. Re-install from Github and see if it works better now.

  6. Aarne says:

    Hi Max,
    this package is simply great and I am more than happy for the possibilities it offers to me to turn our data analysis more reproducible and effective by it.
    However, working on it I have one point. I tried to adapt the script for comparing three groups and just replacing the wilcox.test by the kruskal.test works out perfectly.
    But doing the same with the fisher.test – i.d. replacing it by the chisq.test, brings up the error code:
    Error in getDescriptionStatsBy(parameter, group, :
    could not find function “prDescGetAndValidateDefaultRef”
    Looking through the code I found the call for prDescGetAndValidateDefaultRef (line 119), but I couldn’t find the definition for the function.
    Could you help me?
    Best Wishes Aarne

  7. Jeff says:


    let me thank you for the forestplot library. It is immensely helpful in creating some logistic regression/Odds ratio plots we needed for publication. That being said, I’m am *very new* to R, and for the life of me cannot figure out how to re-engineer this to change the line style. I’m sure the editor is going to want this in black and white. I have symbols separating my groups (as in your sweden/denmark example from another post), and have changed the colors to Black/Grey to try and differentiate the groups further. But differentiating by line type (say solid vs. dashed) would be great

    But I am simply too new to R to sensibly figure this out. Any possibility of a brief post on how to change the line style itself?

    Again, thank you for a great package.

    • Max Gordon says:

      Just added this feature to the development version run: devtools::install_github("gforge/forestplot", ref="develop") The line types can be set the same way as any other line-specific parameter through lty.ci. See the vignette for an example.

      I’m not entirely satisfied regarding how to name the argument for the T vertices that mark the ends of the confidence interval, let me know if you have a better naming suggestion. Due to the dashed nature of the line I believe that this necessary in order to avoid any confusion regarding exact line length.

  8. David A says:

    Hi Max,

    This is great and extremely helpful in my Biostatistics homework, thank you very much. I’m just beginning to learn R.

    I am having an issue with the printCrudeAndAdjustedModel command. It’s giving me HTML output rather than the table. Is there some way I can correct this?

  9. Sascha says:

    Hi Max,

    thanks for your valuable contributions! In Gmisc, can you give me hints on how to adapt the style of arrowheads (I basically want no heads, having arrows going in and out just straight)? Thanks!

    • Max Gordon says:

      Hi Sascha,

      not sure I follow exactly what you want. If you want plain arrows you may be better off with simple grid::polygonGrob implementation than adapting my arrows. The core functionality in the arrows is the Bezier spline that allows you to have a flexible, smooth line. The core idea is to have a center spline and then simply do an offset of x mm in 90 degree to the current segments direction. There are a few things that get complicated (1) if the line is heavily curved then the offset create an inner loop creating unexpected artifacts (2) the arrow is straight and therefore the line mus be shortened in order to compensate for this. If the attaching line isn’t a direct continuation of the arrow it looks rather weird in my opinion. Unfortunately reducing the line forces a recalculation of the spline points which has been a trial and error process where I’ve tested different adaptations to see how close the line would actually follow the original line.

      If you still feel that this is worth-while then you should have a look at the Gmisc:::getBezierAdj4Arrw function and try to generalize it so that you add an arrow at both ends. Under tests/visual_tests/test-bezier-adjustment.R you can find some of the tests that I used when developing the adjustment. The function generates two lines, one left and one right line, that constitute the two borders that together make up the boundaries of the arrow. This is done in the Gmisc:::calculateLinesAndArrow function that you will have to generalize so that it can handle alternative arrow specifications.

      If you get it to work, don’t hesitate to add a pull request into the repository. Good luck!

      • Sascha says:

        Hi Max,

        thanks a lot for your response. However, I got the impression I couldn’t describe my intention properly, which is only about the arrow_head_ instead of the entire arrow (I appreciate the twosided bezier arrows very much). I hope the linked picture clarifies what I desire: http://oc.sascha-holzhauer.de/index.php/s/LmOH2NEj8rlBsj9

        PS: As you see I extended the code for multiple transitions. Unfortuntely, I based my code on Gmisc version 1.0.2 and missed to send a pull request promptly. I hope I find resources to integrate my changes in the current codebase soon.

        • Max Gordon says:

          A pity that you spent time extending the code for multiple transitions since I’ve already done that. You should now use the TransitionClass, I should post the vignette on my blog but I’ve been too busy.

          Substituting the arrow to a straight line is basically the same set of functions although you don’t need to figure out how to change the Bezier spline. The issue you will have in your example is that not all lines come in at a straight angle and you will have to us the Gmisc:::align2Axis on the arrow side. The devil is in the details…

          • Sascha says:

            Ah, ok. Looks like you refactored a lot. Thanks for linking the vignette. A few remarks: the link to pacakges diagram seems wrong; formating in ‘Basic use’ seems corrupted (regarding subheaders); a continouse scale for male/female is rather funny…

            Another question: have you thought about arrow widths proportional to box height, i.e. if a flow is 20% of a box’ population the arrow width should be a fifth of box height?

  10. Max Gordon says:

    Thanks for the bug report. Male/Female is the proportion that transitions… perhaps not the most intuitive example.

    Check out the lwd_prop_type argument. It gives you the options of choosing algorithm for arrow width.

Leave a Reply