R语言 如何将自定义图像插入到Shiny图头中?

vsnjm48y  于 2023-04-18  发布在  其他
关注(0)|答案(2)|浏览(153)

在底部发布的几乎是MWE的代码中,我试图将一个自定义图像拉到plot header中。在完整的应用程序中,用户点击图像以触发解释性模态对话框。然而,在这种情况下,我无法将图像呈现在plot header中。在其他情况下,这对我来说很好,我使用renderUI(),但是在这个例子中,我试图在renderPlot()函数中渲染图像。下面的图像比这些文字解释得更好。有没有办法在renderPlot()中做到这一点?

MWE代码:

library(shiny)
library(survival)

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

ui <- fluidPage(
  selectInput("distSelect","Select distribution:",c("Weibull","Gamma")),
  sliderInput('shape','Adjust 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), # leave paste, actual App has more objects to include here
          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)
baubqpgj

baubqpgj1#

在这个场景中,我会使用shiny的actionLink

library(shiny)
library(survival)

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

ui <- fluidPage(
  selectInput("distSelect","Select distribution:",c("Weibull","Gamma")),
  sliderInput('shape','Adjust 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) # reduce space above Plot
    curve(
      weibSurv(x, shape=input$shape,scale=1/0.03), 
      from=0, to=80,
    )
  })
  
  observeEvent(input$explainBtn, {
    showModal(modalDialog("do something useful"))
  })
  
}
shinyApp(ui, server)
gwbalxhn

gwbalxhn2#

对于任何喜欢renderUI的人,这里有一个解决方案,尽管我会遵从ismirsehregal上面的actionLink()解决方案,因为它更干净。

library(shiny)
library(survival)

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

ui <- fluidPage(
  selectInput("distSelect","Select distribution:",c("Weibull","Gamma")),
  sliderInput('shape','Adjust 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)

相关问题