Einbetten eines Bildes mit Shinymanager R.

Nov 03 2020

Ich habe eine glänzende App, in der ich das shinymanager-Paket verwende, um die Benutzerauthentifizierung durchzuführen. Ich versuche, ein Bild aus meinem WWW / Ordner als Authentifizierungshintergrund hinzuzufügen. Hier ist ein Arbeitsbeispiel.

Wenn Sie die App ausführen, sehen Sie derzeit das "R" -Logo im Hintergrund. Ich habe ein Bild namens "pabackground.png" in meinem www / Ordner. So sieht die aktuelle Struktur meiner glänzenden App aus.

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

Ich habe ein paar Ideen wie URL ('www / pabackground') und img (src = 'www / pabackground') ohne Glück ausprobiert. Danke für Ihre Hilfe.

Anwendungscode

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:[email protected]?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)
  
}

Antworten

2 ismirsehregal Nov 03 2020 at 23:38

Wenn das Bild im wwwOrdner verfügbar ist, ist die Verwendung von nicht addResourcePatherforderlich. Das Präfix für den wwwOrdner lautet "/". Bitte sehen Sie dies .

Dementsprechend sollte folgendes ausreichen:

tags$img(
          src = "/pabackground.png", width = 100
        )
1 HubertL Nov 03 2020 at 23:17

Wie @ismisehregal hervorhob, müssen Sie nicht verwenden, addResourcePathwenn sich Bilder im wwwUnterordner der App befinden.

Wenn sie sich in einem anderen Unterordner befinden, müssen Sie sie verwenden addResourcePath, damit Shiny sie bedienen kann.

Zum Beispiel, wenn sich Ihr Bild im Unterordner "Bilder" Ihrer App befindet und Sie es in der Unter-URL "Logo" bereitstellen möchten:

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
        )
      ),
...