【问题标题】:Set relative link / anchor in R Shiny在 R Shiny 中设置相对链接/锚点
【发布时间】:2020-08-28 19:21:03
【问题描述】:

我想创建一个可钻取的图形,链接到我闪亮应用程序中的其他地方。

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)

以下行完美地链接到外部网站:

tags$area(shape ="rect", coords="130,250,240,150", alt="floors", href="https://www.w3schools.com")

但是,我实际上想在内部链接到“floorsUp”选项卡,如下所示:

tags$area(shape ="rect", coords="130,250,240,150", alt="floors", href="/floorsUp")

【问题讨论】:

    标签: html r shiny anchor relative-url


    【解决方案1】:

    您可以为您的元素添加一个 onclick 侦听器。不幸的是,我无法重现您的示例,但我修改了闪亮文档中的示例应用程序。

    您可以从 javascript 向 shiny 发送消息,并通过 onclick 侦听器触发 javascript 代码。

    shiny::tags$a("Switch to Widgets", onclick="Shiny.onInputChange('tab', 'widgets');")
    

    onInputChange的参数是id和value。在服务器端,您可以通过input$id 访问这些值。在我们的例子中是input$tab。结果值为widgets。

    那么我们可以使用updateTabItems来更新tabItem:

     observeEvent(input$tab, {
        updateTabItems(session, "tabs", input$tab)
      })
    

    其他详情:

    请注意,如果值更改,输入只会在服务器端触发。因此,我们可能希望在我们发送的值中添加一个随机分量。

    "var message = {id: \"tab\", data: \"widgets\", nonce: Math.random()};
     Shiny.onInputChange('tab', message)")
    

    您可以在这里找到更多信息:https://shiny.rstudio.com/articles/js-send-message.html。

    可重现的例子:

    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)
    

    【讨论】:

    • 谢谢。我将不得不解压缩您提供的 JavaScript,因为我也在尝试学习这种语言。非常非常有帮助,托尼​​奥
    猜你喜欢
    • 1970-01-01
    • 2017-03-29
    • 1970-01-01
    • 2012-07-25
    • 1970-01-01
    • 2022-07-05
    • 1970-01-01
    • 1970-01-01
    • 2017-06-22
    相关资源
    最近更新 更多