无法使用engine ="tikz"识别ImageMagick PATH.在针织衫 [英] ImageMagick PATH not being recognized with engine = "tikz" in knitr

查看:119
本文介绍了无法使用engine ="tikz"识别ImageMagick PATH.在针织衫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从knitr编译TikZ图形.我正在使用此处可用的示例.我专门尝试从Rstudio编织.

I am trying to compile TikZ graphics from knitr. I am using the example available here. I am specifically trying to knit from Rstudio.

我从"R Markdown"选项卡中获得的输出如下:

The output I get from the "R Markdown" tab is as follows:

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

如yihui 此处所示,我需要按顺序安装ImageMagick要通过knitr运行TikZ代码,并且需要在我的PATH变量中列出ImageMagick(我正在运行Windows 7).我已经安装了ImageMagick,并且相信我已经在PATH变量中正确指定了该目录.我从Sys.getenv("PATH")

As indicated by yihui here, I need to have ImageMagick installed in order to run TikZ code through knitr and need to have ImageMagick listed in my PATH variable (I am running Windows 7). I have installed ImageMagick and I believe I have properly specified the directory in my PATH variable. I get the following output from 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;在目录列表中排第二,顺序似乎无关紧要-我尝试将其移动到列表中的不同位置并获得相同的结果.

As you can see, C:\\Program Files\\ImageMagick-6.8.9-Q16; is second in my list of directories and order does not seem to matter - I have tried moving it to different locations in the list and get the same result.

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

I have also tried running RStudio as administrator (I have ImageMagick specified in both my user and system PATH variables) and still get the same error message.

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

TikZ also appears to be working fine by itself as I can compile the same code to PDF from an Rnw file or from command-line.

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

Does anyone have any other thoughts as to why knitr is quitting given my settings above?

更新

这似乎是与ImageMagick相关的Windows问题.当我尝试通过r-blogger条目

This appears to be a Windows problem related to ImageMagick. I realized this when I tried to run through Example 4 from the r-blogger entry here.

在该示例结尾处运行system("convert -delay 40 *.png example_4.gif")时,收到以下警告消息:

When I run system("convert -delay 40 *.png example_4.gif") from the end of that example, I get the following warning message:

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

这似乎是一个问题,因为Windows在System32目录中(也在PATH变量中)有一个名为convert.exe的文件.如此处所述,Windows文件是Windows系统实用程序用于将FAT卷转换为NTFS.我已经尝试了所引用链接的所有建议(即对PATH变量中的列表进行重新排序,重命名文件(显然Windows不允许)以及编辑注册表(用户和本地计算机)). Windows似乎(以某种方式)优先考虑了System32目录中的文件.

This appears to be a problem with the fact that Windows has a file in the System32 directory (also in the PATH variable) named convert.exe. As described in more detail here, the windows file is a Windows system utility used for converting FAT volumes to NTFS. I have tried all of the suggestions at the referenced link (i.e. reordering the listings in the PATH variable, renaming the file (apparently not allowed by Windows), and editing the registry (both user and local machine)). Windows appears to (somehow) give primacy to the files located in the System32 directory.

我还尝试使用Sys.setenv()函数从R中手动重定向PATH变量.运行system("where convert", intern=TRUE)会产生以下输出:

I have also tried to manually redirect the PATH variable from within R using the Sys.setenv() function. Running system("where convert", intern=TRUE) yields the following output:

[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]).

The first path is the correct directory. The second path is a Windows system function referenced above. Thus, I manually set the PATH variable as follows: Sys.setenv(PATH=system("where convert", intern=TRUE)[1]).

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

When I try to knit I appear to move past the convert error. Now I get the following error in my R Markdown console:

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

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

This actually makes sense since I reset the PATH variable and have not specified a path to pdflatex.exe - only convert.exe. Thus, I added the path to pdflatex.exe using the Sys.setenv() function as follows: Sys.setenv(PATH=paste(Sys.getenv("PATH"),"C:\\Program Files\\MiKTeX 2.9\\miktex\\bin\\x64",sep=";")). A subsequent call to Sys.getenv("PATH") yields the following output:

[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变量中指定了两个目录之后,我现在再次得到原始错误消息:

Thus, the PATH variable appears to have all of the requisite information: the path to convert.exe and the path to pdflatex.exe. However, with both directories specified in the PATH variable, I am now getting the original error message again:

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命令中完全指定路径,如下所示:system('"C:\\Program Files\\ImageMagick-6.8.9-Q16\\convert.exe" -delay 40 *.png example_4.gif').这将转换示例中的文件,而不会出现警告或错误.但是,(据我所知)我不能为knitr手动指定convert.exe的路径.

As an aside, continuing with the r-blogger example from above, I can fully specify the path in the system command as follows: system('"C:\\Program Files\\ImageMagick-6.8.9-Q16\\convert.exe" -delay 40 *.png example_4.gif'). This will convert the files in the example with no warnings or errors. However, I cannot (to my knowledge) manually specify the path to convert.exe for knitr.

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

Any other suggestions would be much appreciated.

推荐答案

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

So, this is not the most elegant solution, but it does work...

根据上面的r-blogger示例的思想,我在此处.

Following the thoughts of the r-blogger example from above, I forked the github knitr repo here.

我已经更改了 engine.R @yihui版本的代码功能 到我自己的版本:

I have changed the eng_tikz() in the engine.R code function from @yihui's version to my own version:

## 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)
}

可以看出,我只更改了一行-我只是在sprintf()函数中完全指定了convert.exe的路径.

As can be seen, I have only changed one line - I am simply fully specifying the path to convert.exe within the sprintf() function.

运行knitr的派生版本时,最初从knitr返回以下消息:

When I run my forked version of knitr, I initially got the following message back from 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.pdftikz-ex-1.png文件,这与预期的一样.

The warning implies that the function actually ran and I do now see a tikz-ex-1.pdf and tikz-ex-1.png file as expected.

但是,在.png被编入" HTML文档之前,knitr仍然会产生相同的错误.

However, knitr is still producing the same error before the .png gets "knit" into an HTML document.

这是因为sprintf()函数之后的if (conv != 0) stop('problems with convert ; probably not installed?')行似乎在警告时停止了knitr-不仅仅是错误.当我注释掉这一行时,我得到了一个完整的文档.

This is because the if (conv != 0) stop('problems withconvert; probably not installed?') line after the sprintf() function appears to be stopping knitr on warnings - not just errors. When I comment this line out, I get a fully knitted document.

这显然是一个棘手的解决方案(如果有人有更好的主意,我很乐意接受另一种选择).

This is obviously a hackish solution (and I would gladly accept another option if anyone has a better idea).

对于将特征请求提交给knitr仓库的任何评论,我也将感兴趣,其中涉及允许路径指定作为Windows用户的块选项的方式与我类似.能够在使用tikz_engine()时将参数传递给ImageMagick也是很好的.

I would also be interested in any comments about submitting a feature request to the knitr repo about allowing for path specification as a chunk option for windows users in a similar boat to me. It would also be nice to have the ability to pass arguments to ImageMagick when using tikz_engine().

这篇关于无法使用engine ="tikz"识别ImageMagick PATH.在针织衫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