Is it possible to add an image to a Shiny app?

I built a shiny dashboard and I would like to add some images, here's a reproducible example example using the mtcars dataset:

library(shiny)
require(shinydashboard)
library(dplyr)

mtcars <- tibble::rownames_to_column(mtcars,"model_car")


#Dashboard header carrying the title of the dashboard
header <- dashboardHeader(title = "mtcars dashboard", titleWidth = 450)  

#Sidebar content of the dashboard
sidebar <- dashboardSidebar(
  sidebarMenu(
    selectInput("model", "Select a model", 
                choices = mtcars$model_car)
    )
  )



frow1 <- fluidRow(
  valueBoxOutput("value1")
  ,valueBoxOutput("value2")
  ,valueBoxOutput("value3")
)

frow2 <- fluidRow(
  
  box(
    flexdashboard::gaugeOutput("chart"),width=200,
    title="Miles/gallon"
  )
)



# combine the two fluid rows to make the body
body <- dashboardBody(frow1, frow2)

#completing the ui part with dashboardPage
ui <- dashboardPage(header, sidebar, body, skin='green')

# create the server functions for the dashboard  
server <- function(input, output) { 
  
  #creating the valueBoxOutput content
  output$value1 <- renderValueBox({
    a <- mtcars[mtcars$model_car==input$model,]
    b <- a$cyl
    shinydashboard::valueBox(
      b, "Number of cylinders",icon("stats", lib='glyphicon'),
      color = "purple"
    )
  })
  
  
  output$value2 <- renderValueBox({
    c <- mtcars[mtcars$model_car==input$model,]
    d <- c$gear
    valueBox(
      d, "Number of foward gears",icon("close", lib='font-awesome'),
      color = "green"
      )
  })
  
  
  output$value3 <- renderValueBox({
    e <- mtcars[mtcars$model_car==input$model,]
    f <- e$carb
    valueBox(
      f, "Number of carburetors",icon("calendar", lib='font-awesome'),
      color = "orange"
      )
  })
  
  #creating the plotOutput content
  
  output$chart <- flexdashboard::renderGauge({
    x <- mtcars[mtcars$model_car==input$model,]
    y <- x$mpg
    flexdashboard::gauge(y, min = 0, max = 50, symbol = 'mpg')
  })
  
}


shinyApp(ui, server)

So, I would like to add an image of the selected car, for example, this'd be the image of the first car.
How can I associate an image for each row of the dataset?
And how can I put them on the app?

thanks in advance

I made a sub folder called WWW in the directory holding the app.R file and I put three image files there. The selectInput allows picking a variable name from the data frame and in each row there is an image associated with the variable name.

library(shiny)
require(shinydashboard)

DF <- data.frame(VarNames = c("Flower", "Car", "Boat"), 
                 ImgLoc = c("WWW/Img1.jpg", "WWW/Img2.jpg", "WWW/Img3.jpg"),
                 stringsAsFactors = FALSE)
ui <- dashboardPage(title = "Title",
                    dashboardHeader(

                      title= "Title Data"
                    ), 
                    dashboardSidebar(
                        selectInput("VarName", "Variable", choices = DF$VarNames)         
                    ),
                    dashboardBody(
                      imageOutput("image1")
                    )
)

server <- function(input, output) { 
  
  output$image1 <- renderImage({
    ImgTxt <- DF[DF$VarNames == input$VarName, 2]
    width<- "80%"
    height<- "20%"
    list(src = ImgTxt,
         contentType = "image/jpg",
         width = width,
         height = "auto"
    )
  }, deleteFile = FALSE)
  
}
shinyApp(ui, server)

Wow really thanks, exactly what I was looking for

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.