ImageMagick PATH 无法被knitr 中的engine = "tikz" 识别

2023-12-19

我正在尝试编译 TikZ 图形knitr。我正在使用可用的示例here https://github.com/yihui/knitr-examples/blob/master/058-engine-tikz.Rmd。我专门尝试从 Rstudio 编织。

我从“R Markdown”选项卡获得的输出如下:

processing file: test.Rmd

Invalid Parameter - /test_files
Quitting from lines 16-31 (test.Rmd) 
Error in (knit_engines$get(options$engine))(options) : 
  problems with `convert`; probably not installed?
Calls: <Anonymous> ... process_group.block -> call_block -> block_exec -> in_dir -> <Anonymous>
Execution halted

据易辉表示here https://github.com/yihui/knitr-examples/issues/38,我需要安装 ImageMagick 才能通过运行 TikZ 代码knitr并且需要在我的 PATH 变量中列出 ImageMagick(我运行的是 Windows 7)。我已经安装了 ImageMagick,并且我相信我已经在 PATH 变量中正确指定了目录。我得到以下输出Sys.getenv("PATH")

[1] "C:\\Program Files\\R\\R-3.0.2\\bin\\x64;C:\\Program Files\\ImageMagick-6.8.9-Q16;C:\\Program Files\\Microsoft HPC Pack 2008 R2\\Bin\\;C:\\Program Files (x86)\\AMD APP\\bin\\x86_64;C:\\Program Files (x86)\\AMD APP\\bin\\x86;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;c:\\Program Files (x86)\\Microsoft SQL Server\\100\\Tools\\Binn\\;c:\\Program Files\\Microsoft SQL Server\\100\\Tools\\Binn\\;c:\\Program Files\\Microsoft SQL Server\\100\\DTS\\Binn\\;c:\\Program Files (x86)\\Microsoft SQL Server\\100\\Tools\\Binn\\VSShell\\Common7\\IDE\\;c:\\Program Files (x86)\\Microsoft SQL Server\\100\\DTS\\Binn\\;C:\\Program Files\\MiKTeX 2.9\\miktex\\bin\\x64\\;C:\\Program Files (x86)\\Microsoft Visual Studio 9.0\\Common7\\IDE\\PrivateAssemblies\\;c:\\Program Files (x86)\\ATI Technologies\\ATI.ACE\\Core-Static;C:\\PDF\\xpdfbin-win-3.03\\bin64;C:\\Program Files\\Microsoft SQL Server\\110\\DTS\\Binn\\;C:\\Program Files (x86)\\Microsoft SQL Server\\110\\Tools\\Binn\\;C:\\Program Files\\Microsoft SQL Server\\110\\Tools\\Binn\\;C:\\Program Files (x86)\\Microsoft SQL Server\\110\\Tools\\Binn\\ManagementStudio\\;C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\Common7\\IDE\\PrivateAssemblies\\;C:\\Program Files (x86)\\Microsoft SQL Server\\110\\DTS\\Binn\\;C:\\Program Files (x86)\\Windows Kits\\8.1\\Windows Performance Toolkit\\;C:\\Program Files\\SlikSvn\\bin;C:\\Program Files (x86)\\QuickTime\\QTSystem\\;C:\\Program Files\\nodejs\\;C:\\Program Files (x86)\\Google\\Google Apps Sync\\;C:\\Program Files (x86)\\Google\\Google Apps Migration\\;C:\\Program Files (x86)\\Microsoft Visual Studio 11.0\\Common7\\IDE\\PrivateAssemblies\\;C:\\Program Files\\ImageMagick-6.8.9-Q16;C:\\Users\\mienkoja\\AppData\\Local\\Pandoc\\;C:\\Program Files (x86)\\Google\\google_appengine\\;C:\\Users\\mienkoja\\AppData\\Roaming\\MiKTeX\\2.9\\miktex\\bin\\x64\\;C:\\Users\\mienkoja\\AppData\\Roaming\\npm" 

如你看到的,C:\\Program Files\\ImageMagick-6.8.9-Q16;在我的目录列表中是第二个,顺序似乎并不重要 - 我尝试将其移动到列表中的不同位置并得到相同的结果。

我还尝试以管理员身份运行 RStudio(我在用户和系统 PATH 变量中都指定了 ImageMagick),但仍然收到相同的错误消息。

