2016-10-22 62 views
1
library(shiny) 
    library(shinydashboard) 
    filetime <- format(file.mtime("mydata.csv"), format = "%a %e-%b-%Y %r IST") 

    ui <- dashboardPage(
     dashboardHeader(title = "Recruitment"), 
     dashboardSidebar(), 
     dashboardBody(
     shinyUI(fluidPage(
     box(verbatimTextOutput("final_text"), status = "primary", solidHeader = TRUE, collapsible = TRUE, width = 12, title = "Collapsable text") 
    )))) 

    server <- shinyServer(function(input, output, session) { 
     output$final_text <- renderText({ 
     HTML(paste("<center>","Last updated at", filetime, "</center>")) #"<font size=\"2\">", 
     }) 
    } 

在上面的代碼中Last updated at and filetime沒有得到居中對齊,在進一步的研究,我發現,如果這是造成問題的原因center標籤不HTML5的工作,不知道。中心對齊閃亮框標題用HTML或CSS

作爲一種解決方法,我添加了一個div and class來通過css居中對齊文本,這是我的第二次嘗試。

#Next to fluidPage 
tags$style(HTML(".man_made_class{color:#f2f205; text-align: center;}")), 
#Then further in Output 
    output$final_text <- renderText({ 
    HTML(paste("<div class= man_made_class>","Last updated at", filetime, "</div>")) #"<font size=\"2\">", 
    }) 

在我的兩個attepmt,我能夠改變colorfont sizemargin等,但不能居中對齊文本。任何幫助?

回答

1

你並不需要添加自定義類,作爲textOutput已經有一個唯一的ID final_text。工作示例:

library(shiny) 
library(shinydashboard) 
filetime <- format(file.mtime("mydata.csv"), format = "%a %e-%b-%Y %r IST") 

ui <- dashboardPage(
    dashboardHeader(title = "Recruitment"), 
    dashboardSidebar(), 
    dashboardBody(
    shinyUI(fluidPage(
     tags$head(tags$style(HTML(" 
           #final_text { 
            text-align: center; 
           } 
           div.box-header { 
            text-align: center; 
           } 
           "))), 
     box(verbatimTextOutput("final_text"), status = "primary", solidHeader = TRUE, collapsible = TRUE, width = 12, title = "Collapsable text") 
    )))) 

server <- shinyServer(function(input, output, session) { 
    output$final_text <- renderText({ 
    HTML(paste("Last updated at", filetime)) 
    }) 
}) 
shinyApp(ui = ui, server = server) 
+0

的final_text得到居中對齊,我需要的盒子被中心對準頭部。謝謝 – Vasim

+0

已更新的答案。 –

+0

是的,框標題是居中對齊,但圖表上的所有框都獲得中心對齊。我只希望#final_text框居中對齊 – Vasim

0

如何更改爲ui.R和server.R幫助?

ui.R

library(shiny) 
    library(shinydashboard) 
    #filetime <- format(file.mtime("mydata.csv"), format = "%a %e-%b-%Y %r IST") 

    ui <- dashboardPage(
      dashboardHeader(title = "Recruitment"), 
      dashboardSidebar(), 
      dashboardBody(
        shinyUI(fluidPage(
          tags$style(HTML(".man_made_class{color:#f2f205; text-align: center;}")), 
          box(htmlOutput("final_text"), status = "primary", solidHeader = TRUE, collapsible = TRUE, width = 12, title = "Collapsable text") 
        )))) 

server.R

server <- shinyServer(function(input, output, session) { 
      output$final_text <- renderText({ 
        "<div class= man_made_class>Last updated at xxxx</div>" 
      }) 
    }) 
+0

這不但是創建類,框標題是尚未居中對齊 – Vasim