shiny:change tab when click on image

2020-07-25 11:13发布

问题:

I'm building a Shiny application and some of my users (not very familiar with this kind of layout) don't understand that the application works with tabs and they just don't see where to go from the Homepage.

That's why I want to display a big infography on the main page, and when they click on it, it automatically activates the second tab. I know how to add a link to an image:

tags(a(img(src="image.png"), href="link.com"))

And I know how to programmatically select a different tab:

updateTabsetPanel(session, inputId="navbar", selected="tab2")

But how to combine those 2 actions? Thanks,

回答1:

You can give the image an id, and use the onclick() function from shinyjs. Working example:

require(shiny)
require(shinyjs)


ui <- fluidPage(
  img(id="my_img",src="image.png",style="cursor:pointer;"),
  useShinyjs(),
  tabsetPanel(id="navbar",
              tabPanel("tab1", p("This is tab 1")),
              tabPanel("tab2", p("This is tab 2"))
  )
)



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

  shinyjs::onclick("my_img",  updateTabsetPanel(session, inputId="navbar", selected="tab2"))

}

shinyApp(ui,server)

Hope this helps!



标签: r shiny navbar