nbarrowman / vtree

An R package for calculating and drawing variable trees
https://nbarrowman.github.io/vtree
75 stars 6 forks source link

How do I print a vtree in RStudio using print method #3

Closed kishorerathi closed 5 years ago

kishorerathi commented 5 years ago

Hello

I have generated a list of vtree objects using lapply and would like to render them one after another in RStudio

xx = lapply(1:3, function(i) vtree(FakeData,"Severity Sex"))
lapply(1:3, function(i) print(xx[[i]]))

The code doesn't render anything in RStudio.

How do I print vtree objects programatically

regards

Kishore

nbarrowman commented 5 years ago

Are you using these commands in the Console window of RStudio? When I do that the Viewer pane shows me the variable tree vtree(FakeData,"Severity Sex"). (It's actually there 3 times, which I see when I use the arrows at the top of the Viewer pane to navigate.)

However, is it possible you're calling this from code chunks in R Markdown? If you are, first try running this example (which generates three different variable trees) directly in the Console window:

xx = lapply(1:3, function(i) vtree(FakeData,names(FakeData)[i+1]))
lapply(1:3, function(i) print(xx[[i]]))
kishorerathi commented 5 years ago

Thanks for the quick reply. Appreciate it very much.

I am running the code in code chunk. It works fine in R Console and you can see the output in viewer window.

But from code chunk, nothing appears when I render the above code in a chunk (or knit it). How ever, if I put the following, the tree appears in the rendered output

#### doesn't render anything
xx = lapply(1:3, function(i) vtree(FakeData,names(FakeData)[i+1]))
lapply(1:3, function(i) print(xx[[i]]))

### this renders the second vtree
xx[[2]]

how to fix it?

regards

K

nbarrowman commented 5 years ago

If you do a Google search for knitr looping graphs you'll see quite a few answers to this type of question.

A little while ago I figured out a solution of sorts:

---
title: "Looping vtree"
author: "Nick Barrowman"
date: "27/03/2019"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
library(vtree)
library(knitr)
makeChunk <- function(Rcode,options="") {
  paste0("```{r, ",options,"}\n",Rcode,"\n```\n") 
} 
out <- NULL
chunkOptions <- "fig.width=3,fig.height=3"
vars <- c("Severity","Sex","Group","Viral")
for (var in vars) {
  r <- paste0("vtree(FakeData,'",var,"')")
  out <- c(out,makeChunk(r,options=chunkOptions))
}

r paste(knitr::knit(text = out), collapse = '\n')

kishorerathi commented 5 years ago

Thanks a bunch.

Will try this approach as it looks reasonable.

Just curious. if vtree has a print method then it mayn't be necessary to have such a solution. Is this by design or something else prompted absence of print.

I came across vtree from cheatsheet section of RStudio. And I could immediately see usage of it in some EDA type of activities in Analytics. Just unfortunate that the package is not widely talked about.

nbarrowman commented 5 years ago

vtree uses the grViz function in the DiagrammeR package, which returns an object of class htmlwidget. If you look at the help for DiagrammeR::grViz, you'll see that it returns:

An object of class htmlwidget that will intelligently print itself into HTML in a variety of contexts including the R console, within R Markdown documents, and within Shiny output bindings.

So there is in fact a print method (print.htmlwidget). I'm not certain, but I believe that the issue with looping graphs in code chunks has to do with knitr.

Glad to hear that vtree is of interest to you. I have found that it's very useful for EDA, and also for producing flow charts for the kind of clinical research studies I'm involved in. Thank you for your suggestions.

kishorerathi commented 5 years ago

Thanks for the code. I was able to modify the code to suit my requirement. One thing I noticed is the execution speed. My case involved 2 variables. To have better understanding I broke the whole vtree in to individual trees based on first variable. This allowed me to have respectable sized rendered trees. The complete tree was really large.

Rendering of the individual 23 vtree's was quite slow on my system which is reasonably powerful (quad core i7 with 16GB of RAM).

Any thoughts on speedup of rendering will be great. Thanks for your help.

nbarrowman commented 5 years ago

Yes, it can be slow. I haven’t spent much time trying to optimize the speed of vtree, but profiling using Rprof ( https://support.rstudio.com/hc/en-us/articles/218221837-Profiling-with-RStudio ) would be one avenue.

How long did it take on your system to make those trees? How big were they?