简体   繁体   中英

html of rmarkdown file (.Rmd) is not displayed correctly in browser

When I knit my .Rmd to html, it does not get displayed properly, ie it seems like something is wrong with the format (see screenshot below): The two hyperlinks do not work, the font is not sans-serif, there is no table of contents, etc. I used the same code on a different machine 6 months ago and it rendered as expected. However, today it does not. Does anybody have an idea why the html looks ugly? It seems not to depend on the browser I open the html with (tested it in IE and Chrome).

我的.Rmd文件的html输出

This is the code i use:

---
title: "my title"
author: "subtitle"
date: "my name, `r format(Sys.time(), '%d. %B, %Y')`"
output:
  html_document:
    code_folding: hide
    highlight: haddock
    number_sections: yes
    toc: yes
    toc_float: yes
---

# section
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod 
tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At 
vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd 
gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum 
dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor 
invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero 
eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no 
sea takimata sanctus est Lorem ipsum dolor sit amet.

```{r, warning = FALSE}
head(mtcars)
```

And my session info:

> sessionInfo()
R version 3.4.2 (2017-09-28)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)

Matrix products: default

locale:
[1] LC_COLLATE=German_Switzerland.1252  LC_CTYPE=German_Switzerland.1252    
LC_MONETARY=German_Switzerland.1252
[4] LC_NUMERIC=C                        LC_TIME=German_Switzerland.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] compiler_3.4.2  backports_1.1.1 magrittr_1.5    rprojroot_1.2   
htmltools_0.3.6 tools_3.4.2     yaml_2.1.15    
 [8] Rcpp_0.12.14    stringi_1.1.6   rmarkdown_1.8   knitr_1.17      
stringr_1.2.0   digest_0.6.12   evaluate_0.10.1

update : Opened the html in Chrome, pressed F12, and under console it says

Chrome中的控制台标签

A friend of mine got the solution for this problem: The .libPaths() in our company was defined as

.libPaths()
#> [1] "\\\\userhome/my_user_name/R/win-library/3.4"   # note the four \\\\
#> [2] "C:/Program Files/R/3.4.2/library"

After changing the first location to

.libPaths()
#> [1] "C:/Users/my_user_name/R/win-library/3.4.2"     # path starts with C:/
#> [2] "C:/Program Files/R/3.4.2/library"

the .Rmd renders as expected :).

We placed the following function in our .Rprofile . It creates the directory step-by-step and adds it to .libPaths() .

# install packages locally within user profile
(function() {
  components <- list('C:/', 'users', Sys.info()['login'], 'R',
                     'win-library', paste0(R.Version()$major,
                                           '.',
                                           R.Version()$minor))

  # loop over components and create dir step-by-step
  # (recursive = TRUE leads to errors)
  for (k in 4:length(components)) {
    p <- do.call(file.path, components[1:k])
    dir.create(p, showWarnings = FALSE, recursive = FALSE)
  }

  path <- do.call(file.path, components)
  .libPaths(c(path, .Library))
})()

Created on 2018-09-24 by the reprex package (v0.2.1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM