Plotting of D3Tree in Rshiny

I have a D3tree that I would like to output in my Rshiny but it does not seem to work. I tried plotting on a normal R script and it works. Tried to change the Render option but it does not work either. Anyone could possibly share why the following code would not output the map?

UI

library(shiny)
library (igraph)
library (tidygraph)
library (ggraph)
library (plotly)
library (tmap)
library (tidyverse)
library (sf)
library (patchwork)
library (ggstatsplot)
library (gapminder)
library (d3treeR)
library(treemap)
library(r2d3)


interaction <- readRDS("data/participant_interaction.rds")
interaction_all <- readRDS("data/participant_interaction_all.rds")
total_data <- read_rds('data/total_data.rds')
buildings <- read_sf("data/Buildings.csv", 
                     options = "GEOM_POSSIBLE_NAMES=location")
resto_month2 <- read_rds('data/resto_month2.rds')
total_data_sf <- st_as_sf(total_data)

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    tabsetPanel(
      
      tabPanel( "Social Interaction",
                # Sidebar with a slider input for number of bins 
                sidebarLayout(
                  sidebarPanel(
                    helpText(" Visualise the Social Network Interaction of the Population
          in Ohio"),
                    
                    selectInput(inputId = "Category",
                                label = "Choose a Category",
                                choices = c( "Household Size" = "Household_Size",
                                             "Have Kids" = "Have_Kids",
                                             "Education Level" = "Education_Level",
                                             "Interest Group" = "Interest_Group",
                                             "Age Group" = "Age_Group"
                                ),
                                selected = "Household_Size")
                  ),
                  mainPanel(
                    plotOutput("treemapPlot")
                  )
                )         
        
        
      ),

 dataset <- reactive({
    interaction_all %>%
      group_by(Participant_ID,MonthYear, .data[[input$Category]])%>%
      summarise(InteractionCount = n()) %>%
      ungroup
  })
  
  
    output$treemapPlot <- renderD3tree3({
      d3tree3(
        treemap(dataset(),
              index = c(input$Category,"Participant_ID"),
              vSize = "InteractionCount",
              type = "index",
              palette = "Set2",
              align.labels=list(
                c("center", "center"), 
                c("right", "bottom")
              )), 
        rootname = "Tree Map of Interaction Count by Participant"
      )
    })

The interaction count will change based on the category input.

random dput

structure(list(Participant_ID = c(1, 1, 2, 1, 2, 0, 1, 1, 1, 
0, 0, 1, 1, 0, 2), MonthYear = structure(c(2023.08333333333, 
2022.75, 2022.33333333333, 2022.25, 2022.83333333333, 2022.75, 
2022.25, 2022.41666666667, 2022.58333333333, 2022.75, 2022.83333333333, 
2022.58333333333, 2023.25, 2022.91666666667, 2022.83333333333
), class = "yearmon"), Income = c(118421.02, 118421.02, 104429, 
118421.02, 104429, 134904.67, 118421.02, 118421.02, 118421.02, 
134904.67, 134904.67, 118421.02, 118421.02, 134904.67, 104429
), Expenses = c(-21139.52, -21139.52, -18363.69, -21139.52, -18363.69, 
-18226.85, -21139.52, -21139.52, -21139.52, -18226.85, -18226.85, 
-21139.52, -21139.52, -18226.85, -18363.69), Household_Size = c(3, 
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3), Have_Kids = c(TRUE, 
TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, 
TRUE, TRUE, TRUE), Age = c(25, 25, 35, 25, 35, 36, 25, 25, 25, 
36, 36, 25, 25, 36, 35), Education_Level = c("High School or College", 
"High School or College", "High School or College", "High School or College", 
"High School or College", "High School or College", "High School or College", 
"High School or College", "High School or College", "High School or College", 
"High School or College", "High School or College", "High School or College", 
"High School or College", "High School or College"), Interest_Group = c("B", 
"B", "A", "B", "A", "H", "B", "B", "B", "H", "H", "B", "B", "H", 
"A"), Joviality = c(0.3280865, 0.3280865, 0.39346959, 0.3280865, 
0.39346959, 0.001626703, 0.3280865, 0.3280865, 0.3280865, 0.001626703, 
0.001626703, 0.3280865, 0.3280865, 0.001626703, 0.39346959), 
    Age_Group = structure(c(2L, 2L, 4L, 2L, 4L, 5L, 2L, 2L, 2L, 
    5L, 5L, 2L, 2L, 5L, 4L), levels = c("20 & Below", "21-25", 
    "26-30", "31-35", "36-40", "41-45", "46-50", "51-55", "56-60"
    ), class = "factor")), row.names = c(1810L, 7870L, 12712L, 
4384L, 11450L, 973L, 3175L, 4360L, 4941L, 490L, 596L, 8420L, 
8559L, 670L, 9179L), class = "data.frame")

if you are using a renderD3tree then you need to place it in your ui as a d3treeOutput

 mainPanel(
                    d3treeOutput("treemapPlot")
                  )

I change the UI and the Output function but still there is no display after I run the app.

server <- function(input, output, session) {
  
  dataset <- reactive({
    interaction_all %>%
      group_by(Participant_ID,MonthYear, .data[[input$Category]])%>%
      summarise(InteractionCount = n()) %>%
      ungroup
  })
  
  
    output$treemapPlot <- renderD3tree({
      d3tree(
        treemap(dtf = dataset(),
              index = c(input$Category,"Participant_ID"),
              vSize = "InteractionCount",
              type = "value",
              vColor = "InteractionCount",
              palette = "Set2",
              align.labels=list(
                c("center", "center"), 
                c("right", "bottom")
              ))
      )
    })

I tested it and it does work after my recommended change, here it is fully reprexed.

library(tidyverse)
library(shiny)
library(d3treeR)
library(treemap)
# Define UI for application that draws a histogram
ui <- fluidPage(
  
  # Application title
  tabsetPanel(
    
    tabPanel( "Social Interaction",
              # Sidebar with a slider input for number of bins 
              sidebarLayout(
                sidebarPanel(
                  helpText(" Visualise the Social Network Interaction of the Population
          in Ohio"),
                  
                  selectInput(inputId = "Category",
                              label = "Choose a Category",
                              choices = c( "Household Size" = "Household_Size",
                                           "Have Kids" = "Have_Kids",
                                           "Education Level" = "Education_Level",
                                           "Interest Group" = "Interest_Group",
                                           "Age Group" = "Age_Group"
                              ),
                              selected = "Household_Size")
                ),
                mainPanel(
                  d3treeOutput("treemapPlot")
                )
              )         
    )))
              
    

interaction_all <- structure(list(Participant_ID = c(1, 1, 2, 1, 2, 0, 1, 1, 1, 
                                                     0, 0, 1, 1, 0, 2), MonthYear = structure(c(2023.08333333333, 
                                                                                                2022.75, 2022.33333333333, 2022.25, 2022.83333333333, 2022.75, 
                                                                                                2022.25, 2022.41666666667, 2022.58333333333, 2022.75, 2022.83333333333, 
                                                                                                2022.58333333333, 2023.25, 2022.91666666667, 2022.83333333333
                                                     ), class = "yearmon"), Income = c(118421.02, 118421.02, 104429, 
                                                                                       118421.02, 104429, 134904.67, 118421.02, 118421.02, 118421.02, 
                                                                                       134904.67, 134904.67, 118421.02, 118421.02, 134904.67, 104429
                                                     ), Expenses = c(-21139.52, -21139.52, -18363.69, -21139.52, -18363.69, 
                                                                     -18226.85, -21139.52, -21139.52, -21139.52, -18226.85, -18226.85, 
                                                                     -21139.52, -21139.52, -18226.85, -18363.69), Household_Size = c(3, 
                                                                                                                                     3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3), Have_Kids = c(TRUE, 
                                                                                                                                                                                              TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, 
                                                                                                                                                                                              TRUE, TRUE, TRUE), Age = c(25, 25, 35, 25, 35, 36, 25, 25, 25, 
                                                                                                                                                                                                                         36, 36, 25, 25, 36, 35), Education_Level = c("High School or College", 
                                                                                                                                                                                                                                                                      "High School or College", "High School or College", "High School or College", 
                                                                                                                                                                                                                                                                      "High School or College", "High School or College", "High School or College", 
                                                                                                                                                                                                                                                                      "High School or College", "High School or College", "High School or College", 
                                                                                                                                                                                                                                                                      "High School or College", "High School or College", "High School or College", 
                                                                                                                                                                                                                                                                      "High School or College", "High School or College"), Interest_Group = c("B", 
                                                                                                                                                                                                                                                                                                                                              "B", "A", "B", "A", "H", "B", "B", "B", "H", "H", "B", "B", "H", 
                                                                                                                                                                                                                                                                                                                                              "A"), Joviality = c(0.3280865, 0.3280865, 0.39346959, 0.3280865, 
                                                                                                                                                                                                                                                                                                                                                                  0.39346959, 0.001626703, 0.3280865, 0.3280865, 0.3280865, 0.001626703, 
                                                                                                                                                                                                                                                                                                                                                                  0.001626703, 0.3280865, 0.3280865, 0.001626703, 0.39346959), 
                                  Age_Group = structure(c(2L, 2L, 4L, 2L, 4L, 5L, 2L, 2L, 2L, 
                                                          5L, 5L, 2L, 2L, 5L, 4L), levels = c("20 & Below", "21-25", 
                                                                                              "26-30", "31-35", "36-40", "41-45", "46-50", "51-55", "56-60"
                                                          ), class = "factor")), row.names = c(1810L, 7870L, 12712L, 
                                                                                               4384L, 11450L, 973L, 3175L, 4360L, 4941L, 490L, 596L, 8420L, 
                                                                                               8559L, 670L, 9179L), class = "data.frame")


server <- function(input, output, session) {
  dataset <- reactive({
    interaction_all %>%
      group_by(Participant_ID,MonthYear, .data[[input$Category]])%>%
      summarise(InteractionCount = n()) %>%
      ungroup
  })
  
  output$treemapPlot <- renderD3tree3({
    d3tree3(
      treemap(dataset(),
              index = c(input$Category,"Participant_ID"),
              vSize = "InteractionCount",
              type = "index",
              palette = "Set2",
              align.labels=list(
                c("center", "center"), 
                c("right", "bottom")
              )), 
      rootname = "Tree Map of Interaction Count by Participant"
    )
  })
  
}

shinyApp(ui, server)

Oh,

Apparently I received this error message.

Loading required package: d3treeR
Warning in install.packages :
package ‘d3treeR’ is not available for this version of R

A version of this package for your version of R might be available elsewhere,
see the ideas at

this is confusing as you said 'I tried plotting on a normal R script and it works.' and I didn't choose the library, you did...

However, I can tell you that its not on CRAN (i.e. CRAN has no version of it ), I use renv as my package manager , so installed it from the github with

renv::install("d3treeR/d3treeR")

ah great! it works already! Sorry for the confusion. I plotted it using my laptop and it was working. I pushed to Git and pull it to my desktop and I not too sure why my desktop version of R is different and could not download D3TreeR. Thanks alot!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.