[英]Set relative link / anchor in R Shiny
I would like to create a drillable graphic that links to other places in my Shiny App.
我想创建一个可钻取的图形,链接到我的 Shiny 应用程序中的其他地方。
library(tidyverse)
library(shiny)
library(shinydashboard)
ui <- dashboardPage(
dashboardHeader(title="My Fitness Dashboard",titleWidth =400),
####sidebar#####
dashboardSidebar(width = 240,
sidebarMenu(startExpanded = TRUE,
br(),
br(),
br(),
menuItem(text = 'Overview',
tabName = "fitDash"),
menuItem(text = 'Floors',
tabName = "floors")
)), #close dashboardSidebar
dashboardBody(
tabItems(
tabItem(tabName = 'fitDash',
uiOutput("dashboard"),
), #close tabItem
tabItem(tabName = 'floorsUp',
fluidRow(
column(width = 10,
box(width = 12,
textOutput('floorsClimbed') #plot comments
) #close box
) #close column
) #close fluidRow
) #close tabItem
) #close tabItems
) #close dashboardBody
) #close dashboardPage
###### Server logic required to draw plots####
server <- function(input, output, session) {
output$dashboard <- renderUI({
tags$map(name="fitMap",
tags$area(shape ="rect", coords="130,250,240,150", alt="floors", href="https://www.w3schools.com"),
#tags$area(shape ="rect", coords="130,250,240,150", alt="floors", href="/floorsClimbed"),
tags$img(src = 'fitbit1.jpg', alt = 'System Indicators', usemap = '#fitMap')
) #close tags$map
})
output$floorsClimbed <- renderText({
"I walked up 12 floors today!"
})
} #close server function
# Run the application
shinyApp(ui = ui, server = server)
The following line works perfectly to link to an external site:以下行完美地链接到外部站点:
tags$area(shape ="rect", coords="130,250,240,150", alt="floors", href="https://www.w3schools.com")
However, I would actually like to internally link to the "floorsUp" tab with something like:但是,我实际上想在内部链接到“floorsUp”选项卡,如下所示:
tags$area(shape ="rect", coords="130,250,240,150", alt="floors", href="/floorsUp")
You could add an onclick listener to your element.您可以将 onclick 侦听器添加到您的元素。 Unfortunately, i can´t reproduce your example, but i modified an example app from the shiny docu.
不幸的是,我无法重现您的示例,但我修改了 shiny 文档中的示例应用程序。
You can send a message from javascript to shiny and trigger the javascript code by the onclick
listener.您可以从 javascript 向 shiny 发送消息,并通过
onclick
监听器触发 javascript 代码。
shiny::tags$a("Switch to Widgets", onclick="Shiny.onInputChange('tab', 'widgets');")
The parameters of onInputChange
are the id
and value
. onInputChange
的参数是id
和value
。 On the server side you can access the values by input$id
.在服务器端,您可以通过
input$id
访问这些值。 In our case it is input$tab
.在我们的例子中,它是
input$tab
。 The resulting value would be widgets
.结果值将是
widgets
。
Then we can use updateTabItems
to update the tabItem:然后我们可以使用
updateTabItems
来更新 tabItem:
observeEvent(input$tab, {
updateTabItems(session, "tabs", input$tab)
})
Additional details:额外细节:
Note that the input only fires on the server side if the value changes.请注意,如果值更改,输入仅在服务器端触发。 Therefore, we might want to add a stochastic component to the value we send.
因此,我们可能希望向我们发送的值添加一个随机分量。
"var message = {id: \"tab\", data: \"widgets\", nonce: Math.random()};
Shiny.onInputChange('tab', message)")
You can find more infos here: https://shiny.rstudio.com/articles/js-send-message.html .您可以在这里找到更多信息: https://shiny.rstudio.com/articles/js-send-message.html 。
Reproducible example:可重现的例子:
library(shiny)
ui <- dashboardPage(
dashboardHeader(title = "Simple tabs"),
dashboardSidebar(
sidebarMenu(
id = "tabs",
menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
menuItem("Widgets", tabName = "widgets", icon = icon("th"))
)
),
dashboardBody(
tabItems(
tabItem(tabName = "dashboard",
h5("Click the upper left hand corner of the picture to switch tabs"),
tags$map(name="fitMap",
tags$area(shape ="rect", coords="10,10,200,300", alt="floors",
onclick="var message = {id: \"tab\", data: \"widgets\",
nonce: Math.random()}; Shiny.onInputChange('tab', message)"),
tags$img(src = 'https://i.stack.imgur.com/U1SsV.jpg',
alt = 'System Indicators', usemap = '#fitMap')
)
),
tabItem(tabName = "widgets",
h2("Widgets tab content")
)
)
)
)
server <- function(input, output, session) {
observeEvent(input$tab, {
updateTabItems(session, "tabs", input$tab$data)
})
}
shinyApp(ui, server)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.