mitchelloharawild / icons

R package to easily insert web icons to RMarkdown
https://pkg.mitchelloharawild.com/icons
313 stars 43 forks source link

Creating and using svg as icons in R Shiny #64

Closed Rogerjwhill closed 3 years ago

Rogerjwhill commented 3 years ago

I have a folder of svg that I need to be able to use as icons in an R shiny app. It is unclear how the functions work to load these as icons and then use them.

Would you be able to provide a working example of how to do this if it is possible. I have tired:

library(icons)

icon_set("T:/4_Software_and_Training/Identity Documents/Icons Library/Icons/NDRS Icons/",meta = list(name = "NDRS")) 

but it is not clear if this works or how to then use these icons in the kind of way discussed in this thread https://community.rstudio.com/t/use-of-open-source-icons-in-shiny/110056/3:

library(shiny)
library(icons)

icon_set("T:/4_Software_and_Training/Identity Documents/Icons Library/Icons/NDRS Icons/",meta = list(name = "NDRS")) 
#download_fontawesome()

ui <- fluidPage(
  actionButton("rocket1", "Rocket 1", icon=NULL,width=NULL,img(icons::NDRS$Academic)),
)

server <- function(input, output) {}
shinyApp(ui = ui, server = server)

Any help or advice would be appreciated. Thank you

mitchelloharawild commented 3 years ago

Here's an example of how you can use icon_set() in a shiny app. Note that you should use relative paths, as if you later choose to run your app on a server, it won't know how to find "T:/4_Software_and_Training/..."


# Obtain icon for demo (only needed to run once)
xfun::dir_create("hex")
download.file("https://raw.githubusercontent.com/mitchelloharawild/icons/master/hex/icons.svg", "hex/icons.svg")
library(shiny)

library(icons)
# Load your custom icon library and give it a name
hex_stickers <- icon_set("hex/")

# Define UI for application that draws a histogram
ui <- fluidPage(
    # Add SVG icon as an image
    actionButton("shiny", "icons!", icon=NULL,width=NULL, img(hex_stickers$icons))
)

# Define server logic required to draw a histogram
server <- function(input, output) {
}

# Run the application
shinyApp(ui = ui, server = server)
Rogerjwhill commented 3 years ago

Brilliant thank you.