R闪亮模块 - 在ggvis图上悬停工具提示

时间:2016-05-08 13:09:11

标签: r module shiny tooltip ggvis

我开始使用闪亮的模块,而且我无法使用ggvis绘图上的悬停工具提示显示文本。当我编写没有模块的代码时,这不是一个问题。谢谢你的任何想法。

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 个答案:

答案 0 :(得分:0)

不确定您是否必须使用模块,但将模块移至ui并且server函数似乎正在运行:

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)