我正在写我的第一个软件包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\
确实存在,所以我不确定发生了什么。谢谢。