首页 > 解决方案 > 在 Shiny App 中调整大小的图像上的单击坐标不覆盖完整图像

问题描述

我的应用程序通过添加边框来调整图像大小。然后,我需要在带有边框的修改后的图像上的任意位置收集点击坐标。这似乎不适用于动态大小的图像输出。

下面的代码生成一个应用程序,您可以在其中查看双击的 x 坐标。请注意,在没有边框的情况下,图像整个范围的 x 坐标如何更新——从左到右单击您会看到整个范围的值。但是,当添加边框时,x 坐标会在中间停止,并且在单击图像右侧时不会更新。

如果我在 中删除动态确定的宽度和高度(例如session$clientData$output_img_widthrenderImage,它会按预期工作。

有没有办法获得动态创建的图像大小和完整的点击坐标?

谢谢!

library(shiny)
library(magick)

img <- image_read("https://jeroen.github.io/images/frink.png")

ui <- fluidPage(
    sliderInput("border", "Border Size", 0, 100, 0),
    verbatimTextOutput("coords", placeholder = TRUE),
    imageOutput("img",  
                dblclick = "img_dblclick")
    )


server <- function(input, output, session) {

    updatedImageLoc <-reactiveVal(value = NULL)

    observe({
        file.saved.to.disk <- img %>%
            image_border("red",
                         geometry = paste0(input$border * 10,
                                           "x",
                                           input$border * 10)) %>%
            image_write(tempfile(fileext = "test.png"))

        updatedImageLoc(file.saved.to.disk)
    })

    output$coords <- renderText({ paste("Double-click x coord is:",input$img_dblclick$x) })

    output$img <- renderImage({
            # Read width and height. These are reactive values, so this
            # expression will re-run whenever these values change.
            width  <- session$clientData$output_img_width
            height <- session$clientData$output_img_height

            # Return a list
            list(src = updatedImageLoc(), 
                 contentType = paste0("image/png"),
                 width = width,
                 height = height)
        }, 
        ## DO NOT DELETE THE FILE!
        deleteFile = FALSE
    )
}

# Run the application 
shinyApp(ui = ui, server = server)

标签: rimageshinyimagemagickshiny-reactivity

解决方案


推荐阅读