Creating Hover buttons in R igraph - html

I am trying to create nodes which will change colour when the mouse hover over it.
I used R (igraph) to plot the nodes and to generate a network.
I then create a html template using the cat().
However, I am not sure how to link a css sheet to create the hover button which should lie on top of the nodes.
require(igraph)
htmlfile = file.path('~/Dropbox/Cambridge/PhD/ICAR/AIG/Map/html/', "page1.html")
cat("<html><h1>My first HTML page from R</h1>",file = htmlfile)
cat("\n<br>Hello Web World!", append = TRUE, file = htmlfile)
set.seed(1)
E.circuit.2 <- graph_from_literal(1--2:3:4:5, 2--3,3--2, 4--5)
E.circuit.2
coordinates <- layout_with_dh(E.circuit.2)
coordinates
plot(E.circuit.2)
cat('\n<p><img src="map.png", align="center"></p>', append = TRUE, file = htmlfile)
cat("\n</html>", append = TRUE,file = htmlfile)

If i understand correctly you only have to add:
<head><link rel="stylesheet" type="text/css" href="mystyle.css"></head>
after your <html> tag. Where you change mystyle.css to the stylesheet you would like to use.

Related

How do I express html in popup on streamlit folium map?

df_final['html_file']: html files are plotly graphs
for i in range(0,len(df_final)):
p = open(df_final['html_file'][i], 'rt', encoding='UTF8')
popup = folium.Popup(components.html(p.read(), width=850, height=400, scrolling=True))
folium.Circle([df_final['lats'].iloc[i],df_final['lngs'].iloc[i]],
radius=200,
color='red',
fill=True,
#fill_color='coral',
fill_opacity=0.1,
popup=popup
).add_to(map)
map.save("index.html")
st_folium(map, returned_objects=[], width = 1300, height = 1000)
HTML and map are displayed separately - I want to put HTML in the popup
Please refer to my answer here:
Folium - add larger pop ups with data from XML file
You can embed html code in an iFrame which is then put in your popup.

Use CSS to change hover over text color in shiny navbar

