• R语言
  • R blogdown 无法用 serve_site(), new_site() 等命令

问题描述

前几天刚参照 Yihui 的官方文档,用 blogdown + hugo + netfily + Github 成功地建立了个人博客。
但近日出现了在 RStudio 中无法运行 serve_site()new_site() 等命令的情况,错误信息如下:

'C:\Users\GAO' 不是内部或外部命令,也不是可运行的程序
或批处理文件。
Error in shell(cmd, mustWork = TRUE, intern = intern) : 
  '"C:\Users\GAO LEI\AppData\Roaming\Hugo\hugo.exe" -b / -D -F -d "public" --themesDir themes -t hugo-tranquilpeak-theme' execution failed with error code 1

尝试解决

1. 重装 blogdownhugo

尝试了重新安装 blogdownHugo(版本为 0.57.2),但没有效果。

install.packages('blogdown')
blogdown::install_hugo()
blogdown::hugo_version()
[1] ‘0.57.2’

此时,在 CMD 中直接运行 hugo 的server可以更新本地网站:

d:\leiweb\gaolei.xyz> "C:\Users\GAO LEI\AppData\Roaming\Hugo\hugo.exe" server

2. 在项目文件夹下手动安装hugo

  • 尝试重新安装hugo,都没有作用,因为每次都还是默认安装到C:\Users\GAO LEI\AppData\Roaming\Hugo目录下。
  • 索性新建了一个文件夹D:\leiweb\Hugo,并且把hugo.exe复制到该文件夹下,并添加至PATH,则运行blogdown::serve_site()blogdown::build_site()等都正常了。

系统环境

R version 3.5.0 (2018-04-23)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17763)

Matrix products: default

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

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

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.0      bookdown_0.13   digest_0.6.15   withr_2.1.2     magrittr_1.5   
 [6] evaluate_0.14   blogdown_0.15   stringi_1.1.7   rstudioapi_0.7  rmarkdown_1.15 
[11] devtools_1.13.5 tools_3.5.0     stringr_1.3.1   xfun_0.9        compiler_3.5.0 
[16] memoise_1.1.0   htmltools_0.3.6 knitr_1.24     

    leigao 跑一下 blogdown::hugo_build() 看看有没有什么错误信息。

      谢谢 yihui 回复。我运行了 blogdown::hugo_build(),运行结果如下:

      Warning: You are recommended to ignore certain files in config.toml: set the option ignoreFiles = ["\\.Rmd$", "\\.Rmarkdown$", "_files$", "_cache$"]
      'C:\Users\GAO' 不是内部或外部命令,也不是可运行的程序
      或批处理文件。
      Error in shell(cmd, mustWork = TRUE, intern = intern) : 
        '"C:\Users\GAO LEI\AppData\Roaming\Hugo\hugo.exe" -d "public" --themesDir themes -t hugo-tranquilpeak-theme' execution failed with error code 1

      tctcab 谢谢您。但我之前的目录一直是如此,且运行正常。
      此目录是blogdown::install_hugo()的默认安装目录,之后也重新安装过几次Hugo但无济于事。

        leigao 我理解的@tctcab 大侠的意思是,你的用户名“GAO LEI”之间的空格可能是造成问题的原因。你试试把Hugo 的路径手动加到系统的环境变量-路径里面。按windows键,输入view advanced system,在弹出的窗口里,点系统变量(enviromen variables),双击path看看有没有到Hugo的路径,没有就就加进去试试。详细的,可以参考Hugo的主页。

          leigao

          blogdown目录没问题,问题是报错的第一句

          'C:\Users\GAO' 不是内部或外部命令,也不是可运行的程序

          说的是找不到hugo的可执行文件

            tctcab 根据您和Fye 的反馈,我又做了如下尝试。

            1. 重新安装 Hugo(附注 1);
            2. hugo.exe所在目录C:\Users\GAO LEI\AppData\Roaming\Hugo添加到系统环境变量(附注 2)。

            目前的情况是:

            1. 博客本地文件夹运行d:\leiweb\gaolei.xyz>hugo server可正常更新建站。
              我的理解是,这说明系统环境变量添加成功。因为未添加时,运行hugo server会提示'hugo' 不是内部或外部命令,也不是可运行的程序或批处理文件。
            2. RStudio 下运行blogdown::serve_site()blogdown::hugo_build()仍报错。
              > blogdown::serve_site()
              系统找不到指定的路径。
              Error in shell(cmd, mustWork = TRUE, intern = intern) : 
                '"hugo" -b / -D -F -d "public" --themesDir themes -t hugo-tranquilpeak-theme' execution failed with error code 1
              > blogdown::hugo_build()
              'hugo" -d "public' 不是内部或外部命令,也不是可运行的程序
              或批处理文件。
              Error in shell(cmd, mustWork = TRUE, intern = intern) : 
                '"hugo" -d "public" --themesDir themes -t hugo-tranquilpeak-theme' execution failed with error code 1

            附注 1
            按照 Hugo 的安装指导 ,我把解压出来的hugo.exeLICENCEREADME.md复制到了C:\Users\GAO LEI\AppData\Roaming\Hugo目录下,现在该目录下的文件结构为:

            2019/08/26  11:46    <DIR>          .
            2019/08/26  11:46    <DIR>          ..
            2019/08/17  17:54        36,045,824 hugo.exe
            2019/08/17  17:42            11,357 LICENSE
            2019/08/21  21:21    <DIR>          public
            2019/08/17  17:42            11,235 README.md
            2019/08/21  21:21    <DIR>          resources
                           3 个文件     36,068,416 字节
                           4 个目录 10,298,228,736 可用字节

            附注 2
            环境变量:

            C:\Users\GAO LEI>echo %Path%
            ...(无关内容已省略)
            C:\Users\GAO LEI\AppData\Roaming\Hugo;

            hugo的位置:

            C:\Users\GAO LEI>where hugo
            C:\Users\GAO LEI\AppData\Roaming\Hugo\hugo.exe
            • Fye 回复了此帖

              leigao 你试试用 blogdown::update_hugo() 试试看。感觉有个先后顺序的问题。如果手动安装,并添加变量,完了要重启一下电脑。我刚才删了blogdown默认安装的hugo,结果就不运行不了了。但是用update_hugo() 重新装就没有问题。

              看了看谢大的源代码,没看除门道,😄

                Fye 问题解决了。

                • 尝试重新安装hugo,都没有作用,因为每次都还是默认安装到C:\Users\GAO LEI\AppData\Roaming\Hugo目录下。
                • 索性新建了一个文件夹D:\leiweb\Hugo,并且把hugo.exe复制到该文件夹下,并添加至PATH,则运行blogdown::serve_site()blogdown::build_site()等都正常了。

                tctcab 版主大大,问题解决了。已经更新至原帖,请帮忙在题目中注明「已解决」。

                但我没有搞清楚为什么最开始hugo默认安装在C:\Users\GAO LEI\AppData\Roaming\Hugo下就可以正常运行,而之后就不行。