File: utils.R

package info (click to toggle)
r-cran-animation 2.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,268 kB
  • sloc: javascript: 873; sh: 15; makefile: 2
file content (69 lines) | stat: -rw-r--r-- 2,485 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# evaluate code in a specified directory
in_dir = function(wd, expr) {
  owd = setwd(wd); on.exit(setwd(owd))
  expr
}

# compress pdf
compress_pdf = function(img.name){
  compress = if (use.pdftk <- !is.null(ani.options('pdftk'))) pdftk else {
    if (use.qpdf  <- !is.null(ani.options('qpdf'))) qpdf
  }
  if (is.null(compress)) return()

  for (f in list.files(
    dirname(img.name), pattern = sprintf('^%s[0-9]*\\.pdf$', basename(img.name)),
    full.names = TRUE
  )) compress(f)
}

# auto browse
auto_browse = function(output){
  if(!ani.options('autobrowse')) return()
  if (.Platform$OS.type == 'windows') {
    try(shell.exec(output))
  } else if (Sys.info()['sysname'] == 'Darwin') {
    system(paste('open ', shQuote(output)))
  } else system(paste('xdg-open ', shQuote(output)))
}

# find ImageMagick/GraphicsMagick
find_magic = function() {
  # try to look for ImageMagick in the Windows Registry Hive, the Program Files
  # directory and the LyX installation
  if (!inherits(try({
    magick.path = utils::readRegistry('SOFTWARE\\ImageMagick\\Current')$BinPath
  }, silent = TRUE), 'try-error')) {
    if (nzchar(magick.path)) {
      convert = normalizePath(file.path(magick.path, 'convert.exe'))
      message('but I can find it from the Registry Hive: ', magick.path)
    }
  } else if (
    nzchar(prog <- Sys.getenv('ProgramFiles')) &&
      length(magick.dir <- list.files(prog, '^ImageMagick.*')) &&
      length(magick.path <- list.files(file.path(prog, magick.dir), pattern = '^convert\\.exe$',
                                       full.names = TRUE, recursive = TRUE))
  ) {
    convert = normalizePath(magick.path[1])
    message('but I can find it from the "Program Files" directory: ', magick.path)
  } else if (!inherits(try({
    magick.path = utils::readRegistry('LyX.Document\\Shell\\open\\command', 'HCR')
  }, silent = TRUE), 'try-error')) {
    convert = file.path(dirname(gsub('(^\"|\" \"%1\"$)', '', magick.path[[1]])), c('..', '../etc'),
                        'imagemagick', 'convert.exe')
    convert = convert[file.exists(convert)]
    if (length(convert)) {
      convert = normalizePath(convert)
      message('but I can find it from the LyX installation: ', dirname(convert))
    } else {
      warning('No way to find ImageMagick!')
      return()
    }
  } else {
    warning('ImageMagick not installed yet!')
    return()
  }
  ## write it into ani.options() to save future efforts
  ani.options(convert = convert)
  return(convert)
}