TikZ 本身似乎也运行良好,因为我可以从 Rnw 文件或命令行将相同的代码编译为 PDF。

有没有人有任何其他想法为什么knitr鉴于我的上述设置,是否要退出?

UPDATE

这似乎是与 ImageMagick 相关的 Windows 问题。当我尝试运行 r-blogger 条目中的示例 4 时,我意识到了这一点here http://www.r-bloggers.com/animate-gif-images-in-r-imagemagick/.

当我跑步时system("convert -delay 40 *.png example_4.gif")从该示例的末尾,我收到以下警告消息:

Invalid Parameter - 40
Warning message:
running command 'convert -delay 40 *.png example_4.gif' had status 4 

这似乎是一个问题,因为 Windows 在 System32 目录(也在 PATH 变量中)中有一个名为convert.exe。如更详细描述的here http://savage.net.au/ImageMagick/html/install-convert.html,windows 文件是 Windows 系统实用程序,用于将 FAT 卷转换为 NTFS。我已尝试了引用链接中的所有建议(即重新排序 PATH 变量中的列表、重命名文件(Windows 显然不允许)以及编辑注册表(用户和本地计算机))。 Windows 似乎(以某种方式)优先考虑位于 System32 目录中的文件。

我还尝试使用 R 中的手动重定向 PATH 变量Sys.setenv()功能。跑步system("where convert", intern=TRUE)产生以下输出:

[1] "C:\\Program Files\\ImageMagick-6.8.9-Q16\\convert.exe" "C:\\Windows\\System32\\convert.exe"

第一个路径是正确的目录。第二条路径是上面引用的 Windows 系统函数。因此,我手动设置 PATH 变量如下:Sys.setenv(PATH=system("where convert", intern=TRUE)[1]).

当我尝试编织时,我似乎超越了convert错误。现在我在 R Markdown 控制台中收到以下错误:

