从 ggvis 中的 handle_hover() 或 handle_click() 返回值
Returning Values from handle_hover() or handle_click() in ggvis
我只是希望弄清楚如何使用 handle_hover() 或 handle_click() 来 return 可视化环境之外的值。
最终我想用它来 return 一个关键值,这样我就可以 link 两个图表到一个闪亮的应用程序上。
使用文档中的示例,我有:
mtcars$id <- seq_len(nrow(mtcars))
hoveron<-function(data,...){
testval<<-str(data)
testval
}
mtcars %>% ggvis(~mpg, ~wt,key:=~id) %>% layer_points() %>%
handle_hover(hoveron)
这个returns
'data.frame': 1 obs. of 3 variables:
$ id : int 16
$ mpg: num 10.4
$ wt : num 5.42"
在控制台中
我需要做什么才能 return testval= 16
非常感谢!
我不知道这是否有帮助,因为不清楚您想 return 键值在哪里。不过这里有一个简单的演示。
这个闪亮的应用程序中有 2 个情节。第二个地块的点是绿色的,但如果您将鼠标悬停在第一个地块的 id 为 25 的点上(您可以在控制台上看到打印的 id),第二个地块的点将变为红色。我使用 needed_val <- reactiveValues()
来跟踪测试值是否为 25。您可以从另一个 reactive()
上下文访问此值。
ui.R-
library(ggvis)
library(shiny)
shinyUI(
mainPanel(
ggvisOutput("plot"),
ggvisOutput("plot2")
)
)
server.R -
library(shiny)
library(dplyr)
library(ggvis)
shinyServer(function(input, output, session) {
needed_val <- reactiveValues(testval = 11)
mtcars$id <- seq_len(nrow(mtcars))
hoveron<-function(data, ...){
needed_val$testval <- as.numeric(data$id)
str(needed_val$testval)
}
mtcars %>% ggvis(~mpg, ~wt,key:=~id) %>% layer_points() %>%
handle_hover(hoveron) %>% bind_shiny("plot")
dat2 <- reactive({
if(needed_val$testval == 25){
color <- "red"
}
else{
color <- "green"
}
color
})
vis <- reactive({
mtcars %>% ggvis(~mpg, ~wt) %>% layer_points(fill := dat2())
}) %>% bind_shiny("plot2")
})
如果有帮助请告诉我。
我故意含糊其辞地说出我的确切需求,因为我仍然需要一些乐趣来解决问题,只是朝着正确的方向轻推一下;)
我想 link 通过悬停,突出显示下面散点图上的特定数据点。从 nafizh 中汲取灵感,我能够,代码如下:
library(shiny)
library(dplyr)
library(ggvis)
shinyServer(function(input, output, session) {
needed_val <- reactiveValues(testval = 11)
mtcars$id <- seq_len(nrow(mtcars))
theData <- reactive({
data.frame(mtcars)
})
hoveron<-function(data, ...){
needed_val$testval <- as.numeric(data$id)
str(needed_val$testval)
}
observe({
theData() %>% ggvis(~mpg, ~wt,key:=~id) %>% layer_points() %>%
handle_hover(hoveron) %>% bind_shiny("plot")
})
observe({
theData() %>% ggvis(~mpg, ~wt) %>%
layer_points()%>%
layer_points(fill:="red",data=reactive(theData()[needed_val$testval,]))%>%
bind_shiny("plot2")
})
})
干杯!
我只是希望弄清楚如何使用 handle_hover() 或 handle_click() 来 return 可视化环境之外的值。
最终我想用它来 return 一个关键值,这样我就可以 link 两个图表到一个闪亮的应用程序上。
使用文档中的示例,我有:
mtcars$id <- seq_len(nrow(mtcars))
hoveron<-function(data,...){
testval<<-str(data)
testval
}
mtcars %>% ggvis(~mpg, ~wt,key:=~id) %>% layer_points() %>%
handle_hover(hoveron)
这个returns
'data.frame': 1 obs. of 3 variables:
$ id : int 16
$ mpg: num 10.4
$ wt : num 5.42"
在控制台中
我需要做什么才能 return testval= 16
非常感谢!
我不知道这是否有帮助,因为不清楚您想 return 键值在哪里。不过这里有一个简单的演示。
这个闪亮的应用程序中有 2 个情节。第二个地块的点是绿色的,但如果您将鼠标悬停在第一个地块的 id 为 25 的点上(您可以在控制台上看到打印的 id),第二个地块的点将变为红色。我使用 needed_val <- reactiveValues()
来跟踪测试值是否为 25。您可以从另一个 reactive()
上下文访问此值。
ui.R-
library(ggvis)
library(shiny)
shinyUI(
mainPanel(
ggvisOutput("plot"),
ggvisOutput("plot2")
)
)
server.R -
library(shiny)
library(dplyr)
library(ggvis)
shinyServer(function(input, output, session) {
needed_val <- reactiveValues(testval = 11)
mtcars$id <- seq_len(nrow(mtcars))
hoveron<-function(data, ...){
needed_val$testval <- as.numeric(data$id)
str(needed_val$testval)
}
mtcars %>% ggvis(~mpg, ~wt,key:=~id) %>% layer_points() %>%
handle_hover(hoveron) %>% bind_shiny("plot")
dat2 <- reactive({
if(needed_val$testval == 25){
color <- "red"
}
else{
color <- "green"
}
color
})
vis <- reactive({
mtcars %>% ggvis(~mpg, ~wt) %>% layer_points(fill := dat2())
}) %>% bind_shiny("plot2")
})
如果有帮助请告诉我。
我故意含糊其辞地说出我的确切需求,因为我仍然需要一些乐趣来解决问题,只是朝着正确的方向轻推一下;)
我想 link 通过悬停,突出显示下面散点图上的特定数据点。从 nafizh 中汲取灵感,我能够,代码如下:
library(shiny)
library(dplyr)
library(ggvis)
shinyServer(function(input, output, session) {
needed_val <- reactiveValues(testval = 11)
mtcars$id <- seq_len(nrow(mtcars))
theData <- reactive({
data.frame(mtcars)
})
hoveron<-function(data, ...){
needed_val$testval <- as.numeric(data$id)
str(needed_val$testval)
}
observe({
theData() %>% ggvis(~mpg, ~wt,key:=~id) %>% layer_points() %>%
handle_hover(hoveron) %>% bind_shiny("plot")
})
observe({
theData() %>% ggvis(~mpg, ~wt) %>%
layer_points()%>%
layer_points(fill:="red",data=reactive(theData()[needed_val$testval,]))%>%
bind_shiny("plot2")
})
})
干杯!