• R语言已解决
  • 解决“多字节字符串有错”导致中文书籍(Html)上传book.org失败

问题描述

我用李东风老师制作的中文图书模板写了一本简单的中文书,直接点击Rstudio右上角的publish按钮,想以html的形式上传到自己的book.org,但是遇到如下问题。

运行结果

问题1:

根据报错我自己判断好像是因为缺少textshaping包,导致最后说“多字节字符串有错”。但是已经下载到了library文件夹,同时找不到fribidi包。为简洁说明,我直接贴出error message和warning message。

[Connect] 2023/04/12 02:45:54.492774266 [1] "Command failed (1)\n\nFailed to run system command:\n\n\t'/opt/R/3.6.2/lib/R/bin/R' --vanilla CMD INSTALL --preclean '/opt/rstudio-connect/mnt/tmp/RtmpA2twbs/packrat-install-5a5774df2baf/textshaping' --library='/opt/rstudio-connect/mnt/app/packrat/lib/x86_64-pc-linux-gnu/3.6.2' --install-tests --no-docs --no-multiarch --no-demo \n\nThe command failed with output:\n* installing source package ‘textshaping’ ...\n** package ‘textshaping’ successfully unpacked and MD5 sums checked\n** using staged installation\nPackage fribidi was not found in the pkg-config search path.\nPerhaps you should add the directory containing fribidi.pc'\nto the PKG_CONFIG_PATH environment variable\nNo package 'fribidi' found\nUsing PKG_CFLAGS=\nUsing PKG_LIBS=-lfreetype -lharfbuzz -lfribidi -lpng\n--------------------------- [ANTICONF] --------------------------------\nConfiguration failed to find the harfbuzz freetype2 fribidi library. Try installing:\n * deb: libharfbuzz-dev libfribidi-de
[Connect] v (Debian, Ubuntu, etc)\n * rpm: harfbuzz-devel fribidi-devel (Fedora, EPEL)\n * csw: libharfbuzz_dev libfribidi_dev (Solaris)\n * brew: harfbuzz fribidi (OSX)\nIf harfbuzz freetype2 fribidi is already installed, check that 'pkg-config' is in your\nPATH and PKG_CONFIG_PATH contains a harfbuzz freetype2 fribidi.pc file. If pkg-config\nis unavailable you can set INCLUDE_DIR and LIB_DIR manually via:\nR CMD INSTALL --configure-vars='INCLUDE_DIR=... LIB_DIR=...'\n-------------------------- [ERROR MESSAGE] ---------------------------\n<stdin>:1:10: fatal error: hb-ft.h: No such file or directory\ncompilation terminated.\n--------------------------------------------------------------------\nERROR: configuration failed for package ‘textshaping’\n* removing ‘/opt/rstudio-connect/mnt/app/packrat/lib/x86_64-pc-linux-gnu/3.6.2/textshaping’"
[Connect] 2023/04/12 02:45:54.494743145 Error: Command failed (1)
[Connect] 2023/04/12 02:45:54.494752457
[Connect] 2023/04/12 02:45:54.494783089 Failed to run system command:
[Connect] 2023/04/12 02:45:54.494784293
[Connect] 2023/04/12 02:45:54.494804077 '/opt/R/3.6.2/lib/R/bin/R' --vanilla CMD INSTALL --preclean '/opt/rstudio-connect/mnt/tmp/RtmpA2twbs/packrat-install-5a5774df2baf/textshaping' --library='/opt/rstudio-connect/mnt/app/packrat/lib/x86_64-pc-linux-gnu/3.6.2' --install-tests --no-docs --no-multiarch --no-demo
[Connect] 2023/04/12 02:45:54.494805312
[Connect] 2023/04/12 02:45:54.494818812 The command failed with output:
[Connect] 2023/04/12 02:45:54.494819766 * installing *source* package ‘textshaping’ ...
[Connect] 2023/04/12 02:45:54.494827266 ** package ‘textshaping’ successfully unpacked and MD5 sums checked
[Connect] 2023/04/12 02:45:54.494828394 ** using staged installation
[Connect] 2023/04/12 02:45:54.494835655 Package fribidi was not found in the pkg-config search path.
[Connect] 2023/04/12 02:45:54.494836974 Perhaps you should add the directory containing
fribidi.pc'
[Connect] 2023/04/12 02:45:54.494843995 to the PKG_CONFIG_PATH environment variable
[Connect] 2023/04/12 02:45:54.494845139 No package 'fribidi' found
[Connect] 2023/04/12 02:45:54.494852606 Using PKG_CFLAGS=
[Connect] 2023/04/12 02:45:54.494853486 Using PKG_LIBS=-lfreetype -lharfbuzz -lfribidi -lpng
[Connect] 2023/04/12 02:45:54.494860396 --------------------------- [ANTICONF] --------------------------------
[Connect] 2023/04/12 02:45:54.494861120 Configuration failed to find the harfbuzz freetype2 fribidi library. Try installing:
[Connect] 2023/04/12 02:45:54.494868246 * deb: libharfbuzz-dev libfribidi-dev (Debian, Ubuntu, etc)
[Connect] 2023/04/12 02:45:54.494868927 * rpm: harfbuzz-devel fribidi-devel (Fedora, EPEL)
[Connect] 2023/04/12 02:45:54.494875945 * csw: libharfbuzz_dev libfribidi_dev (Solaris)
[Connect] 2023/04/12 02:45:54.494876582 * brew: harfbuzz fribidi (OSX)
[Connect] 2023/04/12 02:45:54.494883071 If harfbuzz freetype2 fribidi is already installed, check that 'pkg-config' is in your
[Connect] 2023/04/12 02:45:54.494883704 PATH and PKG_CONFIG_PATH contains a harfbuzz freetype2 fribidi.pc file. If pkg-config
[Connect] 2023/04/12 02:45:54.494890275 is unavailable you can set INCLUDE_DIR and LIB_DIR manually via:
[Connect] 2023/04/12 02:45:54.494891249 R CMD INSTALL --configure-vars='INCLUDE_DIR=... LIB_DIR=...'
[Connect] 2023/04/12 02:45:54.494897935 -------------------------- [ERROR MESSAGE] ---------------------------
[Connect] 2023/04/12 02:45:54.494898551 <stdin>:1:10: fatal error: hb-ft.h: No such file or directory
[Connect] 2023/04/12 02:45:54.494905176 compilation terminated.
[Connect] 2023/04/12 02:45:54.494905826 --------------------------------------------------------------------
[Connect] 2023/04/12 02:45:54.494912210 ERROR: configuration failed for package ‘textshaping’
[Connect] 2023/04/12 02:45:54.494912920 * removing ‘/opt/rstudio-connect/mnt/app/packrat/lib/x86_64-pc-linux-gnu/3.6.2/textshaping’
[Connect] 2023/04/12 02:45:54.494919516
[Connect] 2023/04/12 02:45:54.494920110 Unable to fully restore the R packages associated with this deployment.
[Connect] 2023/04/12 02:45:54.494926346 Please review the preceding messages to determine which package
[Connect] 2023/04/12 02:45:54.494927000 encountered installation difficulty and the cause of the failure.
[Connect] 2023/04/12 02:45:54.504290549 Warning message:
[Connect] 2023/04/12 02:45:54.504500788 In packrat::restore(overwrite.dirty = TRUE, prompt = FALSE, restart = FALSE) :
[Connect] 2023/04/12 02:45:54.504505763 The most recent snapshot was generated using R version 4.2.3
[Connect] An error occurred while building your content.
[Connect] This link offers advice for this kind of error:
[Connect] https://docs.posit.co/connect/user/troubleshooting/#r-missing-system-library
[Connect] Build error: An error occurred while building your content. (Error code: r-missing-system-library)
Site deployment failed with error: An error occurred while building your content. (Error code: r-missing-system-library)
Warning message:
Error detecting locale: Error in make.names(col.names, unique = TRUE): '<d6><f7><bb><fa>'多字节字符串有错
(Using default: en_US)

