Why does bookdown::serve_book() gives error after changing style.css and saving it?

  Kiến thức lập trình

I use RStudio IDE. I have tried to follow the instructions at https://bookdown.org/yihui/bookdown/serve-the-book.html and use bookdown::serve_book() to view the HTML of my book on RStudio Viewer. This is the expected behavior:

whenever you modify any files in the book directory, serve_book() can detect the changes, recompile the Rmd files, and refresh the web browser automatically. If the modified files do not include Rmd files, it just refreshes the browser (e.g., if you only updated a certain CSS file). This means once the server is launched, all you have to do next is simply write the book and save the files. Compilation and preview will take place automatically as you save files.

To test this I have:

STEP 1

Created a bookdown project in RStudio (using File, New Project, etc.) naming it bookdown-mve.Rproj. The files created by default have the following content:

style.css:

p.caption {
  color: #777;
  margin-top: 10px;
}
p code {
  white-space: inherit;
}
pre {
  word-break: normal;
  word-wrap: normal;
}
pre code {
  white-space: inherit;
}

_output.yml:


bookdown::gitbook:
  css: style.css
  config:
    toc:
      before: |
        <li><a href="./">A Minimal Book Example</a></li>
      after: |
        <li><a href="https://github.com/rstudio/bookdown" target="blank">Published with bookdown</a></li>
    edit: https://github.com/USERNAME/REPO/edit/BRANCH/%s
    download: ["pdf", "epub"]
    output_dir: "_book"
bookdown::pdf_book:
  includes:
    in_header: preamble.tex
  latex_engine: xelatex
  citation_package: natbib
  keep_tex: yes
bookdown::epub_book: default

index.Rmd (reproducing the YAML header only to keep it short):


--- 
title: "A Minimal Book Example"
author: "John Doe"
date: "`r Sys.Date()`"
site: bookdown::bookdown_site
documentclass: book
bibliography: [book.bib, packages.bib]
# url: your book url like https://bookdown.org/yihui/bookdown
# cover-image: path to the social sharing image like images/cover.jpg
description: |
  This is a minimal example of using the bookdown package to write a book.
  The HTML output format for this example is bookdown::gitbook,
  set in the _output.yml file.
link-citations: yes
github-repo: rstudio/bookdown-demo
---

STEP 2

In the default file style.css I have added the lines to create a shaded box. I took these lines exactly AS IS from https://bookdown.org/yihui/rmarkdown-cookbook/custom-blocks.html#block-shaded. As a result the file style.css reads as follows:

p.caption {
  color: #777;
  margin-top: 10px;
}
p code {
  white-space: inherit;
}
pre {
  word-break: normal;
  word-wrap: normal;
}
pre code {
  white-space: inherit;
}
.blackbox {
  padding: 1em;
  background: black;
  color: white;
  border: 2px solid orange;
  border-radius: 10px;
}
.center {
  text-align: center;
}

STEP 3

In the file index.Rmd I have added, at the bottom, the lines to create a shaded box, these lines are taken exactly AS IS from https://bookdown.org/yihui/rmarkdown-cookbook/custom-blocks.html#block-shaded. As a result the file index.Rmd has the following extra lines at the bottom:

## Added lines to test creating and modifying the style.css file

::: {.blackbox data-latex=""}
::: {.center data-latex=""}
**NOTICE!**
:::

Thank you for noticing this **new notice**! Your noticing it has
been noted, and _will be reported to the authorities_!
:::

STEP 4

In the Console of RStudio I have executed:

bookdown::serve_book(dir = ".", output_dir = "_book", preview = TRUE,
  in_session = TRUE, quiet = FALSE)

which is taken AS IS from https://bookdown.org/yihui/bookdown/serve-the-book.html#serve-the-book.

As a result the HTML output of the book appears in the RStudio Viewer and here is what appears in the Console:

processing file: _main.Rmd
                                                                                        
output file: _main.knit.md

