如何将自定义图像插入Shiny绘图标题?

huangapple go评论124阅读模式
英文:

How to insert a custom image into Shiny plot header?

问题

在底部发布的几乎是最小工作示例(MWE)代码中,我试图将自定义图像添加到绘图标题中。在完整的应用程序中,用户点击图像以触发解释性模态对话框。但是,在这种情况下,我无法在绘图标题中呈现图像。在其他情况下,对我来说效果很好,我使用renderUI(),但在这种情况下,我试图在renderPlot()函数内呈现图像。下面的图像比这些文字更好地解释了问题。有没有办法在renderPlot()内部实现这一点?

如何将自定义图像插入Shiny绘图标题?

MWE 代码:

library(shiny)
library(survival)

### 定义函数 ###
weibSurv <- function(t, shape, scale) pweibull(t, shape=shape, scale=scale, lower.tail = FALSE)

ui <- fluidPage(
  selectInput("distSelect", "选择分布:", c("Weibull", "Gamma")),
  sliderInput('shape', '调整形状:', min = 0, max = 3, step = 0.1, value = 1.5),
  plotOutput("plot")
)

server <- function(input, output, session) {
  output$plot <- renderPlot({
    curve(
      weibSurv(x, shape = input$shape, scale = 1/0.03), 
      from = 0, to = 80,
      main = 
        fluidRow(
          paste(input$distSelect), # 留下 paste,实际应用程序中还有更多要包括的对象
          tags$button(
            id = "explainBtn",
            class = "btn action-button",
            tags$img(src = "https://images.plot.ly/language-icons/api-home/python-logo.png")
          )
        ) 
    )
  })
}

shinyApp(ui, server)

<details>
<summary>英文:</summary>

In the almost-MWE code posted at the bottom, I&#39;m trying to pull a custom image into the plot header. In the full App the user clicks on the image in order to trigger an explanatory modal dialogue. However, I can&#39;t get the image to render in the plot header in this case. In other cases where this works for me fine, I use `renderUI()`, but in this case I&#39;m trying to render the image inside the `renderPlot()` function. Image below explains better than these words. Is there a way to do this inside `renderPlot()`?

[![enter image description here][1]][1]

