构建时出现“未找到对象”错误(bookdown),但相同的代码在控制台中运行

问题描述 投票:0回答:1

我正在尝试制作一本书,其中包含以下代码

ggdotchart(themes, x = "theme", y = "citations",          # requires `easyGgplot2`, `ggplot2`, `ggpubr` 
            color = "#440154",                                # Color by groups
            palette = c("#00AFBB", "#E7B800", "#FC4E07"), # Custom color palette
            sorting = "descending",                       # Sort value in descending order
            add = "segments",                             # Add segments from y = 0 to dots
            rotate = TRUE,                                # Rotate vertically
            dot.size = 9,                                 # Large dot size
            label = round(themes$citations),                        # Add citations values as dot labels
            font.label = list(color = "white", size = 10, 
                              vjust = 0.5),               # Adjust label parameters
            ggtheme = theme_pubr(),                        # ggplot2 theme
            ylab="Citation Count",
            xlab="Theme"
 )

当我在控制台中运行代码时,我得到以下图形:

但是当我编织时我得到这个错误:

Quitting from lines 28-42 (bookdown-demo.Rmd) 
Error in ggdotchart(themes, x = "theme", y = "citations", color = "#440154",  : 
  could not find function "ggdotchart"
Calls: <Anonymous> ... withVisible -> eval_with_user_handlers -> eval -> eval
In addition: Warning message:
In xfun::prose_index(x) : Code fences are not balanced
Execution halted

或者当我构建时,出现以下错误:

Quitting from lines 54-68 (bookdown-demo.Rmd) 
Error in ggdotchart(themes, x = "theme", y = "citations", color = "#440154",  : 
  object 'themes' not found
Calls: local ... eval_with_user_handlers -> eval -> eval -> ggdotchart
Execution halted
Error: bookdown::render_book() failed to render the output format 'bookdown::gitbook'.
Execution halted

Exited with status 1.

我试过

themes$citations <- as.character(themes$citations)

load("themes.RData")

我正在使用 Bookdown 模板 repo 的新副本。 我的仓库在这里。

请让我知道我是否应该在此处添加任何其他上下文。

科林

r ggplot2 r-markdown bookdown ggpubr
1个回答
0
投票

感谢@stefan 让我在这方面走上正轨。

这里是关键点,对于那些刚开始使用 rMarkdown 和 bookdown 的人来说,这显然是一个常见的问题。

  • RStudio 在新的 R 会话中呈现 .Rmd 文件,这意味着您在编织或构建时无法访问您在环境中加载的任何对象或函数;因此我看到了错误。
    • 要加载包,请在 index.Rmd 文件中使用
      library(package)
      require(package)
      • 确保加载或需要包裹
        readr
        ,或
        tidyverse
    • 加载数据,使用
      dataframe <- read_csv("path/to/file.csv")
      将您需要的数据加载到一个名为 dataframe 的对象(或任何适合您喜欢的对象)中。这将在您的 .Rmd.
    • 中创建一个可供 R 块访问的 tibble
  • 我在 index.Rmd 上使用以下 R 块完成了所有这些......
knitr::opts_chunk$set(echo = FALSE)
require(ggplot2)
require(easyGgplot2)
require(ggpubr)
require(readr)
pubdate <- read_csv("data/pubdate.csv")
themes <- read_csv("data/themes.csv")

我很可能遗漏了一些细微差别,或者有更简约的解决方案,我很高兴收到你的来信。

© www.soinside.com 2019 - 2024. All rights reserved.