【问题标题】:How do you change the font family in a formattable in R?如何更改 R 格式表中的字体系列?
【发布时间】:2017-02-22 12:26:58
【问题描述】:

在我所在大学的一个数据科学项目中,我们被要求制作一个决策树来模拟哪些客户可能会响应或不响应广告系列。任务的要求之一是制作混淆矩阵,我将其转换为数据框。这是我生成的数据框。

    confusionMatrixCARTDecisionTree <- data.frame("Number Predicted to Not Subscribe" = 
    c(36006, 542), "Number Predicted to Subscribe" = c(3554, 1086), 
    row.names = c("Number of Individuals that Subscribed", 
    "Number of Individuals that didn't Subscribe"));

然后我使用此代码将其转换为格式表。

    formattableConfusionMatrixCARTDecisionTree <- formattable(
    confusionMatrixCARTDecisionTree, align = "c", 
    list("Predicted Number not Subscribing" = color_tile("white", "yellow"),     
    "Predicted Number Subscribing" = color_tile("yellow", "white")));

然后我在这里找到了一个函数,https://github.com/renkun-ken/formattable/issues/26,将格式表放入 pdf 报告中。但是,我现在要做的是将字体系列更改为混淆矩阵的 CMU Serif,使其与 LaTeX 文件一致。我已经弄清楚如何更改列的字体,如下所示:

    formattableConfusionMatrixCARTDecisionTree <- 
    formattable(confusionMatrixCARTDecisionTree, list("Predicted Number not Subscribing" = 
    formatter("span", style=style("font-family" = "CMU Serif"))));

但是,我已阅读此处https://cran.r-project.org/web/packages/formattable/formattable.pdf 的文档并在线搜索,但找不到更改标题字体或行名称的方法。如果有人对此有解决方案,我将不胜感激!如果有人知道一些可以改变整个表格字体系列的代码,我也会非常高兴。

注意:我确实知道有人在 How to change appearance of table header row with R formattable package 处提出了与更改标题外观有关的问题,但是,这个问题没有得到解答,也没有解决更改字体系列的问题。

【问题讨论】:

    标签: css r formattable


    【解决方案1】:
    @font-face {
       font-family: 'Name';
       src:url('../fonts/Name.eot?#') format('eot'),url('../fonts/Name.woff') format('woff'),url('../fonts/Name.ttf') format('truetype');
    }
    html {
       height:100%
    }
    body{
       font-family:Name,Tahoma,tahoma,Arial;
       background-color:#fafafa;
       height:100%
    }
    

    【讨论】:

    • 亲爱的阿里,非常感谢您的快速回复!我对 R 很陌生,所以我对这种语法非常不熟悉。然而,我很快用谷歌搜索了标签@font-face,这似乎有很多优点!但是,我不确定如何用我当前的代码实现这一点。我必须用 CSS 制作一个单独的文件吗?
    • 不客气,是的,你可以,但如果你想为所有浏览器使用字体,你必须使用上述一种字体的所有格式。
    • 感谢 Ali 和 Al-Mothafar 的帮助,非常感谢!我已经添加了解决方案并回答了问题!
    猜你喜欢
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多