Add external hyperlink to tabPanel or navbarMenu in r Shiny - html

I am trying to add external hyperlinks to the tabPabel and navbarMenu tabs/dropdowns in a navbarPage setup in Shiny (using bootstrapPage). I found multiple questions that refer to linking to another tab within a Shiny app, but I want to specifically link to another webpage without opening a new browser window.
I found the following questions that kind of get there:
How to direct to another web page after clicking tabPanel in Shiny App
Open URL by tabPanel in Shiny
The second question is what I want to do; however, when I use the following method to accomplish this, it adds a "phantom" tab:
tabPanel(a("Open Sales Gsheet", href="http://google.com", target="_blank"))
Here is some example code for the Shiny app setup that I am working with:
library(shiny); library(shinythemes)
ui <- bootstrapPage("",
navbarPage(
id = "navbar",
theme = shinytheme("yeti"),
title = a("Home", href = "https://google.com", style = "color:white;"), ## page title with hyperlink and browser tab title (works as intended)
tabPanel(title = HTML("Panel_1</a></li><li><a href='http://google.com' target='_blank'>test")), ## tabPanel hyperlink test (adds "phantom" tab)
navbarMenu(title = "Test Menu",
tabPanel(title = a("Open Sales Gsheet", href="http://google.com", target="_blank")) ## navbarMenu hyperlink test (adds "phantom" option)
)
)
)
server <- function(input, output, session) {
## empty server
}
shinyApp(ui, server)
Here is a screenshot of the "phantom" tab issue:
https://i.imgur.com/tIYbhzT.png
As you can see, both the tabPanel and navbarMenu tabs/dropdowns have additional "phantom" tabs that have been added as a result. The first question I posted above shows an answer that involves editing the html code (or the list that is produced in R)... but I cannot figure out how to do this with a tabPanel or navbarMenu object.
I just want this to look like a normal navbarPage dropdown where the tabPanel and navbarMenu selections link to an external site (in the same browser window - browseURL as an observeEvent in the server script does not work since it opens in another window). Any help would be appreciated!

It's tricky to add custom elements in a shiny navbar page but it can be done with some javascript. The following code should add your link to the dropdown menu in the navbar. Save it as a .js file in your app's base directory then include the script in your ui function.
navAppend.js in your app's base directory:
$(document).ready(function() {
$(".navbar .container-fluid .navbar-nav .dropdown .dropdown-menu").append('<li>Open Sales Gsheet</li>');
});
in your ui:
ui <- tagList(
tags$head(includeScript("navAppend.js")),
navbarPage(
id = "navbar",
theme = shinytheme("yeti"),
title = a("Home", href = "https://google.com", style = "color:white;"), ## page title with hyperlink and browser tab title (works as intended)
# nav menu the link will be added to
navbarMenu(title = "Test Menu")
)
)

Related

bookdown: cross-referencing content in tabbed section

