Почему R выдает «Предупреждение: в файле (con,« r »): не удается открыть файл» из пакета, но не из источника? - PullRequest
0 голосов
/ 10 мая 2019

Я пишу свой первый пакет, JTools. У меня есть функция lastCall(), которая печатает предыдущие вызовы функций:

lastCall <- function(calls.to.return = 1) {
    if (length(calls.to.return) == 1) {
        num.call <- 1:calls.to.return
    } else {
        num.call <- calls.to.return
    }
    history.file <- tempfile()
    try(savehistory(history.file), silent = TRUE )
    try(
        raw.history <- readLines(history.file),
        silent = TRUE
    )
    unlink(history.file)
    if (exists('raw.history') ) {
        # create object "commands" with type "expression"
        commands <- expression()
        num.line <- max(abs(num.call) + 1)
        # look through at least max(num.call)+1 lines of raw.history until at
        # least max(n)+1 functioning calls/commands have been found
        while (length(commands) < max(abs(num.call) + 1)) {
            lines <- tail(raw.history, n = num.line)
            # check if current "lines" constitutes funtional calls
            try(commands <- parse(text = lines), silent = TRUE)
            num.line <- num.line + 1
            # stop while loop if all of raw.history has been evaluated
            if (num.line > length(raw.history)) break
        }
        ret <- rev(commands)[num.call + 1]
        if (length(ret) == 1) {
            calls <- ret[1]
        } else {
            calls <- ret
        }
        out <- lapply(calls, deparse) %>%
            sapply(paste, collapse = "\n") %>%
            paste(collapse = "\n\n## preceding call ###################\n") %>%
            c("most recent call\n", .) %>%
            paste(collapse = "") %>%
            paste("\n")
    } else {out <- NULL}
    out
}

Эта функция работает, как и ожидалось, когда я получаю код непосредственно в RStudio. Затем я включаю функцию в пакет, используя следующий код, который, кажется, работает:

pkg_name <- "JTools"
imports <- c("utils", "magrittr", "easyPubMed")
require(devtools)
require(magrittr)
pkg_imports <- imports %>%
    sort() %>%
    sapply(function(pkg) {
        paste0(pkg, " (>= ", packageVersion(pkg), ")")
    }) %>%
    paste(collapse = ",\n         ")
options(devtools.desc = list(Imports = pkg_imports))
pkg_path <- file.path("C:/Users/JT/R/Packages", pkg_name)
devtools::create_description(pkg_path)
#  No DESCRIPTION found. Creating with values:
#  Package: JTools
#  Title: What the Package Does (one line, title case)
#  Version: 0.0.0.9000
#  Authors@R: person("First", "Last", email = "first.last@example.com", role = #  #  c("aut", "cre"))
#  Description: What the package does (one paragraph).
#  Depends: R (>= 3.5.3)
#  License: What license is it under?
#  Encoding: UTF-8
#  LazyData: true
#  Imports: easyPubMed (>= 2.13),
#           magrittr (>= 1.5),
#           utils (>= 3.5.3)
#  [1] TRUE
devtools::document(pkg_path)
#  Updating JTools documentation
#  Loading JTools
#  Welcome to my package of custom functions. They're grRreat!
#  First time using roxygen2. Upgrading automatically...
#  Updating roxygen version in C:\Users\JT\R\Packages\JTools/DESCRIPTION
#  Writing NAMESPACE
#  Writing NAMESPACE
#  Writing JTools.Rd
#  Writing [blah blah blah]
.
.
devtools::load_all(pkg_path)
#  Loading JTools
#  Welcome to my package of custom functions. They're grRreat!
devtools::install(pkg_path)
#  Installing JTools
#  "C:/PROGRA~1/R/R-35~1.3/bin/x64/R" --no-site-file --no-environ --no-save --no-restore --quiet CMD INSTALL  \
#  "C:/Users/JT/R/Packages/JTools" --library="C:/Program Files/R/R-3.5.3/library" --install-tests 
#  
#  every R session
#  * installing *source* package 'JTools' ...
#  ** R
#  ** byte-compile and prepare package for lazy loading
#  ** help
#  *** installing help indices
#  converting help for package 'JTools'
#  finding HTML links ... done
#  JTools                                  html  
#  clr                                     html  
#  dot-onAttach                            html  
#  func                                    html  
#  lastCall                                html  
#  packageInstallLoad                      html  
#  removeWindowsProhibited                 html  
#  rmAll                                   html  
#  timeStamp                               html  
#  ** building package indices
#  ** testing if installed package can be loaded
#  *** arch - i386
#  
#  *** arch - x64
#  
#  * DONE (JTools)
#  In R CMD INSTALL
#  Reloading installed JTools
#  Welcome to my package of custom functions. They're grRreat!

В этот момент я пытаюсь загрузить пакет JTools, запустить lastCall() и получить следующую ошибку:

library(JTools)
lastCall(5)
#  NULL
#  Warning message:
#  In file(con, "r") :
#    cannot open file 'C:\Users\JT\AppData\Local\Temp\RtmpAnT4WV\file27d4be7246d': No such file or directory

Каталог C:\Users\JT\AppData\Local\Temp\RtmpAnT4WV\ существует, поэтому я не уверен, что происходит. Спасибо.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...