gpt4 book ai didi

r - 将图像悬停在 Shiny 应用程序中的plotly r 图表中

转载 作者:行者123 更新时间:2023-12-02 02:26:03 24 4
gpt4 key购买 nike

这里有人有一个在悬停在绘图或任何可以执行此操作的包上时显示图像的示例吗?我已经尝试过一些方法,但它只会显示 url,但不会显示图像。我知道这段代码只是包含 URL。如何构建一个 div 来显示图像。

library(shiny)
library(shinydashboard)
library(DT)
library(dplyr)
library(plotly)

# Data ------------------------------------------------------------------
dt <- data.frame(fruits = c("apple","banana","oranges"),
rank = c(11, 22, 33),
image_url = c(
'https://images.unsplash.com/photo-1521671413015-ce2b0103c8c7?ixlib=rb-0.3.5&s=45547f67f01ffdcad0e33c8417b840a9&auto=format&fit=crop&w=667&q=80',
"https://images.unsplash.com/photo-1520699697851-3dc68aa3a474?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=ef15aee8bcb3f5928e5b31347adb6173&auto=format&fit=crop&w=400&q=80",
"https://images.unsplash.com/photo-1501925873391-c3cd73416c5b?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=379e4a0fffc6d11cd5794806681d0211&auto=format&fit=crop&w=750&q=80"
))

# img_dt <- dt %>%
# mutate(img = paste0("<a target='_blank' href='", image_url, "'><img src=\'", image_url, "' height='40'></img></a>")) %>%
# mutate(link = paste0("<a href='", image_url,"' target='_blank'>","View photo","</a>"))

# Dashboard ----------------------------------------------------------------
ui <- dashboardPage(
dashboardHeader(title = "Test"),

dashboardSidebar(),

dashboardBody(
tags$head(
tags$style(
HTML(
"img.small-img {
max-width: 75px;
}")
)
),

plotlyOutput("hoverplot")
)
)

server <- function(input, output) {

output$hoverplot <- renderPlotly({
plot_ly(
dt,
x = ~fruits,
y = ~rank,
type = 'scatter',
mode = 'markers',
hoverinfo = 'text',
text = ~ paste(
'Species: ', fruits,
'</br> Creative: ', paste0(
"<a target='_blank' href='", image_url, "'><img src=\'",
image_url,
"' height='40'></img></a>"
)
)
)
})
}

shinyApp(ui = ui, server = server)

最佳答案

正如 @StéphaneLaurent 所说,它是 not possible使用plotly的hoverinfo直接显示图像。

但是,这里有一个基于 @mb158127 已经提到的 customdata 参数的解决方法。

这还考虑了 hover_event 的 x 和 y 位置:

library(shiny)
library(shinydashboard)
library(plotly)

# Data ------------------------------------------------------------------
dt <- data.frame(
fruits = c("apple", "banana", "oranges"),
rank = c(11, 22, 33),
image_url = c(
'https://images.unsplash.com/photo-1521671413015-ce2b0103c8c7?ixlib=rb-0.3.5&s=45547f67f01ffdcad0e33c8417b840a9&auto=format&fit=crop&w=667&q=80',
"https://images.unsplash.com/photo-1520699697851-3dc68aa3a474?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=ef15aee8bcb3f5928e5b31347adb6173&auto=format&fit=crop&w=400&q=80",
"https://images.unsplash.com/photo-1501925873391-c3cd73416c5b?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=379e4a0fffc6d11cd5794806681d0211&auto=format&fit=crop&w=750&q=80"
)
)

# Dashboard ----------------------------------------------------------------
ui <- dashboardPage(
dashboardHeader(title = "Test"),
dashboardSidebar(),
dashboardBody(tags$head(tags$style(
HTML("img.small-img {
max-width: 75px;
}")
)),
plotlyOutput("hoverplot"))
)

server <- function(input, output, session) {
output$hoverplot <- renderPlotly({
plot_ly(
dt,
x = ~ fruits,
y = ~ rank,
type = 'scatter',
mode = 'markers',
hoverinfo = 'none',
source = "hoverplotsource",
customdata = ~ image_url
) %>%
event_register('plotly_hover') %>%
event_register('plotly_unhover')
})

hover_event <- reactive({
event_data(event = "plotly_hover", source = "hoverplotsource")
})

unhover_event <- reactive({
event_data(event = "plotly_unhover", source = "hoverplotsource")
})

hoverplotlyProxy <- plotlyProxy("hoverplot", session)

observeEvent(unhover_event(), {
hoverplotlyProxy %>%
plotlyProxyInvoke("relayout", list(images = list(NULL)))
})

observeEvent(hover_event(), {
hoverplotlyProxy %>%
plotlyProxyInvoke("relayout", list(images = list(
list(
source = hover_event()$customdata,
xref = "x",
yref = "y",
x = hover_event()$x,
y = hover_event()$y,
sizex = 20,
sizey = 20,
opacity = 1
)
)))
})
}

shinyApp(ui = ui, server = server)

Result

Here您可以找到有关如何在此场景中使用本地镜像的后续问题。

关于r - 将图像悬停在 Shiny 应用程序中的plotly r 图表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58897330/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com