I have created a shiny dashboard using the 'flatly' shiny theme. However, the theme makes my navbar headings turn green when I hover over them. I would prefer them to turn orange instead. I have tried modifying this with custom html code but nothing changes.
The code I have attempted is:
ui <- navbarPage(theme = shinytheme('flatly'), collapsible = TRUE,
HTML('<a style="text-decoration:none;cursor:default;color:#FFFFFF;" class="active" href="#">World Happiness Dashboard</a>'), id="nav",
tags$head(tags$style('.navbar-nav .nav-item.active .nav-link,
.navbar-nav .nav-item:hover .nav-link {
color:orange;
}')),
windowTitle = "World Happiness Dashboard",
the best approach to solve your issue will be to do an inspection with browser dev tools on the element you want to change.
at dev tools you will be able to override css properties to your custom and see if it works.
take note at the name of the element (name of the tag where the element is, provably a div), and use it on your css code to override it.
Here what you need:
https://developer.chrome.com/docs/devtools/
It's quite easy to achieve with the sass package. Here is a step-by-step approach:
Create a SASS file and call it something like my-style.sass
In the SASS file, add the things you want to change. You'll need to do some digging around in the browser, as suggested by #raOliveira, but for your case, you probably want to add the following:
.navbar-default:hover .navbar-nav>li>a:hover
color: orange
.navbar-default .navbar-brand:hover
color: orange
Add the folder www if you don't have it already.
Run this code in the start of your app.R file or in your global.R file:
library(sass)
sass(
sass_file("my-style.sass"),
output = "www/my-style.css"
)
This will create a custom CSS based on the SASS file you created.
Reference the CSS in your shiny UI:
tags$head(
tags$link(href = "my-style.css", rel = "stylesheet", type = "text/css")
)
Reproducible Shiny code
library(shiny)
library(sass)
sass(
sass_file("my-style.sass"),
output = "www/my-style.css"
)
ui <- navbarPage(
title = "test",
theme = shinythemes::shinytheme('flatly'),
collapsible = TRUE,
tags$head(
tags$link(href = "my-style.css", rel = "stylesheet", type = "text/css")
),
tabPanel("Plot"),
tabPanel("Summary"),
tabPanel("Table")
)
server <- function(input, output) {}
shinyApp(ui = ui, server = server)
Output:
The tabs and the title are orange when I hover them:

How would you use rivets.js with HTML template tags

HTML has a draft specification for a < template > tag. Details here: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/template
I'm thinking about using Rivets.JS on a new project, I also want to use this new template tag.
Can the two be made to work together nicely?
I imagine I'd want to tell rivets something along the lines of 'get this template, bind it to this data and output the result here'.
You can copy the template as your normally would, and then use Rivets to bind to your new element. demo # jsfiddle
HTML:
<template id="demo">
<p id="tag">{ demo.info }<p>
</template>
Javascript:
var demo = {
info: "Test string"
}
// Copy template data to visible DOM
var el = document.getElementById("demo");
var clone = document.importNode(el.content, true);
document.body.appendChild(clone);
// Bind using Rivets as normal
var tag = document.getElementById("tag");
rivets.bind(tag, { demo: demo });

Open the next html file in the directory

I am making a sort of "html app" which involves a very large number of seperate .html files. The 'app' is a sort of 'pro tips' thing, where on every page, is a life tip. I am wondering if there is a code for opening the next html file within the same directory, instead of changing the next tip's in each html file to open it.
Example:
Next Tip
Then in the next tip's html file I would have to put:
Next Tip
And so on:
Next Tip
Sorry if I am not being clear enough.
If you want a client-side only solution this might work for your scenario. Give the a tag an id and add the script part to very page. It will parse your current filename, parses the number from it, adds one and buildsup the next url, replacing that value on your a tag
<html>
<head>
<script src="nav.js"></script>
</head>
<body>
<!-- REMEMBER TO PUT THE ID ON IT -->
<a id="next" href="n.html">next</a>
</body>
</html>
and create a new file called nav.js in your folder and add this code to it:
window.onload = function() {
var a=document.getElementById('next'),
l=document.location.href,
s=Math.max(l.lastIndexOf('\\'),l.lastIndexOf('/')),
d=l.indexOf('.'),
f=l.substring(s+1,l.indexOf('.')),
p=l.substring(0,s+1),
e=l.substring(s+1+f.length, l.length),
n=parseInt(f,10) + 1;
if (a) {
a.href= p + n.toString()+e;
}
};
You could rename all the files into an ordered way, for example tip1.htm, tip2.htm, etc. with Bulk Rename Utility or Ant Renamer if you don't want to get your hands dirty with lots of CMD and PowerShell. After that, add this php code to your main page:
<?php
$file="";
$counter=0;
$arr=array('tip','0','.htm');
function next_file()
{
$counter=$counter+1;
$arr[1]=streval($counter);
include(join("", $arr));
}
?>
Then just call the next_file() function every time the user clicks on the link.
If you have files 1.html, 2.html, etc. this will work.
On your html pages include a blank div (where you want the link to be):
<div id="link"></div>
Include a separate javascript file in the pages:
<script src="app.js"></script>
And then write this in that javascript file:
window.onload = function() {
var maxPages = 3;
var url = window.location.pathname;
var filename = url.substring(url.lastIndexOf('/')+1);
var thenumber = filename.replace(".html", "");
thenumber++;
if (thenumber > maxPages) {
document.getElementById("link").innerHTML = 'Next Tip';
} else {
document.getElementById("link").innerHTML = 'Next Tip';
}
}
Set maxPages so when you get to the end of all the tips, you will go back to the beginning.

setting source of an image component not working

I'm pulling in a xml-file with just one result in it. One of the nodes is picture, which contains a link to a picture. This is the xml-file:
<artist>
<id>502</id>
<name>Bad Religion</name>
<picture>http://userserve-ak.last.fm/serve/500/46612615/Bad Religion BR 2010.jpg</picture>
<twitter></twitter>
</artist>
I've tested the url, and it's correct. This is how I try to bind the url to the image instance (artistPic), but it's not working. Displaying the artist name does work.
var artist:XMLList = new XMLList(event.result);
artistPic.source = artist.picture;
lblArtistName.text = artist.name;
That's because artist.picture returns an XMLList object. Try the following code :
var artist:XML = new XML(event.result);
artistPic.source = String(artist.picture[0]);
lblArtistName.text = artist.name; // This one is probably transtyped automagically by Flex.