如何在Shiny R Dashboard的Navbar中读取和显示时间

时间:2018-09-29 07:50:00

标签: r shiny shiny-server shinydashboard

我想在闪亮的R的导航栏中显示“上次更新时间”。为此,我将上次更新时间存储在csv文件中,该文件将用于在server.R中读取,但我无法弄清楚如何在导航栏的最右侧显示该时间。任何帮助将不胜感激。谢谢

    shinyUI(
       navbarPage(
              title = 'Welcome',
               tabPanel('Overview',
           tabsetPanel(
           tabPanel('Forward',
                    fluidRow(
                      DT::dataTableOutput("view_fwd"),width = 6
                    )
           ),
           tabPanel('Reverse',
                    fluidRow(
                      DT::dataTableOutput("view_rvo"),width = 6
                    ))

           ))

2 个答案:

答案 0 :(得分:2)

library(shiny)


ui <- fluidPage(

  navbarPage(
    title = 'Welcome',
    tabPanel('Overview',
             tabsetPanel(
               tabPanel('Forward',
                        fluidRow(
                          DT::dataTableOutput("view_fwd"),width = 6
                        )
               ),
               tabPanel('Reverse',
                        fluidRow(
                          DT::dataTableOutput("view_rvo"),width = 6
                        ))

             )),
              tabPanel(tags$ul(class='nav navbar-nav', 
                               style = "padding-left: 550px;", htmlOutput("time"))) # here you output time, need to positions on the left side by 550px
    )

)

# Define server logic
server <- function(input, output) {

  output$time <- renderUI({

    as.character(strptime(Sys.time(), "%Y-%m-%d %H:%M:%S", tz = "EET"))


  })

}

# Run the application 
shinyApp(ui = ui, server = server)

答案 1 :(得分:1)

使用一些CSS和Javascript,您可以让时间向右浮动,并禁用该选项卡上的单击事件。您将需要软件包shinyjs

library(shiny)
library(shinyjs)

jscode <- '
shinyjs.init = function() {
$(".nav").on("click", ".disabled", function (e) {
e.preventDefault();
return false;
});
}
'

ui <- fluidPage(
  tags$head(tags$style(HTML("
.navbar-nav {
  float: none;
}
.navbar ul > li:nth-child(2) {
  float: right;
}
.navbar ul > li:nth-child(2) {
  color: black !important;
}
                       "))),
  useShinyjs(),
  extendShinyjs(text = jscode, functions = "init"),
  navbarPage(
    title = 'Welcome',

    tabPanel('Overview',
             tabsetPanel(
               tabPanel('Forward',
                        fluidRow(
                          DT::dataTableOutput("view_fwd"),width = 6
                        )
               ),
               tabPanel('Reverse',
                        fluidRow(
                          DT::dataTableOutput("view_rvo"),width = 6
                        ))

             )),
    tabPanel(tags$ul(class='nav navbar-nav', 
                     style = "padding-left: 5px; float: right;", htmlOutput("time")))
  )

)

# Define server logic
server <- function(input, output) {
  observe({
    toggleClass(condition = input$foo,
                class = "disabled",
                selector = ".navbar ul > li:nth-child(2)")
  })

  output$time <- renderUI({
    as.character(strptime(Sys.time(), "%Y-%m-%d %H:%M:%S", tz = "EET"))
  })

}

# Run the application 
shinyApp(ui = ui, server = server)