我想过可能是输入数据时导入格式不对,就将导入数据的格式改成如下,在原来的基础上添加encoding = "UTF-8":

library(haven)
netizen <- read_stata("eman.dta", encoding = "UTF-8")

但上传的时候还是显示一样的报错信息。

问题2:

在Markers界面,显示如下信息。

0101-usage.Rmd
Line 48 Paths should be to files within the project directory

我回到Rmd文件“0101-usage.Rmd”的第48行,是数据导入部分,如下所示。

library(haven)
netizen <- read_stata("eman.dta")
dput("netizen")

我的数据文件eman.dta是放在和“CBook.Rproj”同一个文件夹中的,不清楚错误在哪。

我的系统环境

sessionInfo()
R version 4.2.3 (2023-03-15 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 22621)

Matrix products: default

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

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

loaded via a namespace (and not attached):
[1] rstudioapi_0.14 knitr_1.42 magrittr_2.0.3 tidyselect_1.2.0 R6_2.5.1
[6] rlang_1.1.0 fastmap_1.1.1 fansi_1.0.4 skimr_2.1.5 dplyr_1.1.1
[11] tools_4.2.3 xfun_0.38 utf8_1.2.3 cli_3.6.1 jquerylib_0.1.4
[16] htmltools_0.5.5 askpass_1.1 openssl_2.0.6 yaml_2.3.7 digest_0.6.31
[21] tibble_3.2.1 lifecycle_1.0.3 bookdown_0.33 repr_1.1.6 base64enc_0.1-3
[26] vctrs_0.6.1 rsconnect_0.8.29 curl_5.0.0 glue_1.6.2 evaluate_0.20
[31] rmarkdown_2.21 compiler_4.2.3 pillar_1.9.0 generics_0.1.3 jsonlite_1.8.4
[36] pkgconfig_2.0.3

    residual-sun 我没试过将书籍源码交给 RStudio Connect 去编译的情况,因为这需要清楚了解 RStudio Connect 系统上的 R 包和软件依赖。你可以本地编译好,然后发布到上面去,帮助文档在这

      Cloud2016 感谢帮助!我按照帮助文档中的如下代码进行运行
      publish_book(render = "local", account = "residualsun1")
      虽然控制台仍然显示“多字节字符串有错”,但编译的图书内容已成功发布到bookdown.org的content上!
      只是不理解命令中sever参数应该怎么设置,这是用来设置自己的URL地址吗?我想设置一个私人的URL存储自己的图书内容,尝试设置为如下
      publish_book(render = "local", account = "residualsun1", server = "https://bookdown.org/residualsun1/Amospractice")
      运行之后显示如下结果:

      Error: You must register an account using setAccountInfo prior to proceeding.

      报错说我需要有一个账户,但我已经有一个residualsun1的账号,前面上传成功的内容就在https://bookdown.org/content/98abdc9d-3a06-44aa-829f-12840a32dfbd/。我注意到这里上传的是在content(应该是公用的),如果要设置一个个人的地址,好像和posit的API相关,但没在益辉大哥写的文档中找到具体的说明。

        residual-sun

        下面是我的账户设置,你可以照着替换一下

        rsconnect::addConnectServer("https://bookdown.org", "bookdown.org")
        rsconnect::connectApiUser(
          account = "xiangyun", server = "bookdown.org",
          apiKey = Sys.getenv("CONNECT_API_KEY")
        )

        account 和 apiKey 换成你的,server 就是 bookdown.org

          Cloud2016 您好!我按照您的代码替换了一下,如下:

          rsconnect::addConnectServer("https://bookdown.org", "bookdown.org")
          rsconnect::connectApiUser(
               account = "residualsun1", server = "bookdown.org",
               apiKey = Sys.getenv("fFkbuRH3j2eQ9bxBssKhgLHXaSrEY6XK")
           )

          运行之后出现以下信息。

          Server 'bookdown.org' added successfully: https://bookdown.org/__api__
          The account 'residualsun1' is already registered; attempting to reconnect it.
          Error: HTTP 401
          GET https://bookdown.org/__api__/users/current
          We couldn't log you in with the provided credentials. Please ask your administrator for assistance.

          我想account应该是用在posit connect上用邮箱创的账号,只有一个。然后报错说无法根据我提供的凭证登录,不知道是不是没有找到我设置的API(我只设置了一个API)。

            residual-sun
            你这里只需

            apiKey = "fFkbuRH3j2eQ9bxBssKhgLHXaSrEY6XK"

            我上面是设置了一个环境变量 CONNECT_API_KEY 指向密钥。

              Cloud2016 您好!根据您的建议,我将代码修改如下:

              rsconnect::addConnectServer("https://bookdown.org", "bookdown.org")
              rsconnect::connectApiUser(
                   account = "residualsun1", server = "bookdown.org",
                   apiKey = "fFkbuRH3j2eQ9bxBssKhgLHXaSrEY6XK"
               )

              两行代码都运行成功,反馈信息如下:

              Server 'bookdown.org' added successfully: https://bookdown.org/__api__
              The account 'residualsun1' is already registered; attempting to reconnect it.
              Account registered successfully: residualsun1

              不过发布书籍的时候,我应该还是在sever的设置上没能理解,出错了,代码如下:
              publish_book(render = "local", account = "residualsun1", server = "https://bookdown.org/residualsun1/Amospractice")

              运行结果如下:

              Output created: docs/index.html
              Error: You must register an account using setAccountInfo prior to proceeding.

              我的目的是发布到一个属于自己的URL,例如这次期望形成的URL是“https://bookdown.org/residualsun1/knowledge/Amospractice”。
              其中,residualsun1是我的账户名,knowledge是账户的下一级,Amospractice是书籍的名字,这样我以后发布其他梳理知识的书籍也可以放到knowledge的下一级,便于管理。比如我这次想上传的是Amos软件的练习笔记,我命名为Amospractice,下一次我想上传Stata软件的练习笔记,就会命名为Statapractice,期望创建的URL则为“https://bookdown.org/residualsun1/knowledge/Statapractice”。
              请问这是在server的具体参数设置中实现吗?

                residual-sun
                publish_book 函数设置如下

                publish_book(
                  name = "书名",
                  account ="residualsun1",
                  server = "bookdown.org",
                  render = "none"
                )

                推荐看一下该函数的帮助文档。

                  residual-sun 其中,residualsun1是我的账户名,knowledge是账户的下一级,Amospractice是书籍的名字,这样我以后发布其他梳理知识的书籍也可以放到knowledge的下一级,便于管理。

                  不可以。除非你自己搭建服务器。

                    Cloud2016 按照您提供的参考代码,我已成功设置自己期望的URL地址(这个问题困扰了我差不多一个星期),十分感谢您的耐心解答!(我会去补一补publish_book的函数帮助文档内容的!)

                    Cloud2016 好的!这个我会自行去网络查找相关的内容,真的很感谢您的慷慨解答!(哈哈,有点柳暗花明又一村的感觉)

                    7 个月 后
                    residual-sun 更改标题为「解决“多字节字符串有错”导致中文书籍(Html)上传book.org失败