使用Shinymanager R嵌入图像 [英] Embedding an image with shinymanager R

查看:41
本文介绍了使用Shinymanager R嵌入图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个闪亮的应用程序,其中我正在使用Shinymanager程序包来处理用户身份验证.我正在尝试从www/文件夹添加图片作为身份验证背景.这是一个有效的示例.

I have a shiny app where i am using shinymanager package to handle user authentication. I am trying to add a picture from my www/ folder as the authentication background. Here is a working example.

运行该应用程序时,您当前会看到"R"徽标在背景中.我有一个名为"pabackground.png"的图片在我的www/文件夹中.这是我闪亮的应用程序当前结构的样子.

When you run the app you will currently see the "R" logo in the background. I have a image called "pabackground.png" in my www/ folder. Here is what the current structure of my shiny app looks like.

| shinyApp/
    | ui.R
    | server.R
    | www/
       | pabackground.png

我尝试了一些想法,例如url('www/pabackground')和img(src ='www/pabackground'),但是没有运气.感谢您的帮助.

I have tried a few ideas like url('www/pabackground') and img(src = 'www/pabackground') with no luck. Thanks for your help.

应用代码

if (interactive()) {
  
  library(shiny)
  library(shinymanager)
  
 
  credentials <- data.frame(
    user = c("fanny", "victor"),
    password = c(scrypt::hashPassword("azerty"), scrypt::hashPassword("12345")),
    is_hashed_password = TRUE,
    comment = c("alsace", "auvergne"),
    stringsAsFactors = FALSE
  )
  
  # app
  ui <- fluidPage(
    
    # authentication module
    auth_ui(
      id = "auth",
      tags_top = 
        tags$div(
          tags$h4("Demo", style = "align:center"),
          tags$img(
            #i would like to change this picture to a picture from my www/ folder
            src = "https://www.r-project.org/logo/Rlogo.png", width = 100
        )
      ),
    
      tags_bottom = tags$div(
        tags$p(
          "For any question, please  contact ",
          tags$a(
            href = "mailto:someone@example.com?Subject=Shiny%20aManager",
            target="_top", "administrator"
          )
        )
      ),
      # change auth ui background ?
      # https://developer.mozilla.org/fr/docs/Web/CSS/background
      background  = "linear-gradient(rgba(0, 0, 255, 0.5),
                       rgba(255, 255, 0, 0.5)),
                       url('https://www.r-project.org/logo/Rlogo.png');", 
      choose_language = TRUE
    ),
    
    # result of authentication
    verbatimTextOutput(outputId = "res_auth"),
    
    # classic app
    headerPanel('Iris k-means clustering'),
    sidebarPanel(
      selectInput('xcol', 'X Variable', names(iris)),
      selectInput('ycol', 'Y Variable', names(iris),
                  selected=names(iris)[[2]]),
      numericInput('clusters', 'Cluster count', 3,
                   min = 1, max = 9)
    ),
    mainPanel(
      plotOutput('plot1')
    )
  )
  
  server <- function(input, output, session) {
    
    # authentication module
    auth <- callModule(
      module = auth_server,
      id = "auth",
      check_credentials = check_credentials(credentials)
    )
    
    output$res_auth <- renderPrint({
      reactiveValuesToList(auth)
    })
    
    # classic app
    selectedData <- reactive({
      
      req(auth$result)  # <---- dependency on authentication result
      
      iris[, c(input$xcol, input$ycol)]
    })
    
    clusters <- reactive({
      kmeans(selectedData(), input$clusters)
    })
    
    output$plot1 <- renderPlot({
      palette(c("#E41A1C", "#377EB8", "#4DAF4A", "#984EA3",
                "#FF7F00", "#FFFF33", "#A65628", "#F781BF", "#999999"))
      
      par(mar = c(5.1, 4.1, 0, 1))
      plot(selectedData(),
           col = clusters()$cluster,
           pch = 20, cex = 3)
      points(clusters()$centers, pch = 4, cex = 4, lwd = 4)
    })
  }
  
  shinyApp(ui, server)
  
}

推荐答案

如果图像在 www 文件夹中可用,则无需使用 addResourcePath . www 文件夹的前缀为"/".请参阅.

If the image is available in the www folder using addResourcePath is unnecessary. The prefix for the www folder is "/". Please see this.

相应地,以下内容应足够:

Accordingly the following should be sufficent:

tags$img(
          src = "/pabackground.png", width = 100
        )

这篇关于使用Shinymanager R嵌入图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