首页 > 解决方案 > 在 Shiny 中使用具有自适应约束的自定义视觉对象

问题描述

假设我想在交互式环境(如 R Shiny)中使用自定义图像或 shapefile,例如这张纸飞机的图像:

纸飞机

我也愿意自己在程序中绘制图像以实现完全控制。

但总体目标是允许用户拖动图像的边缘,并且程序可以跟踪每个维度的变化大小(比如纸飞机的翼展)

Shiny在这里是可能的,还是我需要使用另一个程序?我还想要一些可供用户使用的更改的统计信息。

有没有人有类似的例子,或者可以指出我正确的方向?

标签: rshinyfrontend

解决方案


就像我在评论中写的那样,您可以使用 shinyjqui 包并阅读用户的会话信息。

可以在下面找到一个可重现的示例:

library(shiny)
library(shinyjqui)
library(ggplot2)
server <- function(input, output, session){
  global <- reactiveValues(width = 400, height = 400)

  observe({
    print(session)
    if(!is.null(session$clientData[["output_plot1_height"]])){
      global$height <- session$clientData[["output_plot1_height"]]
      global$width <- session$clientData[["output_plot1_width"]]
    }
  })

  output$plot1 <- renderImage({
    outfile <- tempfile(fileext='.png')
    png(outfile, width = global$width, height = global$height)
    hist(rnorm(200))
    dev.off()
    list(src = outfile)
  }, deleteFile = TRUE)

  output$clientdataText <- renderText({
    paste("height is ", global$height, "width is", global$width)
  })


  }

ui <- fluidPage(
    verbatimTextOutput("clientdataText"),
    jqui_resizabled(plotOutput("plot1"))
)


shinyApp(ui, server)

推荐阅读