error in texi2dvi(file = file, pdf = true, clean = clean, quiet = quiet, : 
pdflatex is not available 

这实际上是有道理的,因为我重置了 PATH 变量并且没有指定路径pdflatex.exe - only convert.exe。因此,我添加了路径pdflatex.exe使用Sys.setenv()函数如下:Sys.setenv(PATH=paste(Sys.getenv("PATH"),"C:\\Program Files\\MiKTeX 2.9\\miktex\\bin\\x64",sep=";"))。随后调用Sys.getenv("PATH")产生以下输出:

[1] "C:\\Program Files\\ImageMagick-6.8.9-Q16\\convert.exe;C:\\Program Files\\MiKTeX 2.9\\miktex\\bin\\x64"

因此,PATH 变量似乎具有所有必需的信息:convert.exe和路径pdflatex.exe。但是,在 PATH 变量中指定了两个目录后,我现在再次收到原始错误消息:

processing file: test.Rmd

Invalid Parameter - /test_files
Quitting from lines 16-31 (test.Rmd) 
Error in (knit_engines$get(options$engine))(options) : 
  problems with `convert`; probably not installed?
Calls: <Anonymous> ... process_group.block -> call_block -> block_exec -> in_dir -> <Anonymous>
Execution halted

顺便说一句,继续上面的 r-blogger 示例,我可以在系统命令中完全指定路径,如下所示:system('"C:\\Program Files\\ImageMagick-6.8.9-Q16\\convert.exe" -delay 40 *.png example_4.gif')。这将转换示例中的文件,不会出现警告或错误。但是,我无法(据我所知)手动指定路径convert.exe for knitr.

任何其他建议将不胜感激。


所以,这不是最优雅的解决方案,但它确实有效......

按照上面 r-blogger 示例的想法,我分叉了 githubknitr repo here https://github.com/mienkoja/knitr/.

我已经改变了eng_tikz() in the engine.R https://github.com/mienkoja/knitr/blob/master/R/engine.R@yihui 版本的代码函数 到我自己的版本:

## convert tikz string to PDF
eng_tikz = function(options) {
  if (!options$eval) return(engine_output(options, options$code, ''))

  lines = readLines(tmpl <- options$engine.opts$template %n%
                      system.file('misc', 'tikz2pdf.tex', package = 'knitr'))
  i = grep('%% TIKZ_CODE %%', lines)
  if (length(i) != 1L)
    stop("Couldn't find replacement string; or the are multiple of them.")

  s = append(lines, options$code, i)  # insert tikz into tex-template
  writeLines(s, texf <- str_c(f <- tempfile('tikz', '.'), '.tex'))
  unlink(outf <- str_c(f, '.pdf'))
  tools::texi2pdf(texf, clean = TRUE)
  if (!file.exists(outf)) stop('failed to compile tikz; check the template: ', tmpl)
  unlink(texf)

  fig = fig_path('', options)
  dir.create(dirname(fig), recursive = TRUE, showWarnings = FALSE)
  file.rename(outf, str_c(fig, '.pdf'))
  # convert to the desired output-format, calling `convert`
  ext = tolower(options$fig.ext %n% dev2ext(options$dev))
  if (ext != 'pdf'){
    # Changed this line
    # conv = system(sprintf('convert %s.pdf %s.%s', fig, fig, ext))
    # to specify full path to ImageMagick
    conv = system(sprintf('"C:\\Program Files\\ImageMagick-6.8.9-Q16\\convert.exe" %s.pdf %s.%s', fig, fig, ext))
    if (conv != 0) stop('problems with `convert`; probably not installed?')
  }
  options$fig.num = 1L; options$fig.cur = 1L
  extra = knit_hooks$get('plot')(paste(fig, ext, sep = '.'), options)
  options$engine = 'tex'  # for output hooks to use the correct language class
  engine_output(options, options$code, '', extra)
}

可以看出,我只更改了一行 - 我只是完全指定了路径convert.exesprintf()功能。

当我运行我的分叉版本时knitr,我最初收到以下消息knitr:

convert.exe: iCCP: profile 'default_rgb.icc': 0h: PCS illuminant is not D50 
'C:/Users/mienkoja/AppData/Local/Temp/magick-11148QAz4QgZyDsZV1' @ warning/png.c/MagickPNGWarningHandler/1831.
Quitting from lines 16-31 (annual_review.Rmd) 
Error in (knit_engines$get(options$engine))(options) : 
  problems with `convert`; probably not installed?
Calls: <Anonymous> ... process_group.block -> call_block -> block_exec -> in_dir -> <Anonymous>
Execution halted

该警告意味着该函数实际上运行了,我现在看到了tikz-ex-1.pdf and tikz-ex-1.png按预期归档。

然而,knitr之前仍然产生相同的错误.png被“编织”到 HTML 文档中。

这是因为if (conv != 0) stop('problems withconvert; probably not installed?')后的行sprintf()功能似乎正在停止knitr关于警告 - 不仅仅是错误。当我注释掉这一行时,我得到了一份完整的文档。

这显然是一个黑客解决方案(如果有人有更好的想法,我很乐意接受另一个选择)。

我也对有关向 提交功能请求的任何评论感兴趣knitr关于允许路径规范作为与我类似的 Windows 用户的块选项的存储库。使用时能够将参数传递给 ImageMagick 也很好tikz_engine().

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ImageMagick PATH 无法被knitr 中的engine = "tikz" 识别 的相关文章

随机推荐

  • 数组的反序列化不支持类型

    我正在尝试对数组进行反序列化 但我不断遇到错误 JavaScriptSerializer jsonSerializer new JavaScriptSerializer Profiles thingy jsonSerializer Dese
  • 如何使用nodejs禁用tls 1.0并仅使用tls 1.1

    我想禁用 TLS v1 0 并仅使用 TLS 1 1 及更高版本 通过nodejs 我使用https模块 如何设置https选项 我已阅读 api 文档节点 API TLS https nodejs org api tls html 但我还
  • 片段 setuserVisibleHint true 但 getActivity 返回 null

    我一直在片段内部做一些逻辑setUserVisibleHint 方法 我一直在检查是否isVisibleToUser为 true 然后使用getActivity返回活动 在我将支持库更新到最新版本之前 这一切都运行良好 100 的时间 su
  • 当鼠标停止移动时执行Jquery

    我有一个快速脚本 其轨迹跟随光标 jQuery document ready function document mousemove function e fall each function if this css opacity 0 t
  • SQL:使 COUNT(*) > 1 高效

    如果你想知道是否COUNT gt 0 那么你可以使用EXISTS使查询更加高效 当我想知道是否可以时 有没有一种方法可以使查询更有效COUNT gt 1 需要与 SQL Server 和 Oracle 兼容 谢谢 杰米 Edit 我正在尝试
  • Dapper 支持 like 运算符吗?

    使用 Dapper dot net 以下在数据对象中不会产生任何结果 var data conn Query select top 25 Term as Label Type ID from SearchTerms WHERE Term l
  • 线程安全和非线程安全 PHP Windows 安装包之间有哪些技术差异?

    我目前正准备为基于 Apache Windows 的开发环境安装 PHP 但似乎我即将遇到第一个障碍 选择要安装的正确包 PHP 的可用版本不少于four口味 http windows php net download VC9 x86 非线
  • R:简单乘法导致整数溢出

    在较长的脚本中 我必须将向量 A 的长度 2614 乘以数据帧 B 的行数 1456000 如果我直接这样做length A nrow B 我收到消息NAs produced by integer overflow尽管当我乘以相同的数字时没
  • 如何在spark中读取orc事务hive表?

    如何在spark中读取orc事务hive表 我在通过 Spark 读取 ORC 事务表时遇到问题 我获得了配置单元表的架构 但无法读取实际数据 查看完整场景 hive gt create table default Hello id int
  • Selenium 验证 div 有滚动条

    我正在尝试验证特定的 div 有滚动条 如何使用 Selenium 来做到这一点 元素 div class checkout mini cart Code JavascriptExecutor jsExecutor JavascriptEx
  • 当自动递增列用完时会发生什么?

    考虑一个带有自动增量列的简单表 如下所示 CREATE TABLE foo fooid bigint unsigned NOT NULL auto increment snipped other columns PRIMARY KEY fo
  • 除了静态库本身之外,停止 cmake target_link_libraries 链接静态库的两个目标文件

    我尝试使用 cmake ninja msvc 在 Windows 上构建一个相当大的共享库 它由子文件夹中的多个静态库组成 那么一个根CMakeLists txt好像 project sharedlib CXX include CMAKE
  • 如何检查计费协议是否有效或取消?

    我在 PayPal 中使用参考交易 API 不确定计费时计费协议是否有效 我想提前了解计费协议是否被取消 我想知道计费协议取消电话的回拨电话 要检查计费协议的状态 您可以使用 BAUpdate API NVP METHOD BillAgre
  • R 中 N 个元素与 q 个元素的组合

    I have N 6元素和q 3元素符号为0 1 2 我想创建的所有向量N 6元素与2元素等于0 2元素等于1 and 2元素等于2在所有可能的位置 这些向量的数量等于combn 6 2 combn 4 2 combn 2 2 90 这是构
  • qt中的“morph into”有什么用?

    在qt gui编辑器中 任何gui组件都可以变形为某种类型的其他gui组件 但实际上 这个选项的实际用途是什么 可以动态完成吗 如果是的话那么这样做的好处是什么 实际用途是快速将小部件转换为其他类似的小部件 假设您有一个包含一些小部件的组框
  • Azure 无效 AccessToken

    我正在尝试使用 Microsoft Azure Management Resources 库来管理一些 Azure 资源 我已在 Azure AD 中注册了应用程序 并授予了它所有权限 我获取了它的 ApplicationId 和 Secr
  • 链接器文件中输入和输出部分之间的区别?

    虽然在生成的二进制或 ELF 文件的上下文中可以清楚什么是节 但文档中的许多地方 与所使用的编译器无关 将它们称为输入或输出节 它们之间有什么区别 链接器使用目标文件 以及可能的共享库 并输出 可执行文件或共享库 输入目标文件由命名的 部分
  • 在 Python 2.4 中处理上下文类

    我正在尝试使用 python daemon 模块 它提供 daemon DaemonContext 类来正确地守护脚本 虽然我主要针对 Python 2 6 但我想保持对版本 2 4 的向后兼容性 Python 2 5 支持从以下位置导入上
  • 创建一个接受对象但不接受数组的通用函数

    我想创建一个满足以下条件的通用 TypeScript 函数 f a 1 success f undefined success f should fail the type check f 1 2 should fail the type
  • ImageMagick PATH 无法被knitr 中的engine = "tikz" 识别

    我正在尝试编译 TikZ 图形knitr 我正在使用可用的示例here https github com yihui knitr examples blob master 058 engine tikz Rmd 我专门尝试从 Rstudio