shiny:change tab when click on image

无人久伴 提交于 2020-02-27 12:35:27

问题


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!



来源:https://stackoverflow.com/questions/47850836/shinychange-tab-when-click-on-image

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!