【问题标题】:R ShinyDashboard textOutput to HeaderR ShinyDashboard 文本输出到标题
【发布时间】:2018-04-08 06:34:27
【问题描述】:

我正在尝试在标题上获取一个自定义字段,以便人们知道上次刷新数据的时间。

在我的测试运行中,我只是在代码中添加了一个变量,但当我使用 textOutput 时,它却给了我 HTML 背景逻辑。

<div id="Refresh" class="shiny-text-output"></div>

下面是我的代码:

library (shiny)
library (shinydashboard)

rm(list=ls())

header <- dashboardHeader(
          title = "TEST",
          tags$li(class = "dropdown", tags$a(paste("Refreshed on ", textOutput("Refresh")))))

body <- dashboardBody(

  fluidRow(box(textOutput("Refresh")))
)

sidebar <- dashboardSidebar()

ui <- dashboardPage(header, sidebar, body)

server <- function(input, output) {

  output$Refresh <- renderText({
  toString(as.Date("2017-5-4"))
  })
}

shinyApp(ui, server)

这是我目前看到的:

已编辑以显示更正的代码

library (shiny)
library (shinydashboard)

header <- dashboardHeader(
  title = "TEST",
  tags$li(class = "dropdown", tags$a((htmlOutput("Refresh1")))))

body <- dashboardBody(

  fluidRow(box(textOutput("Refresh2")))
)

sidebar <- dashboardSidebar()

ui <- dashboardPage(header, sidebar, body)

server <- function(input, output) {

  output$Refresh1 <- renderUI({
    HTML(paste("Refreshed on ", toString(as.Date("2017-5-4"))))
  })

  output$Refresh2 <- renderText({
    toString(as.Date("2017-5-4"))
  })
}

shinyApp(ui, server)

【问题讨论】:

    标签: shiny shinydashboard


    【解决方案1】:

    您必须将内容作为HTML 粘贴到tags$a 中,如下所示。您还必须renderText 两次,因为不能在 UI 中使用相同的值。

    library (shiny)
    library (shinydashboard)
    
    rm(list=ls())
    
    header <- dashboardHeader(
      title = "TEST",
      tags$li(class = "dropdown", tags$a(HTML(paste("Refreshed on ", textOutput("Refresh1"))))))
    
    body <- dashboardBody(
    
      fluidRow(box(textOutput("Refresh2")))
    )
    
    sidebar <- dashboardSidebar()
    
    ui <- dashboardPage(header, sidebar, body)
    
    server <- function(input, output) {
    
      output$Refresh1 <- renderText({
        toString(as.Date("2017-5-4"))
      })
    
      output$Refresh2 <- renderText({
        toString(as.Date("2017-5-4"))
      })
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • 像魅力一样工作。谢谢@Sagar!
    • 嗨@Sagar,我刚刚注意到这个解决方案将Refresh2017-5-4 放在两个单独的行上,导致标题不必要地扩展。如何确保Refresh on 2017-5-4 只有一行?
    • 使用stackoverflow.com/questions/23233497/… 的帮助解决了这个问题,使用renderUIhtmlOutput 代替renderTexttextOutput 并在UI 中添加我的字符​​串。我已经更新了我原来的问题以显示这一点。
    • 很高兴您能够自行解决此问题。今天看到你的更新。但这是在同一行显示输出的另一种简单方法:tags$li(tags$style("#Refresh1{display:inline}"), class = "dropdown", tags$a(HTML(paste("Refreshed on ", textOutput("Refresh1")))))),
    猜你喜欢
    • 2018-02-04
    • 2020-07-23
    • 2015-10-05
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 2019-12-11
    • 2016-04-05
    • 1970-01-01
    相关资源
    最近更新 更多