2016-05-23 100 views
2

我想將自定義字體合併到我的Rshiny應用程序中。我有一個預感的代碼將在標籤$風格,但沒有實際的代碼,以包括這一點。如何在RShiny應用程序中使用自定義字體

示例代碼:

ui <- fluidPage(
     tags$style(), 
     column(12, 
       dataTableOutput("testtab") 
      ) # close column 
) #close fluidpage 

server <- function(input, output, session) { 
    output$testtab <- 
     DT::renderDataTable({ 
           tab <- data.frame(a = 1:10, b = 11:20, c = 21:30) 
           dat.tab <- datatable(tab) %>% formatPercentage('a', 0) %>% 
                  formatCurrency(1:ncol(tab), '$') 
           return(dat.tab) 
          }) # close renderDataTable 
} # close server 

shinyApp(ui=ui, server=server) 

例如起見,假設我想在網絡上使用任何自定義字體在那裏。

+0

(小備註 - 以防萬一,如果你不知道,有一個很好的包裝[shinythemes](https://rstudio.github.io/shinythemes /)其中包含7個不錯的主題) –

+0

@UnnamedUser,謝謝。不過,我想使用特定的自定義字體,而這是我想要更改的唯一內容。 –

+1

哪一種字體?就像你已經安裝的那個一樣?你還用什麼系統? Windows/Mac/Linux的字體內容略有不同。 –

回答

3

這應該有所幫助。

首先,您需要從http://www.fontspace.com/gunarta/surabanglus下載字體,然後單擊帶有ttf擴展名的文件並單擊安裝來安裝它。這裏我添加了標籤來控制默認的正文字體,以及使用「id標籤」來控制特定控件中的字體和背景顏色的標籤。

還有其他的方法可以使用單獨的CSS文件等來做到這一點。但這是快速和容易,不太髒。

library(shiny) 
library(dplyr) 
library(DT) 

ui <- fluidPage(
    tags$style(HTML('body {font-family:"Times New Roman",Georgia,Serif; background-color:orange}')), 
    tags$style(HTML('#testtab {font-family:"surabanglus",Georgia,Serif; background-color:lightblue}')), 
    tags$style(HTML('#hello2 {font-family:"Courier",Georgia,Serif; background-color:pink}')), 
    column(12, 
     dataTableOutput("testtab"), 
     actionButton("hello1","Hello There (uses font inherited from body)"), 
     actionButton("hello2","Hello There again (uses Courier)") 

) # close column, 
) #close fluidpage 

server <- function(input, output, session) { 
    output$testtab <- DT::renderDataTable({ 
    tab <- data.frame(a = 1:10, b = 11:20, c = 21:30) 
    dat.tab <- datatable(tab) %>% formatPercentage('a', 0) %>% 
     formatCurrency(1:ncol(tab), '$') 
    return(dat.tab) 
    }) # close renderDataTable 
} # close server 

shinyApp(ui=ui, server=server) 

屈服這樣的:

enter image description here

+0

這看起來不錯,而且如果我只是從上面複製你的代碼,它就可以工作。但是,我無法使用我的實際字體在計算機上工作。標籤$ style(HTML('#body {font-family:「surabanglus」,Georgia,Serif')), tags $ style(HTML('# Tab one {font-family:「surabanglus」,Georgia,Serif}'))' –

+0

最好保留問題和回答簡短而甜蜜,這已經是一個非常成功的問題和答案,所以我建議你只是標記它糾正併發布有關該問題的另一個問題。會符合每個人的利益。 –

相關問題