I'm working on a single RMD file with 'output: bookdown::html_document2'. I noticed that when cross-referencing to figures in tabbed sections (e.g. # Header {.tabset}), clicking on the link works nicely for content in the first tab but not for any of the following tabs. I mean, clicking on the number linking to the figure in the second tab does not open/activate the second tab.
Among the tons of questions regarding cross-referencing, I was not able to find any dealing with the same problem. I'm afraid that it might just not be possible to 'activate' a tab by clicking on a cross-reference, but I do hope to find some workaround. I'm happy about any hints.
Here's a minimal example:
---
title: "Untitled"
date: "17 2 2021"
output:
bookdown::html_document2:
number_sections: FALSE
---
# First section {.tabset}
## Subsection 1
```{r plot1, fig.cap="A first figure"}
plot(cars)
```
## Subsection 2
```{r plot2, fig.cap="A second figure"}
plot(cars)
```
# Second section
Here I want to cross-reference Figures \#ref(fig:plot1) and \#ref(fig:plot2)
```
If we take a look at the final html created, the "figure number link" is a normal html anchor tag linking to the image itself, so clicking on it the page will be scrolled to the figure position, without activating the containing tab.
As suggested by #cderv I would add some js code in order to achieve your desired result.
First of all I would work on naming:
set a naming convention for images contained in tabs (e.g. simply adding a fixed prefix "TBIMG-")
set a naming convention for tabs containing this kind of images (e.g. another custom prefix "TBTAB-" + figure name)
So we'll end up having images with name "TBIMG-name1", "TBIMG-name2", etc.. contained in "TBTAB-name1", "TBTAB-name2", etc.
Now we just need to bind the functionality to the click event of "figure number links" (only the ones having our special prefix).
In their href attribute we'll find the image id.
This one can lead us to the containing tab (using our second custom prefix)
Then we just activate the tab and finally we scroll the page to the tab itself.
Here's the JS code you need to add:
$(document).on("click", 'a[href^="#fig:TBIMG"]', function(event){
//this in order to prevent the page to scroll to the image itself
event.preventDefault();
//from the img name we build the tab name
var tabKey = 'TBTAB' + $(this).attr('href').replace('#fig:TBIMG', '');
//set the tab active
var tabPlc = $('.nav[role="tablist"] a[href="#' + tabKey + '"]')
tabPlc.tab('show');
//the page scrolls to the tab containing the image
$("html, body").animate({ scrollTop: tabPlc.offset().top }, 700);
});
Maybe this could help you:
---
title: "Untitled"
date: "17 2 2021"
output:
bookdown::html_document2:
number_sections: FALSE
---
# First section {.tabset}
## Subsection 1
```{r plot1, fig.cap="A first figure"}
plot(cars)
```
## Subsection 2
```{r plot2, fig.cap="A second figure"}
plot(cars)
```
# Second section
Here I want to cross-reference Figures [1](#subsection-1) and [2](#subsection-2)
The links are good, but not working on Firefox neither Chromium. I dot not know why it does not refresh the window when we click on the link.
Here is the Html code that works to activate the tab (Subsection 2) (I do know how to implement it here, sorry):
<a role="tab" data-toggle="tab" href="#subsection-2" aria-controls="subsection-2" aria-expanded="true">Subsection 2</a>

How to refresh data in other components (Angular 7, router.navigate) when navigating from one to another?

I am new to front-end dev, application is in Angular 7
My homepage contains list of items, clicking on each item will redirect to another page. While navigating I want the title bar (third component) to update with respect to the item which I clicked. Right now it only updates if I manually reload the page
I am using router for navigation
router.navigate([route]);
.......EDIT.............
I have tried the following link....
https://medium.com/#rakshitshah/refresh-angular-component-without-navigation-148a87c2de3f
but it is throwing error - Object is unsubscribed,
Also tried activatedrouter instead of Router
try to update the title with this
function updateDiv()
{
$( "#here" ).load(window.location.href + " #here" );
}
you can change #here(id) for .class(class) which you ill set on that title.it should update that part of a page without reloading
I refreshed the page after navigation. The navigation method is in router.
router.navigate([next]);
window.location.href = 'next';
next(string) is the path- where you want the current page to navigate.

[Shiny]: Add link to another tabPanel in another tabPanel

I'm trying to put a link on my "home" tabPanel to all others tabPanels of my app.
The idea is as follows:
ui = navbarPage("",
tabPanel("home",
fluidPage(
fluidRow(box("this 1st box should lead me to tab1a")),
fluidRow(box("this 2nd box should lead me to tab1b")),
fluidRow(box("this 2nd box should lead me to tab2")))
),
navbarMenu("tab1",
tabPanel("tab1a"),
tabPanel("tab1b")),
tabPanel("tab2")
)
shinyApp(ui, server=function(input, output) {})
I've seen the answer in Add link panel tabs in Shiny with various top level navigation bars, but I couldn't implement it on my code, since it deals with html (which i've never worked before, so I'm not familiar with the functions etc) and the code considers tabPanels within the same tab (not sure if that's why it didn't work here, if maybe it didn't work because the tabs I'm trying to link are on a navbarPage or something).
Can anyone help me or tell me where i could learn how to implement this on my example?
This answer is purely JavaScripted, but very minimal, I guess. Since Shiny creates tabs with random number Ids, and does not give access to the Ids it used, this has do be done client-sided. But there is no knowledge of JavaScript needed to implement this to other scenarios. The JavaScript part is just for Copy/Paste and the trigger command is easy to understand.
What did I do? I installed a function, that finds the Navbar link corresponding to the desired tab, and just clicks it. This utility can be added to any element with the "onclick" attribute. There are no special tags (e.g. no "a" tag) required.
The code below should make it easy to customize this solution to fit your needs.
Note: I used the original Code with the box, although it does not have any visual effect.
Code:
library(shiny)
library(shinydashboard)
ui = shinyUI(
navbarPage("Header",
tabPanel("home",
tags$head(tags$script(HTML('
var fakeClick = function(tabName) {
var dropdownList = document.getElementsByTagName("a");
for (var i = 0; i < dropdownList.length; i++) {
var link = dropdownList[i];
if(link.getAttribute("data-value") == tabName) {
link.click();
};
}
};
'))),
fluidPage(
fluidRow(box("this 1st box should lead me to tab1a", onclick = "fakeClick('tab1a')")),
fluidRow(box("this 2nd box should lead me to tab1b", onclick = "fakeClick('tab1b')")),
fluidRow(box("this 2nd box should lead me to tab2", onclick = "fakeClick('tab2')"))
)
),
navbarMenu("tab1",
tabPanel("tab1a", "Some Text inside Tab 1a."),
tabPanel("tab1b", "Some Text inside Tab 1b.")
),
tabPanel("tab2", "Some Text inside Tab 2.")
)
)
server = function(input, output, session){}
runApp(shinyApp(ui, server), launch.browser = TRUE)
Have fun!

How can I pass a value when creating a new tab panel with CSJS

I want to create a new tabbed panel for the Dojo tab container using CSJS like:
dijit.byId('#{id:djTabContainer1}').createTab({ tabTitle: Math.random()});
The default tab panel has an panel that will use the iframe tag and I want to pass in the above call the src html attribute to the panel.
Question : I can specify a url to load in the iframe. Is there a way to pass this?
It seems like the createTab only does certain tab related parameters like action and tabTitle.
Howard
The syntax is somewhat obscure here. Starting with the code in the ExtLib demo app:
XPagesExt.nsf/Core_DynamicTabs.xsp
Change the script in button4 to:
dijit.byId('#{id:djTabContainer1}')
.createTab({
"newName":'Tab'+Math.random(),
"newHref":'/XPagesExt.nsf/page5.xsp'})
to match the syntax you're requesting.
And, in the tab that's referenced by defaultTabContent, change the title and href to use those passed URL parameters:
<xe:djTabPane xp:key="doc" id="djTabPane2"
title="${javascript:/*load-time-compute*/param.newName}"
href="${javascript:/*load-time-compute*/param.newHref}"
It will create the tab and will attempt to load the href contents. I'm not seeing it as an iframe though - it's just a container div.

In Pelican, how to create a page dedicated to hosting all the blog articles?

In pelican, by default, blog articles are listed on the index.html file.
What I want instead is that I use a static page as my home page and put all the blog articles on a dedicated "Blog" page.
How can I get this done?
While there are several possible methods for achieving your desired goals, I would start with the following changes to your settings file:
SITEURL = '/blog'
OUTPUT_PATH = 'output/blog'
PAGE_URL = '../{slug}.html'
PAGE_SAVE_AS = '../{slug}.html'
DISPLAY_PAGES_ON_MENU = False
DISPLAY_CATEGORIES_ON_MENU = False
MENUITEMS = [('Home', '/'), ('Blog', '/blog/')]
Put your blog posts in content/ as usual, and then create your home page with the following headers and save as content/pages/home.md:
Title: Home
URL: ../
Save_as: ../index.html
This is the home page.
Caveats:
Dynamic navigation menu generation has been effectively turned off since it doesn't work well with this configuration. Highlighting for the currently-active menu item — a feature you normally get out-of-the-box — will not be present in this configuration and, if desired, must be implemented separately in your theme.
If your theme's base.html template has a link to your site home that depends on SITEURL (e.g., as the notmyidea theme does), you will need to change the link to point to <a href="/"> instead.
Set the following in the pelicanconf
DIRECT_TEMPLATES = ['blog']
PAGINATED_DIRECT_TEMPLATES = ['blog']
1st line will set blog.html for the articles
2nd line will allow pagination of blog.html file
For the index page, create a pages folder in the content directory and create the .md file there and set save_as:index.html this will save the md file as index.html
This is covered in the Pelican FAQ
- "How can I override the generated URL of a specific page or article?"
Basically, in your contents folder, create two subfolders:
/contents/blogs, which will store all your blog entries
/content/pages, which will store your other static pages (including your home page)
In the pages subfolder, create a file (e.g. home.rst) with the option :save_as: index.html, which will make this file your home page. E.g.:
Home
####
:date: 2015-05-22 12:30
:url:
:save_as: index.html
This is my home page
In your pelicanconf.py file, specify the following options:
DISPLAY_PAGES_ON_MENU = False
DISPLAY_CATEGORIES_ON_MENU = True
USE_FOLDER_AS_CATEGORY = True
PATH = 'content'
ARTICLE_PATHS = ['articles',]
PAGE_PATHS = ['pages',]
MENUITEMS = ()
You should now have a home page and a contents bar with a Blogs menu.
If you want to add more menus to the contents bar (for example an About or CV menu), create the corresponding files in your pages folder, and add them to MENUITEMS:
MENUITEMS = (
('About', '/pages/about.html'),
('CV', '/pages/cv.html'),
)
I have an answer similar to the one Justin Mayer gave, except in mine I change blog article urls instead of page urls.
I've been getting the following error when trying to use the answer above, so it might be useful to other people having the same issue
ERROR: Skipping volunteering.rst: file '../volunteering.html' would be written outside output path
ERROR: Skipping presentations.rst: file '../presentations.html' would be written outside output path
Make all article urls to be under 'blog/' url
ARTICLE_URL = "blog/{date:%Y}-{date:%m}-{date:%d}-{slug}.html"
ARTICLE_SAVE_AS = "blog/{date:%Y}-{date:%m}-{date:%d}-{slug}.html"
Put blog index under 'blog/' url
INDEX_SAVE_AS = "blog/index.html"
Add a explicit menu item for blog index
MENUITEMS = [
('home', '/'),
('blog', '/blog'),
]
As your page is now an index page, automatically displaying link to that page in the menu will lead to a broken link, so you will have to set the following option and specify the following flag
DISPLAY_PAGES_ON_MENU = False
For the new index page, add a directive save_as, like Justin Mayer pointed it out. Here how it looks in rst
About
=====
:slug: about
:category: About
:save_as: index.html
This should give you a home page and an index page for articles.
When you want to add more static pages, you will also need to add them in menu items that still contains '/pages' prefix in the url if you want links to the pages appear in a menu. i.e for the volunteering.rst with the following content,
Volunteering
============
:slug: about
:category: About
Your MENUITEMS variable will look like the following
MENUITEMS = [
('home', '/'),
('blog', '/blog'),
('volunteering', '/pages/volunteering'),
]
I tested this answer on pelican 4.2.0.
You can use the following settings to put the index file for example at /blog/index.html.
INDEX_SAVE_AS = 'blog/index.html'
INDEX_URL = 'blog/'
Then you created a home.md page and use "save_as: index.html" directive for the actual home page.