css - 使用 Shinymanager R 嵌入图像

标签 css r shiny

我有一个 Shiny 的应用程序,我正在使用 Shiny 管理器包来处理用户身份验证。 我正在尝试从我的 www/文件夹中添加一张图片作为身份验证背景。这是一个工作示例。

当您运行该应用程序时,您当前会在后台看到“R”标志。我的 www/文件夹中有一个名为“pabackground.png”的图像。这是我 Shiny 的应用程序的当前结构。

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

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

申请代码

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)
  
}

最佳答案

正如@ismisehregal 指出的那样,当图片位于应用程序的 www 子文件夹中时,您不必使用 addResourcePath

如果它们在另一个子文件夹中,那么您必须使用 addResourcePath 来允许 Shiny 为它们提供服务。

例如,如果您的图片位于应用程序的“pics”子文件夹中,并且您想在“logo”子网址中提供它:

addResourcePath(prefix = "logo", directoryPath = "pics")

...

ui <- fluidPage(
  
  # authentication module
  auth_ui(
    id = "auth",
    tags_top = 
      tags$div(
        tags$h4("Demo", style = "align:center"),
        tags$img(
          src = "logo/pabackground.png", width = 100
        )
      ),
...

关于css - 使用 Shinymanager R 嵌入图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64671900/

相关文章:

r - 在 data.table 中按组绘制

r - 当第一次观察是 na 时,使用 na.locf 传递最后一个值并忽略第一行

r - 如何在 R 中使用正则表达式转义或清理斜线?

css - IE中的中心网站

html - 平面设计师应该如何学习网页设计?

r - 如何格式化 Shiny 的渲染表?

r - Shiny 中的两个 Action 按钮

依赖 Shiny 的 R 包给出 RJSONIO 警告

javascript - 使用 HTML 和 CSS 可视化数据层次结构

css - 在 Safari 中使用 -webkit-perspective 时选中/单选框消失