MWE code:

    library(shiny)
    library(survival)
    
    ### define function ###
    weibSurv &lt;- function(t, shape, scale) pweibull(t, shape=shape,scale=scale, lower.tail=F)

    ui &lt;- fluidPage(
      selectInput(&quot;distSelect&quot;,&quot;Select distribution:&quot;,c(&quot;Weibull&quot;,&quot;Gamma&quot;)),
      sliderInput(&#39;shape&#39;,&#39;Adjust shape:&#39;,min=0,max=3,step=0.1,value=1.5),
      plotOutput(&quot;plot&quot;)
    )
    
    server &lt;- function(input, output, session) {
      output$plot &lt;- renderPlot({
        curve(
          weibSurv(x, shape=input$shape,scale=1/0.03), 
          from=0, to=80,
          main = 
            fluidRow(
              paste(input$distSelect), # leave paste, actual App has more objects to include here
              tags$button(
                id = &quot;explainBtn&quot;,
                class = &quot;btn action-button&quot;,
                tags$img(src = &quot;https://images.plot.ly/language-icons/api-home/python-logo.png&quot;)
              )
            ) 
        )
      })
    }
    shinyApp(ui, server)


  [1]: https://i.stack.imgur.com/7Uysx.png

</details>


# 答案1
**得分**: 1

在这种情况下,我会使用shiny的`actionLink`:

```R
library(shiny)
library(survival)

### 定义函数 ###
weibSurv <- function(t, shape, scale) pweibull(t, shape=shape, scale=scale, lower.tail = FALSE)

ui <- fluidPage(
  selectInput("distSelect", "选择分布:", c("Weibull", "Gamma")),
  sliderInput('shape', '调整形状:', min = 0, max = 3, step = 0.1, value = 1.5),
  column(12, align = "center",
         actionLink(inputId = "explainBtn", label = strong("Weibull"), icon = NULL, br(), img(src = "https://images.plot.ly/language-icons/api-home/python-logo.png"))
  ),
  plotOutput("plot")
)

server <- function(input, output, session) {
  output$plot <- renderPlot({
    par(mar = c(5, 4, 0, 2) + 0.1) # 减少图上方的空间
    curve(
      weibSurv(x, shape = input$shape, scale = 1/0.03), 
      from = 0, to = 80,
    )
  })

  observeEvent(input$explainBtn, {
    showModal(modalDialog("做一些有用的事情"))
  })
}

shinyApp(ui, server)
英文:

I'd use shiny's actionLink in this scenario:

library(shiny)
library(survival)

### define function ###
weibSurv &lt;- function(t, shape, scale) pweibull(t, shape=shape,scale=scale, lower.tail=F)

ui &lt;- fluidPage(
  selectInput(&quot;distSelect&quot;,&quot;Select distribution:&quot;,c(&quot;Weibull&quot;,&quot;Gamma&quot;)),
  sliderInput(&#39;shape&#39;,&#39;Adjust shape:&#39;,min=0,max=3,step=0.1,value=1.5),
  column(12, align=&quot;center&quot;,
         actionLink(inputId = &quot;explainBtn&quot;, label = strong(&quot;Weibull&quot;), icon = NULL, br(), img(src = &quot;https://images.plot.ly/language-icons/api-home/python-logo.png&quot;))
  ),
  plotOutput(&quot;plot&quot;)
)

server &lt;- function(input, output, session) {
  output$plot &lt;- renderPlot({
    par(mar=c(5,4,0,2)+0.1) # reduce space above Plot
    curve(
      weibSurv(x, shape=input$shape,scale=1/0.03), 
      from=0, to=80,
    )
  })
  
  observeEvent(input$explainBtn, {
    showModal(modalDialog(&quot;do something useful&quot;))
  })
  
}
shinyApp(ui, server)

答案2

得分: 0

以下是您要翻译的内容:

对于任何喜欢`renderUI`的人,这里有一个解决方案,尽管我更倾向于ismirsehregal的上面的`actionLink()`解决方案,因为它更清晰。

library(shiny)
library(survival)

### 在应用中定义的函数
weibSurv <- function(t, shape, scale) pweibull(t, shape=shape, scale=scale, lower.tail=FALSE)

ui <- fluidPage(
  selectInput("distSelect", "选择分布:", c("Weibull", "Gamma")),
  sliderInput('shape', '调整形状:', min=0, max=3, step=0.1, value=1.5),
  uiOutput("plotHeader"),
  fluidRow(plotOutput("plot"))
)

server <- function(input, output, session) {
  output$plot <- renderPlot({
    curve(
      weibSurv(x, shape=input$shape, scale=1/0.03),
      from=0,
      to=80
    )
  })

  output$plotHeader <- renderUI({
    fluidRow(
      align = 'center',
      paste(input$distSelect),
      tags$button(
        id = "explainBtn",
        class = "btn action-button",
        tags$img(src = "https://images.plot.ly/language-icons/api-home/python-logo.png")
      )
    )
  })
}

shinyApp(ui, server)
英文:

For anyone who favors renderUI, here's a solution though I would defer to ismirsehregal's actionLink() solution above as it is cleaner.

library(shiny)
library(survival)

### define functions used in App
weibSurv &lt;- function(t, shape, scale) pweibull(t, shape=shape,scale=scale, lower.tail=F)

ui &lt;- fluidPage(
  selectInput(&quot;distSelect&quot;,&quot;Select distribution:&quot;,c(&quot;Weibull&quot;,&quot;Gamma&quot;)),
  sliderInput(&#39;shape&#39;,&#39;Adjust shape:&#39;,min=0,max=3,step=0.1,value=1.5),
  uiOutput(&quot;plotHeader&quot;),
  fluidRow(plotOutput(&quot;plot&quot;))
)

server &lt;- function(input, output, session) {
  output$plot &lt;- renderPlot({
    curve(
      weibSurv(x, shape=input$shape,scale=1/0.03), 
      from=0, 
      to=80
    )
  })
  
  output$plotHeader &lt;- renderUI({
    fluidRow(
      align = &#39;center&#39;,
      paste(input$distSelect), 
      tags$button(
        id = &quot;explainBtn&quot;,
        class = &quot;btn action-button&quot;,
        tags$img(src = &quot;https://images.plot.ly/language-icons/api-home/python-logo.png&quot;)
      )
    )
  })
}
shinyApp(ui, server)

huangapple
  • 本文由 发表于 2023年4月13日 21:12:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76005856.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定