2

I am trying to get a custom field on the header so people know when the last time the data was refreshed.

In my test runs, I had it work when just putting a variable in the code but when I use textOutput it is giving me the HTML background logic instead.

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

Below is my code:

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)

This is what I am currently seeing:

enter image description here

EDITED to show corrected code

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)
Kevin
  • 1,974
  • 1
  • 19
  • 51

1 Answers1

5

You will have to paste the content as HTML inside tags$a, as shown below. You will also have to renderText twice, as the same value cannot be used in the 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
  • 2,778
  • 1
  • 8
  • 16
  • Works like a charm. Thank you @Sagar! – Kevin Oct 27 '17 at 11:24
  • Hi @Sagar, I just noticed this solution is putting `Refresh` on and `2017-5-4` on two separate lines causing the header to expand unnecessarily. How do I make sure `Refresh on 2017-5-4` is only one line? – Kevin Oct 27 '17 at 11:41
  • Figured this out using help from this https://stackoverflow.com/questions/23233497/outputting-multiple-lines-of-text-with-rendertext-in-r-shiny Using `renderUI` and `htmlOutput` in place of `renderText` and `textOutput` and adding my string in the UI. I have updated my original problem to show this. – Kevin Oct 27 '17 at 12:05
  • 2
    Glad you were able to fix that on your own. Saw your update today. But here's another easy way to show the output on same line: `tags$li(tags$style("#Refresh1{display:inline}"), class = "dropdown", tags$a(HTML(paste("Refreshed on ", textOutput("Refresh1")))))),` – Sagar Oct 27 '17 at 13:47