0
votes

I'm getting started with shiny modules and I'm not able to display text using hover tooltip on a ggvis plot. This isn't a problem when I write the code without modules. Thanks for any ideas.

library(shiny)
library(ggvis)
library(dplyr)

plotModUI <- function(id){
        ns = NS(id)
        tagList(
                selectInput(ns("group"), "Select Group", choices=c("Group A", "Group B"), selected="Group A"), 
                ggvisOutput("ggvisplot")
        )
}

plotMod <- function(input, output, session, data){
        groupDat <- reactive({
                data %>% filter(group==input$group)
        })
        hoverText = function(x){
                paste0("(", format(x$xvar, digits=2), ", ", format(x$yvar, digits=2), ")")
        }
        observe({
                groupDat() %>% ggvis(~xvar, ~yvar) %>% layer_points() %>% 
                        add_tooltip(hoverText, "hover") %>% bind_shiny("ggvisplot")
        })
} 

ui <- fillPage(
        plotModUI("example")
)

server <- function(input, output, session){
        exdat = data.frame(xvar=runif(100), yvar=rnorm(100), group=c(rep("Group A", 50), rep("Group B", 50)))
        callModule(plotMod, "example", exdat)
}

shinyApp(ui, server)
1

1 Answers

0
votes

Not sure if you have to use Module, but move the module into the ui and server functions seem to be working:

ui <- fillPage(
    selectInput("group", "Select Group", choices=c("Group A", "Group B"), selected="Group A"), 
    ggvisOutput("ggvisplot")
)

server <- function(input, output, session){
    exdat = data.frame(xvar=runif(100), yvar=rnorm(100), group=c(rep("Group A", 50), rep("Group B", 50)))
    groupDat <- reactive({
        exdat %>% filter(group==input$group)
    })
    hoverText = function(x){
        paste0("(", format(x$xvar, digits=2), ", ", format(x$yvar, digits=2), ")")
    }
    observe({
        groupDat() %>% ggvis(~xvar, ~yvar) %>% layer_points() %>% 
            add_tooltip(hoverText, "hover") %>% bind_shiny("ggvisplot")
    })
}

shinyApp(ui, server)