scale_interactive 控制台中打印的 onclick 事件
scale_interactive onclick event printed in console
我使用漂亮的 ggiraph 包中的 scale_fill_manual_interactive
函数构建了一个带有 interactive legend
的闪亮应用程序。但是,我想将 onclick
事件中的信息(男性或女性,取决于选择)打印到控制台。这是代码片段。
library(ggplot2)
library(ggiraph)
dat <- data.frame(
name = c( "Guy", "Ginette", "David", "Cedric", "Frederic" ),
gender = c( "Male", "Female", "Male", "Male", "Male" ),
height = c(169, 160, 171, 172, 171 ) )
p <- ggplot(dat, aes( x = name, y = height, fill = gender,
data_id = name ) ) +
geom_bar_interactive(stat = "identity")
p3 <- p +
scale_fill_manual_interactive(
name = label_interactive("gender", tooltip="Gender levels", data_id="legend.title"),
values = c(Male = "#0072B2", Female = "#009E73"),
data_id = function(breaks) { as.character(breaks)},
tooltip = function(breaks) { as.character(breaks)},
onclick = function(breaks) { cat( as.character(breaks)) }
)
x <- girafe(ggobj = p3)
x <- girafe_options(x,
opts_hover_key(girafe_css("stroke:red", text="stroke:none;fill:red")))
if (interactive()) print(x)
现在它只打印整个列表
Female Male
而不是只选择一个。
onclick 元素需要 javascript 指令。这也意味着这将存在于您的浏览器中而不是 R 中,以下是自定义 onclick 事件,以便浏览器控制台在单击元素时获得打印指令:
library(ggplot2)
library(ggiraph)
dat <- data.frame(
name = c( "Guy", "Ginette", "David", "Cedric", "Frederic" ),
gender = c( "Male", "Female", "Male", "Male", "Male" ),
height = c(169, 160, 171, 172, 171 ) )
p <- ggplot(dat, aes( x = name, y = height, fill = gender,
data_id = name ) ) +
geom_bar_interactive(stat = "identity")
p3 <- p +
scale_fill_manual_interactive(
name = label_interactive("gender", tooltip="Gender levels", data_id="legend.title"),
values = c(Male = "#0072B2", Female = "#009E73"),
data_id = function(breaks) { as.character(breaks)},
tooltip = function(breaks) { as.character(breaks)},
onclick = function(breaks) { sprintf("console.log(\"%s\")", breaks ) }
)
x <- girafe(ggobj = p3)
x <- girafe_options(x,
opts_hover_key(girafe_css("stroke:red", text="stroke:none;fill:red")))
if (interactive()) print(x)
这应该可以回答您的问题。它将在 R 控制台中打印最后单击的键。一个反应值可用于获取点击键,其 ID 为 plot_key_selected
:ggiraph ID + _key_selected
library(shiny)
library(ggplot2)
library(ggiraph)
gg <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point_interactive( size = 3 ) + theme_minimal() +
scale_colour_manual_interactive(
data_id = c("setosa" = "setosa", "versicolor" = "versicolor", "virginica" = "virginica"),
tooltip = c("setosa" = "setosa", "versicolor" = "versicolor", "virginica" = "virginica"),
values = c("setosa" = "purple", "versicolor" = "blue", "virginica" = "darkgreen")
)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
),
mainPanel(
ggiraph::ggiraphOutput("plot")
)
)
)
server <- function(input, output) {
output$plot <- renderggiraph({
x <- girafe(code = print(gg), width_svg = 6, height_svg = 8)
x
})
observe({
print(input$plot_key_selected)
})
}
shinyApp(ui = ui, server = server)
我使用漂亮的 ggiraph 包中的 scale_fill_manual_interactive
函数构建了一个带有 interactive legend
的闪亮应用程序。但是,我想将 onclick
事件中的信息(男性或女性,取决于选择)打印到控制台。这是代码片段。
library(ggplot2)
library(ggiraph)
dat <- data.frame(
name = c( "Guy", "Ginette", "David", "Cedric", "Frederic" ),
gender = c( "Male", "Female", "Male", "Male", "Male" ),
height = c(169, 160, 171, 172, 171 ) )
p <- ggplot(dat, aes( x = name, y = height, fill = gender,
data_id = name ) ) +
geom_bar_interactive(stat = "identity")
p3 <- p +
scale_fill_manual_interactive(
name = label_interactive("gender", tooltip="Gender levels", data_id="legend.title"),
values = c(Male = "#0072B2", Female = "#009E73"),
data_id = function(breaks) { as.character(breaks)},
tooltip = function(breaks) { as.character(breaks)},
onclick = function(breaks) { cat( as.character(breaks)) }
)
x <- girafe(ggobj = p3)
x <- girafe_options(x,
opts_hover_key(girafe_css("stroke:red", text="stroke:none;fill:red")))
if (interactive()) print(x)
现在它只打印整个列表
Female Male
而不是只选择一个。
onclick 元素需要 javascript 指令。这也意味着这将存在于您的浏览器中而不是 R 中,以下是自定义 onclick 事件,以便浏览器控制台在单击元素时获得打印指令:
library(ggplot2)
library(ggiraph)
dat <- data.frame(
name = c( "Guy", "Ginette", "David", "Cedric", "Frederic" ),
gender = c( "Male", "Female", "Male", "Male", "Male" ),
height = c(169, 160, 171, 172, 171 ) )
p <- ggplot(dat, aes( x = name, y = height, fill = gender,
data_id = name ) ) +
geom_bar_interactive(stat = "identity")
p3 <- p +
scale_fill_manual_interactive(
name = label_interactive("gender", tooltip="Gender levels", data_id="legend.title"),
values = c(Male = "#0072B2", Female = "#009E73"),
data_id = function(breaks) { as.character(breaks)},
tooltip = function(breaks) { as.character(breaks)},
onclick = function(breaks) { sprintf("console.log(\"%s\")", breaks ) }
)
x <- girafe(ggobj = p3)
x <- girafe_options(x,
opts_hover_key(girafe_css("stroke:red", text="stroke:none;fill:red")))
if (interactive()) print(x)
这应该可以回答您的问题。它将在 R 控制台中打印最后单击的键。一个反应值可用于获取点击键,其 ID 为 plot_key_selected
:ggiraph ID + _key_selected
library(shiny)
library(ggplot2)
library(ggiraph)
gg <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point_interactive( size = 3 ) + theme_minimal() +
scale_colour_manual_interactive(
data_id = c("setosa" = "setosa", "versicolor" = "versicolor", "virginica" = "virginica"),
tooltip = c("setosa" = "setosa", "versicolor" = "versicolor", "virginica" = "virginica"),
values = c("setosa" = "purple", "versicolor" = "blue", "virginica" = "darkgreen")
)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
),
mainPanel(
ggiraph::ggiraphOutput("plot")
)
)
)
server <- function(input, output) {
output$plot <- renderggiraph({
x <- girafe(code = print(gg), width_svg = 6, height_svg = 8)
x
})
observe({
print(input$plot_key_selected)
})
}
shinyApp(ui = ui, server = server)