rstudio / bookdown

Authoring Books and Technical Documents with R Markdown

Home Page:https://pkgs.rstudio.com/bookdown/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

bs4_book error: Error in 1:max_n

wangzhen89 opened this issue · comments

Hi guys, when I compiled a bs4_book, I got an error, here is the traceback:

Error in 1:max_n : 答案矢量会太长
15. paste(words[1:max_n], collapse = " ")
14. tweak_metadata(html, path)
13. bs4_chapter_tweak(path, toc, rmd_index = rmd_index, repo = repo,
footnotes_inline = footnotes_inline)
12. bs4_chapters_tweak(output, repo = repo, rmd_index = unlist(as.list(rmd_index)),
output_dir = output_dir, footnotes_inline = footnotes_inline)
11. bs4_book_build(output, repo = repo, lib_dir = lib_dir, split_bib = split_bib,
footnotes_inline = footnotes_inline)
10. output_format$post_processor(front_matter, input, output_file,
clean, !quiet)
9. rmarkdown::render(main, output_format, ..., clean = clean, envir = envir,
run_pandoc = TRUE, knit_meta = knit_meta)
8..render_new_session(files, main, config, output_format, clean,
envir, ...)
7. render_book(index, output_format = output_format, envir = envir)
6. render_book_script(output_format, envir, quiet)
5. in_dir(input, render_book_script(output_format, envir, quiet))
4. siteGenerator$render(input_file = NULL, output_format = NULL,
envir = new.env(), quiet = quiet, encoding = getOption("encoding"))
3. site$render()
2. rsconnect::deploySite(siteDir = getwd(), siteName = name, account = account,
server = server, render = render, logLevel = "normal")
1. publish_book(name = "survival", render = "local")

This error occurs when I compile a new chapter, but it didn't happen before I try to add this new chapter. But I've checked the texts and codes of this chapter, I found no typos or omissions.

As I check the original code of bs4_book.R, I found three places about max_n:

max_n <- max(which(no_char<= 197))
description_string <- paste(words[1: max_n], collapse = " ")
if (max_n != length(words)) {
description_string <- paste0(description_string, "...")
}

But I still don't know how to fix the error. Any solution? Thanks!

Here is my sessionInfo:

R version 4.3.2 (2023-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 11 x64 (build 22631)

Matrix products: default


locale:
[1] LC_COLLATE=Chinese (Simplified)_China.utf8 
[2] LC_CTYPE=Chinese (Simplified)_China.utf8   
[3] LC_MONETARY=Chinese (Simplified)_China.utf8
[4] LC_NUMERIC=C                               
[5] LC_TIME=Chinese (Simplified)_China.utf8    

time zone: Asia/Shanghai
tzcode source: internal

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

other attached packages:
[1] bookdown_0.38

loaded via a namespace (and not attached):
 [1] vctrs_0.6.5       cli_3.6.2         knitr_1.45        rlang_1.1.3      
 [5] xfun_0.43         jsonlite_1.8.8    glue_1.7.0        htmltools_0.5.8  
 [9] sass_0.4.9        rsconnect_1.2.1   fansi_1.0.6       rmarkdown_2.26   
[13] evaluate_0.23     jquerylib_0.1.4   tibble_3.2.1      fastmap_1.1.1    
[17] yaml_2.3.8        lifecycle_1.0.4   memoise_2.0.1     compiler_4.3.2   
[21] fs_1.6.3          pkgconfig_2.0.3   downlit_0.4.3     rstudioapi_0.16.0
[25] digest_0.6.34     R6_2.5.1          utf8_1.2.4        pillar_1.9.0     
[29] magrittr_2.0.3    bslib_0.7.0       tools_4.3.2       withr_3.0.0      
[33] xml2_1.3.6        cachem_1.0.8     

Thanks for the report.

Can you provide a book project that reproduce this ? Can you share a github repo with your own project I can run otherwise ?

As I check the original code of bs4_book.R, I found three places about max_n:

If we don't have access to a reproducible example, it would be useful if you could debug. It would be interesting to know the value of max_n: max_n <- max(which(no_char<= 197))

bookdown/R/bs4_book.R

Lines 599 to 603 in 50a1c1e

max_n <- max(which(no_char<= 197))
description_string <- paste(words[1: max_n], collapse = " ")
if (max_n != length(words)) {
description_string <- paste0(description_string, "...")
}

You could run debug(bookdown:::tweak_metadata) and then bookdown::render_book() on your project, it should stop in debugging mode inside the function, then you could step line by line until the right place. If you use RStudio IDE, it will help you with debugging.

To help with debugging, here are some resources

thanks

@cderv
Here is my repo for reproducing my project: https://github.com/wangzhen89/for_debug

All the files are created using RStudio IDE, only "01-intro.Rmd" was changed to the possible problematic text.

I'm not familiar with debug, thanks for your help!

Thank you.

So the problem happens because of the specific content of your files containing special characters

# 竞争风险 {#chap12}

在以死亡为结局的研究中,个体可能死于多种不同原因之一。例如,在比较两种或多种前列腺癌疗法的研究中,患者可能死于中风、心肌梗塞或癌症本身。在某些情况下,对所有原因造成的死亡进行分析可能是适当的,并且可以使用生存分析的标准方法。更常见的是,人们会对不同原因导致的死亡风险如何取决于治疗效应和其他解释变量感兴趣。当然,由其中任何一种原因导致的死亡都排除了由任何其他原因导致的死亡,这一特征对于此类数据的分析具有重要意义。在本章中,我们回顾了总结不同死因生存时间数据的方法,并描述了**原因别** (cause-specific) 生存数据的模型。

This part of the code

bookdown/R/bs4_book.R

Lines 596 to 607 in 50a1c1e

if (nzchar(text)) {
words <- unlist(strsplit(text, " "))
no_char <- cumsum(unlist(lapply(words, function(x) {nchar(x) + 1})))
max_n <- max(which(no_char<= 197))
description_string <- paste(words[1: max_n], collapse = " ")
if (max_n != length(words)) {
description_string <- paste0(description_string, "...")
}
set_content(og_description, description_string)
set_content(twitter_description, description_string)
set_content(general_description, description_string)
}

is supposed to automatically derive a description from the content by

  • splitting words from space (in your case it creates only 3 words)
  • taking only the words that fits under 197 characters max (but in your case the first "word" is seen as 241 characters)

So max_n <- max(which(no_char<= 197)) gives -Inf and 1:max_n errors indeed.

@yihui how would you go about this to get a automatic description strings for the metadata ?

At least, I am thinking to avoid error by providing empty description (with a warning ?) if this fail.

Basically this algorithm didn't take CJK languages into consideration (they don't use spaces as word separators). For the case of is.infinite(max_n), I think we can just let max_n = 1. @atusy Could you help with a quick fix? This should be one line of code. Thanks!

Here you go! #1463

Thanks for your work!