Skip to content

Instantly share code, notes, and snippets.

@timelyportfolio
Last active August 29, 2015 14:14
Show Gist options
  • Select an option

  • Save timelyportfolio/c8b2ea18d227d7f9761f to your computer and use it in GitHub Desktop.

Select an option

Save timelyportfolio/c8b2ea18d227d7f9761f to your computer and use it in GitHub Desktop.
dynamically change favicon in browser with shiny and R.devices

Favicon from Plot in R Shiny

Once I saw R.devices function toFavicon I just had to figure out how to use it to dynamically change the favicon with a plot from R. Here is one way to accomplish it, but note the dangerous eval(parse(...)) in line 14, so be careful if you are using for nonpersonal use.

Non-Shiny Use

For non-shiny use, we can see it work with htmltools.

library(htmltools)
library(R.devices)

html_print(
  HTML(
    toFavicon( plot(1:10) )
  )
  ,viewer = browseURL #  since RStudio Viewer doesn't show favicon open in browser
)
library(shiny)
library(htmltools)
library(R.devices)
ui = shinyUI(fluidPage(
textInput("plotExpr","plot:","plot(1:10)")
,uiOutput("fav")
))
server = function(input, output){
output$fav= renderUI( {
return(
HTML(
gsub(
x = toFavicon( eval(parse(text=input$plotExpr)) )
,pattern = '<script>'
,replacement='
<script>
var l = document.getElementsByTagName("link")
for( i = 0; i< l.length; i++){
if(l[i].rel && l[i].rel==="icon"){
l[i].remove();
}
}
'
)
)
)
})
}
runApp( list( ui=ui, server=server ) )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment