Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hover treemap in a shinyapp not showing the right values #44

Open
manolo20 opened this issue Aug 9, 2018 · 0 comments
Open

Hover treemap in a shinyapp not showing the right values #44

manolo20 opened this issue Aug 9, 2018 · 0 comments

Comments

@manolo20
Copy link

manolo20 commented Aug 9, 2018

I am trying to replicate the hover plot from the "treemap" library examples in the following shinyapp:

library(shiny)
library(treemap)
library(dplyr)
library(gridBase)


weights <- readRDS("/home/R projects/treemap1.rds")

### Handle cliks on a treemap

tmLocate <-
  function(coor, tmSave) {
    tm <- tmSave$tm

# retrieve selected rectangle
    rectInd <- which(tm$x0 < coor[1] & (tm$x0 + tm$w) > coor[1] &
                       tm$y0 < coor[2] & (tm$y0 + tm$h) > coor[2])
    return(tm[rectInd[1], ])   
  }

ui = fluidPage(pageWithSidebar(
  headerPanel("Interactive treemap"),
  sidebarPanel(
    selectInput("prov", "Select province:", choices = unique(weights$Region)), width = 2
  ),
  mainPanel(
    plotOutput("plot", hover="hover", height = 700, width = 900), 
    tableOutput("record")
  ))
)

server = function(input, output){
  sel1 <- reactive({
    weights %>% filter(Region %in% input$prov)
  })

  getRecord <- reactive({
    x <- input$hover$x
    y <- input$hover$y

    x <- (x - .tm$vpCoorX[1]) / (.tm$vpCoorX[2] - .tm$vpCoorX[1])
    y <- (y - .tm$vpCoorY[1]) / (.tm$vpCoorY[2] - .tm$vpCoorY[1])

    l <- tmLocate(list(x=x, y=y), .tm)
    l[, 1:(ncol(l)-9)]            
  })

  output$plot <- renderPlot({ 
    #cat(input$hover$x, "\n")
    par(mar=c(0,0,0,0), xaxs='i', yaxs='i') 
    plot(c(0,1), c(0,1),axes=F, col="white")
    vps <- baseViewports()

    .tm <<- treemap(sel1(), 
                    index=c("Level.0","Level.1"),
                    vSize="X2015", vp=vps$plot)

  })
  output$record <- renderTable({
    getRecord()
  })
}

shinyApp(ui=ui, server=server)

Unfortunately, for some variables (like Shelter or Transportation) the hover treemap shows only the values only for the "Level.0" and for other variables it show the values for Level.1 how it should be.

I am trying to show only the values of Level.1 when hovering the treemap and I think that the issue is within the tmLocate function (or maybe the getRecord one) which I don't fully understand.

The dataset can be found here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant