【问题标题】:Display plot data in ggvis tooltip在 ggvis 工具提示中显示绘图数据
【发布时间】:2016-07-08 18:19:16
【问题描述】:

我有一个闪亮的应用程序,它根据用户输入的 x 和 y 变量更新散点图。我希望能够在工具提示中包含 x 和 y 的数据,并在用户更新他们想要的 x 和 y 轴上的内容时进行更新。下面是一些示例代码,其中包含我在此问题上的 2 次尝试(第 2 次尝试已注释掉)。请注意,我已添加到 Iris 数据集中,以便根据数据集中的行号为每个数据点提供唯一的 ID。

#Check packages to use in library
{
library('shiny') #allows for the shiny app to be used
library('stringr') #string opperator
library('ggvis') #allows for interactive ploting
library('dplyr')
library('RSQLite')
}

alldata <- iris

#adds a column of a unique ID for each row
alldata$ID <- 1:nrow(alldata)

# UI

ui<-fluidPage(
titlePanel("Iris"),
fluidRow(
column(12,
       ggvisOutput("plot1")
),
column(4,
       wellPanel(
         h4("Data Variables"),
         selectInput(inputId = "x", label="Select x-axis Variable:", choices=as.character(names(alldata[,1:4])),selected='Petal.Length', multiple = FALSE),
         selectInput(inputId = "y", label="Select y-axis Variable:", choices=as.character(names(alldata[,1:4])),selected='Petal.Width', multiple = FALSE)
       ))
))

#SERVER
server<-function(input,output,session)
{
# Function for generating tooltip text
my_tooltip <- function(tt) {
if (is.null(tt)) return(NULL)
if (is.null(tt$ID)) return(NULL)

# Pick out the shot with this ID
alldata <- isolate(alldata)
Datapoint <- alldata[alldata$ID == tt$ID, ]

paste0("<b>", "Species: ", Datapoint$`Species`, 
       "</b><br>", "ID: ", Datapoint$`ID`,
       "<br>", "X Variable: ", Datapoint$`input$x`,
       "<br>", "Y Variable: ", Datapoint$`input$y`
       # "<br>", "X Variable: ", Datapoint %>% `input$x`,
       # "<br>", "Y Variable: ", Datapoint %>% `input$y`
)
}

vis <- reactive({

xvar <- prop("x", as.symbol(input$x))
yvar <- prop("y", as.symbol(input$y))

p1 = alldata %>%
  ggvis(x = xvar, y = yvar) %>%
  layer_points(size.hover := 200,
               fillOpacity:= 0.5, fillOpacity.hover := 1,
               fill = ~Species,
               key := ~ID
  ) %>%

  # Adds the previously defined tool_tip my_tooltip
  add_tooltip(my_tooltip, "hover")

  # Specifies the size of the plot
  # set_options(width = 800, height = 450, duration = 0)
})

#Actually plots the data
vis %>% bind_shiny("plot1")
}

#Run the Shiny App to Display Webpage
shinyApp(ui=ui, server=server)

【问题讨论】:

    标签: r shiny tooltip ggvis


    【解决方案1】:

    一种选择是仅对工具提示函数中感兴趣的列进行子集化,然后显示此数据集中的所有值。

    my_tooltip <- function(tt) {
            if (is.null(tt)) return(NULL)
            if (is.null(tt$ID)) return(NULL)
    
            Datapoint <- alldata[alldata$ID == tt$ID, c("Species", "ID", input$x, input$y)]
    
            paste0(names(Datapoint), ": ", format(Datapoint), collapse = "<br />")
        }
    

    或者直接使用,例如input$x,因为这些是字符,因此很容易从数据集中提取出来并用作工具提示名称。

    my_tooltip <- function(tt) {
            if (is.null(tt)) return(NULL)
            if (is.null(tt$ID)) return(NULL)
    
            Datapoint = alldata[alldata$ID == tt$ID, ]
    
            paste0("<b>", "Species: ", Datapoint$`Species`,
                  "</b><br>", "ID: ", Datapoint$`ID`,
                  "<br>", input$x, ": ", Datapoint[[input$x]],
                  "<br>", input$y, ": ", Datapoint[[input$y]])
        }
    

    【讨论】:

    • 谢谢您,我只尝试了您的第二个解决方案,因为它是最容易实现的,并且完全符合我的要求!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-02
    • 2016-02-23
    相关资源
    最近更新 更多