ActionButton with a href to another reactive object

2,671 views
Skip to first unread message

charlott...@gmail.com

unread,
Nov 3, 2016, 5:46:56 AM11/3/16
to Shiny - Web Framework for R
Hi everyone,

I have a shiny app with a really long page, and I would like to create an actionButton that allows user to go directly to one particular reactiveObject.

As I already programed in html I thought about href and local anchor.

For a text no problem, I would add an href = "#Object" to the a() (http://stackoverflow.com/questions/28605185/create-link-to-the-other-part-of-the-shiny-app), but for an actionButton, I can not figure out how to do that (perhaps with a div?).

Here is my code for an reproducible example:

library(shiny)

runApp
(
 
# Ui
  list
(ui = pageWithSidebar(
    headerPanel
("Test App"),
   
    sidebarPanel
(
      div
(actionButton("Go", "Go to the Button"), href ="#try" )),
   
    mainPanel
("Hello World!",
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              br
(),
              uiOutput
("try")
             
)
 
),
 
 
# Server
  server
= function(input, output, session) {
   
    output$try
<- renderUI({
      actionButton
("Well", "well done!")
   
})
 
}
))

Sorry for the br(), but I needed to recreate the length of the page.

Do you have any idea for doing that ?

Thanks in advance for your help

Cheers

charlott...@gmail.com

unread,
Nov 3, 2016, 5:58:58 AM11/3/16
to Shiny - Web Framework for R
And I need to precise that I would like that the whole the actionButton (not only the text) have a link to the reactiveObject

(not like that:

actionButton(inputId = "coucou", label = HTML("<a href='#try'>Go to plot 2</a>"))


)

so perhaps with a onlick = "#try", but it did not work neither:

library(shiny)

runApp
(
 
# Ui
  list
(ui = pageWithSidebar(
    headerPanel
("Test App"),
   
    sidebarPanel
(

      actionButton
("coucou", "coucou", onclick ='#try')),

Thanks again

Laz C. Peterson

unread,
Nov 3, 2016, 9:32:00 AM11/3/16
to charlott...@gmail.com, Shiny - Web Framework for R
Hello Charlotte --

I know that the way Shiny processes some of the code makes it a little more difficult to get that custom functionality you're looking for -- at least for me.  Give this one a try:

library(shiny)

ui<-fluidPage(
    tags$p(style='text-align:center;','Top of the page!'),
    tags$div(style='text-align:center;',tags$button(type='button',onClick='$("html,body").animate({scrollTop:$("#myAnchor").offset().top},500)',class='btn btn-primary','Go to Anchor')),
    tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),
    tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),
    tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),
    tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),
    tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),
    tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),
    tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),tags$br(),
    tags$p(name='myAnchor',id='myAnchor',style='text-align:center;','Bottom of the page!')
)

server<-function(input,output,session){
}

shinyApp(ui,server)

~Laz Peterson
Paravis, LLC
--
You received this message because you are subscribed to the Google Groups "Shiny - Web Framework for R" group.
To unsubscribe from this group and stop receiving emails from it, send an email to shiny-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/shiny-discuss/5230ae7f-77d4-4f1b-8b31-35dc7bbd981e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

 

Joe Cheng

unread,
Nov 3, 2016, 3:56:41 PM11/3/16
to Laz C. Peterson, charlott...@gmail.com, Shiny - Web Framework for R
Bootstrap makes it dead simple to make links look like buttons:

Example:
tags$a(href="#try", class="btn btn-default",
  "Go to plot 2"
)

Laz C. Peterson

unread,
Nov 3, 2016, 6:00:03 PM11/3/16
to Joe Cheng, charlott...@gmail.com, Shiny - Web Framework for R
Joe -

When I've tried doing that, the anchor links don’t seem to work.  I’ve only been able to get it to work with onClick Javascript.

~ Laz Peterson
Paravis, LLC

Joe Cheng

unread,
Nov 3, 2016, 7:51:49 PM11/3/16
to Laz C. Peterson, charlott...@gmail.com, Shiny - Web Framework for R
Does this not work for you?

library(shiny)

ui <- fluidPage(
  tags$a(href="#try", class="btn btn-default", "Jump"),
  div(style = htmltools::css(height = "5000px")),
  uiOutput("try")
)

server <- function(input, output, session) {
  output$try <- renderUI("hello")
}

shinyApp(ui, server)

Laz C. Peterson

unread,
Nov 3, 2016, 8:35:33 PM11/3/16
to Joe Cheng, charlott...@gmail.com, Shiny - Web Framework for R
Yes.  It does.

<facepalm>

I had a type-o.  Well hey, this one helped me out too.  :-)

Thanks Joe.


~ Laz Peterson
Paravis, LLC

George Miranda

unread,
May 2, 2017, 12:33:43 PM5/2/17
to Shiny - Web Framework for R, j...@rstudio.com, charlott...@gmail.com
Does this work if linking to a different page, for instance if linking across a navbarPage? When I try using navbarPage and try linking from one tabPanel to another, it doesn't work for me. 


library(shiny)

ui <- navbarPage( title = "",
    tabPanel("Home", fluidRow(tags$a(href="#try", class="btn btn-default", "Jump")) ),
    
    tabPanel("Other", fluidRow( uiOutput("try") ) )
)

server <- function(input, output, session) {
    output$try <- renderUI("hello")
}

shinyApp(ui, server)

Joe Cheng

unread,
May 3, 2017, 9:02:12 PM5/3/17
to George Miranda, Shiny - Web Framework for R, charlott...@gmail.com
Sorry, that won't work--you can only use #anchor style href's to jump to something that's already showing (unless you write some custom JS to get exactly the behavior you want).

DCR

unread,
May 4, 2017, 10:13:40 AM5/4/17
to Shiny - Web Framework for R, georgem...@gmail.com, charlott...@gmail.com
Hi - 

To follow up with Joe's response, using some custom JS is the way to go. The href values are regenerated at each run making it impossible to using href. One work around is to assign a value to the each tabPanel, and then target that with some js. This SO post has an example on how to do that: https://stackoverflow.com/questions/36412407/shiny-add-link-to-another-tabpanel-in-another-tabpanel. For readability, it might be easier to put the js in a different file, and then include the js file using tagList

ui <-tagList(
           tags$head
(includeScript("/path/to/js"),
           navbarPage
(...)                          ## tabs go here
 
)


As an example, your tab panel(s) may look something like this.

tabPanel("My First Tab",
          value = "tab1",
          h1("Some title here"),
          p("...some cool stuff will go here..."),
          tags$a("Go to tab 2", 
                  onclick="fakeClick('tab2')", 
                  class="btn btn-default")
               ),

tabPanel("My Second Tab", 
          value = "tab2",
          h1("Some title here"),
          p("...some cool stuff will go here..."),
          tags$a("Go to tab 1", 
                  onclick="fakeClick('tab1')", 
                  class="btn btn-default")
               )


Hope that helps!

- David

George Miranda

unread,
May 4, 2017, 12:51:22 PM5/4/17
to Shiny - Web Framework for R, georgem...@gmail.com, charlott...@gmail.com
Thanks for that JS! I don't know much JS and yet it worked very easily for me by copy/pasting and changing the tab names. Thanks again!
Reply all
Reply to author
Forward
0 new messages