nbarrowman / vtree

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

How to use vtree in Shiny? #5

Closed trafficonese closed 5 years ago

trafficonese commented 5 years ago

The docs say that it is possible to use vtree in Shiny, but I couldn't find any example and I didnt manage to figure it out. Could you show how it should be achieved?

Here is what I tried:

library(shiny)
library(vtree)
library(DiagrammeR)
library(Hmisc)

getHdata(diabetes)

ui <- fluidPage(
  plotOutput("plot"),
  DiagrammeR::DiagrammeROutput("dia"),
  DiagrammeR::DiagrammeROutput("dia1")

)

server <- function(input, output, session) {
  output$plot <- renderPlot({
    vtree(diabetes,"frame",
          pngknit=FALSE,
          horiz=FALSE,height=250,width=850)
  })
  output$dia <- DiagrammeR::renderDiagrammeR({
    vtree(diabetes,"frame",
          pngknit=FALSE,
          horiz=FALSE,height=250,width=850)
  })
  output$dia1 <- DiagrammeR::renderGrViz({
    vtree(diabetes,"frame",
          pngknit=FALSE,
          horiz=FALSE,height=250,width=850)
  })
}

shinyApp(ui, server)
nbarrowman commented 5 years ago

Thanks for your question. I hadn't tried using vtree with Shiny, but the DiagrammeR package has built-in functions to make it possible.

In response to your question, I modified vtree a bit to make it work. I'll put a new version of vtree on github in the next day or two.

nbarrowman commented 5 years ago

It works with the new release candidate for vtree (v3.0.7). But in your code above, use grVizOutput instead of DiagrammeROutput.

You can install vtree 3.0.7 using this command:

remotes::install_github("nbarrowman/vtree@v3.0.7",build_vignettes=TRUE)

Just in case the above command doesn't build the vignette properly (I'm not sure why this is sometimes an issue with install_github), try:

remotes::install_github("nbarrowman/vtree@v3.0.7",build_opts = c("--no-resave-data", "--no-manual"),build_vignettes=TRUE,force=TRUE)