/opt/homebrew/bin/pandoc +RTS -K512m -RTS _main.knit.md --to html4 --from markdown+autolink_bare_uris+tex_math_single_backslash --output _main.html --lua-filter /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/bookdown/rmarkdown/lua/custom-environment.lua --lua-filter /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/rmarkdown/rmarkdown/lua/pagebreak.lua --lua-filter /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/rmarkdown/rmarkdown/lua/latex-div.lua --lua-filter /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/rmarkdown/rmarkdown/lua/anchor-sections.lua --metadata-file /var/folders/cd/5bfnlsw10nb31vy6w2w33qbh0000gn/T//RtmpyZVmSQ/file1450018fe763d --wrap preserve --standalone --section-divs --table-of-contents --toc-depth 3 --template /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/bookdown/templates/gitbook.html --highlight-style pygments --number-sections --css style.css --mathjax --include-in-header /var/folders/cd/5bfnlsw10nb31vy6w2w33qbh0000gn/T//RtmpyZVmSQ/rmarkdown-str14500122a262a.html --citeproc 

Output created: _book/index.html
To stop the server, run servr::daemon_stop(1) or restart your R session
Serving the directory /Users/.../Bookdown-Material/bookdown-mve at http://127.0.0.1:4321

STEP 5

In the Editor of RStudio I ONLY replaced black with blue in style.css:

.blackbox {
  padding: 1em;
  background: blue;
  color: white;
  border: 2px solid orange;
  border-radius: 10px;
}

and clicked on SAVE expecting this behavior:

whenever you modify any files in the book directory, serve_book() can detect the changes, recompile the Rmd files, and refresh the web browser automatically. If the modified files do not include Rmd files, it just refreshes the browser (e.g., if you only updated a certain CSS file). This means once the server is launched, all you have to do next is simply write the book and save the files. Compilation and preview will take place automatically as you save files.

Instead nothing happens, and the box is till BLACK in the HTML viewer.

STEP 6

I thought, perhaps I need to Build Book once and then repeat the process. So I clicked on Build Book All Formats. As a result: the Viewer is refreshed and the box now appears blue.

OK, now I do the change in reverse, i.e., in the Editor of RStudio I ONLY replaced blue back black in style.css:

.blackbox {
  padding: 1em;
  background: black;
  color: white;
  border: 2px solid orange;
  border-radius: 10px;
}

and clicked on SAVE. Here is what happens, the Viewer refreshes the HTML output but the box is still blue.

STEP 7

Now I repeat STEP 4. The Console shows this:

> bookdown::serve_book(dir = ".", output_dir = "_book", preview = TRUE,in_session = TRUE, quiet = FALSE)


processing file: _main.Rmd
                                                                                  
output file: _main.knit.md

/opt/homebrew/bin/pandoc +RTS -K512m -RTS _main.knit.md --to html4 --from markdown+autolink_bare_uris+tex_math_single_backslash --output _main.html --lua-filter /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/bookdown/rmarkdown/lua/custom-environment.lua --lua-filter /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/rmarkdown/rmarkdown/lua/pagebreak.lua --lua-filter /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/rmarkdown/rmarkdown/lua/latex-div.lua --lua-filter /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/rmarkdown/rmarkdown/lua/anchor-sections.lua --metadata-file /var/folders/cd/5bfnlsw10nb31vy6w2w33qbh0000gn/T//RtmpyZVmSQ/file14500d2f98b7 --wrap preserve --standalone --section-divs --table-of-contents --toc-depth 3 --template /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/bookdown/templates/gitbook.html --highlight-style pygments --number-sections --css style.css --mathjax --include-in-header /var/folders/cd/5bfnlsw10nb31vy6w2w33qbh0000gn/T//RtmpyZVmSQ/rmarkdown-str145004d102a23.html --citeproc 

Output created: _book/index.html
To stop the server, run servr::daemon_stop(2) or restart your R session
Serving the directory /Users/.../Bookdown-Material/bookdown-mve at http://127.0.0.1:5404

The Viewer shows a black box, so all is OK.

STEP 8

Now I repeat STEP 5. Same result, nothing appears in the Console and the Viewer is not updated to showing a blue box.

What am I missing? why isn’t the behavior described here:

The server will listen to changes in the book root directory: whenever you modify any files in the book directory, serve_book() can detect the changes, recompile the Rmd files, and refresh the web browser automatically. If the modified files do not include Rmd files, it just refreshes the browser (e.g., if you only updated a certain CSS file). This means once the server is launched, all you have to do next is simply write the book and save the files. Compilation and preview will take place automatically as you save files.

happening?

Thanks so very much.

New contributor

melissa is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT