我正在开发一个依赖于包的包foo,在我的包代码中有一个对从中导出的函数的调用foo.我已经包括在包的名称NAMESPACE与import(foo)声明,并还可根据Imports:其线性DESCRIPTION文件.但是,运行时收到以下警告R CMD check:
r 'library' or 'require' calls not declared from: 'foo'
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
当我试图在其中运行Python解释器时lldb,我看到:
$ lldb
(lldb) script
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/copy.py", line 52, in <module>
import weakref
File "/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/weakref.py", line 14, in <module>
from _weakref import (
ImportError: cannot import name _remove_dead_weakref
Python Interactive Interpreter. To exit, type 'quit()', 'exit()' or Ctrl-D.
Run Code Online (Sandbox Code Playgroud)
当我检查启动了哪个版本的Python时,Python报告它应该是Homebrew Python(它被符号链接到这个位置):
>>> sys.executable
'/usr/local/opt/python/bin/python2.7'
Run Code Online (Sandbox Code Playgroud)
但是,询问Python版本会返回与默认系统 Python安装相关联的版本,例如
>>> sys.version_info
sys.version_info(major=2, minor=7, micro=10, releaselevel='final', serial=0)
Run Code Online (Sandbox Code Playgroud)
并且,只是为了确认,上面的二进制路径上的Python版本确实不同(注意微版本的差异):
$ /usr/local/opt/python/bin/python2.7 --version
Python 2.7.14
$ /usr/bin/python --version
Python 2.7.10
Run Code Online (Sandbox Code Playgroud)
为了使事情变得更加混乱,名称_remove_dead_weakref 不 …
令我感到惊讶的是:让我们比较两种获取class变量的方法,这些方法包含许多列的大数据框中的变量:sapply解决方案和for循环解决方案.
bigDF <- as.data.frame( matrix( 0, nrow=1E5, ncol=1E3 ) )
library( microbenchmark )
for_soln <- function(x) {
out <- character( ncol(x) )
for( i in 1:ncol(x) ) {
out[i] <- class(x[,i])
}
return( out )
}
microbenchmark( times=20,
sapply( bigDF, class ),
for_soln( bigDF )
)
Run Code Online (Sandbox Code Playgroud)
在我的机器上给了我
Unit: milliseconds
expr min lq median uq max
1 for_soln(bigDF) 21.26563 21.58688 26.03969 163.6544 300.6819
2 sapply(bigDF, class) 385.90406 405.04047 444.69212 471.8829 889.6217
Run Code Online (Sandbox Code Playgroud)
有趣的是,如果我们转换bigDF成一个列表,sapply …
生成图的代码是否可以从.Rmd文档输出两个版本的同一图形,大小不同?要么通过块选项(我没有看到任何直接在这里工作的东西),或通过自定义knitr钩子?优选地,这将通过该png设备完成.
我的动机:我希望能够输出一个大小的数字,这个数字可以在编译的HTML文档中内嵌,以及用户在点击后可以显示的另一个数字(想想fancybox).我想我将能够处理完成这项工作所需的脚本; 但是,首先我需要说服R/knitr输出该图的两个版本.
虽然我确信有一些解决方法,但最好还是有办法让它在幕后"正常工作",例如通过knitr钩子.这样,我们不必对块中的R代码做任何特殊操作,我们只是修改我们解析/评估该块的方式.
或者,可以使用可以很好地缩放的SVG图形,但是然后我们失去了对于标记标签的良好尺寸的良好推断,并且矢量图形对于具有许多点的图不是很好.
假设我有一个矩阵,其条目仅为0和1,例如
set.seed(123)
m <- matrix( sample(0:1, 10, TRUE), nrow=5 )
Run Code Online (Sandbox Code Playgroud)
带样本输出:
[,1] [,2]
[1,] 0 0
[2,] 1 1
[3,] 0 1
[4,] 1 1
[5,] 1 0
Run Code Online (Sandbox Code Playgroud)
矩阵最多有20列,并且会有很多行.
我想要一个函数,让我们调用它rowCounts,返回:
我怎么能解决这个问题?
我想编写一个函数,在给定的索引处按顺序将"字符串"切换为向量.我有一个相当充分的R解决方案; 但是,我认为用C/C++编写代码可能会更快.例如,我希望能够编写一个函数'strslice',其操作如下:
x <- "abcdef"
strslice( x, 2 ) ## should return c("ab", "cd", "ef")
Run Code Online (Sandbox Code Playgroud)
但是,我不知道如何处理在Rcpp代码中传递的'CharacterVector'元素作为字符串.这是我想象的可能工作(鉴于我缺乏C++/Rcpp知识,我确信有更好的方法):
f <- rcpp( signature(x="character", n="integer"), '
std::string myString = Rcpp::as<std::string>(x);
int cutpoint = Rcpp::as<int>(n);
vector<std::string> outString;
int len = myString.length();
for( int i=0; i<len/n; i=i+n ) {
outString.push_back( myString.substr(i,i+n-1 ) );
myString = myString.substr(i+n, len-i*n);
}
return Rcpp::wrap<Rcpp::CharacterVector>( outString );
')
Run Code Online (Sandbox Code Playgroud)
为了记录,我有相应的R代码是:
strslice <- function(x, n) {
x <- as.data.frame( stringsAsFactors=FALSE,
matrix( unlist( strsplit( x, "" ) ), ncol=n, byrow=T )
)
do.call( function(...) …Run Code Online (Sandbox Code Playgroud) 首先,设置阶段的快速示例:
set.seed(123)
dat <- data.frame(
x=rep( c(1, 2, 4, 7), times=25 ),
y=rnorm(100),
gp=rep(1:2, each=50)
)
p <- ggplot(dat, aes(x=factor(x), y=y))
p + geom_boxplot(aes(fill = factor(gp)))
Run Code Online (Sandbox Code Playgroud)

我想制作一个类似的情节,除了控制每组箱图的x位置.我的第一个猜测是使用非因素x美学来控制沿着这些箱形图的x轴的位置.然而,一旦我尝试这样做,似乎geom_boxplot并没有像我希望的那样解释美学.
p + geom_boxplot( aes(x=x, y=y, fill=factor(gp)) )
Run Code Online (Sandbox Code Playgroud)

特别是,geom_boxplot当它们是非因素时,似乎以某种方式崩溃所有x值.
有没有办法x用ggplot2 控制箱线图的位置?要么通过指定因子美学的每个层次之间的距离,要么更巧妙地使用非要素美学,否则?
我要写点文字,因为我的英语不太好。因此,我对RStudio有问题。当我在R中写这个时->
explanatory=readRDS("explanatory_complete.Rds")
Run Code Online (Sandbox Code Playgroud)
我想要编织pdf或word文档,我收到此消息:
"Error in gzfile(file, "rb")- can't oppen connection"
Run Code Online (Sandbox Code Playgroud)
问题在哪里?我设置好的工作目录。
我和我的同事正在 RStudio 中使用 Bookdown 编写一本书。由于我使用 ggplot 创建了很多图形,因此对其他软件包(ggplot、ggforce、gganimate 等)有很多依赖项,因此我在本书的过程中添加了 renv 来负责软件包管理。全新安装 Windows 后,我现在再次设置项目时遇到问题,因为有从 R 到版本 3.6.3 的更新并使用了软件包。
renv 项目的初始状态是 R,版本为 3.6.2,我重新安装了它。打开项目后,我运行renv::restore()以恢复所有包的环境。目前只有 nloptr 包(版本 1.2.1)有一个 bug,因为它必须从源代码构建包。
Installing minqa [1.2.4] ...
OK (linked cache)
Installing nloptr [1.2.1] ...
FAILED
Error installing package 'nloptr':
==================================
* installing *source* package 'nloptr' ...
** package 'nloptr' successfully unpacked and MD5 sums checked
** using staged installation
**********************************************
WARNING: this package has a configure script
It probably needs manual configuration
**********************************************
** libs
C:/Rtools/mingw_64/bin/g++ -std=gnu++11 …Run Code Online (Sandbox Code Playgroud) 作为一个简单,具体的例子:
#' Inverse Value Matching
#'
#' Complement of \code{%in%}. Returns the elements of \code{x} that are
#' not in \code{y}.
#' @usage x %nin% y
#' @param x a vector
#' @param y a vector
#' @export
"%nin%" <- function(x, y) {
return( !(x %in% y) )
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试构建一个包时,该函数似乎被忽略,并且没有生成文档.
在http://cran.r-project.org/doc/manuals/r-release/R-exts.html#Documenting-functions上似乎有关于二进制中缀函数的单行模糊,但我很难时间解析它,以及它对Roxygen文档意味着什么.
假设我有以下语言对象:
lang <- quote( f(x=a) )
Run Code Online (Sandbox Code Playgroud)
我想在替代1的a。我怎样才能做到这一点?
我期望substitute做我想做的,但是
substitute(lang, list(a=1))
Run Code Online (Sandbox Code Playgroud)
刚返回lang,而
substitute(f(x=a), list(a=1))
Run Code Online (Sandbox Code Playgroud)
确实做到了我的期望。
我有矩阵,我想写一个函数,得到矩阵的元素并返回矩阵内的数字坐标.有人可以提出如何实施它的想法吗?
> A
[,1] [,2]
[1,] 10 20
[2,] 21 17
[3,] 13 25
[4,] 21 11
[5,] 31 24
Run Code Online (Sandbox Code Playgroud)
例如
MyFunction的(11)
> 11
> row 3, col 1
Run Code Online (Sandbox Code Playgroud) 可能重复:
使用substitute获取参数名称
请注意,这与使用矢量本身list(...)或某种形式的东西不同....在完成任何解析之前,我希望能够做的只是'echo'传入的所有参数.
例如:我想要一个可能就像:
f(apple, banana, car)
## --> returns c("apple", "banana", "car"),
## ie, skips looking for the objects apple, banana, car
Run Code Online (Sandbox Code Playgroud)
我得到的最接近的是
f <- function(...) {
return( deparse( substitute( ... ) ) )
}
Run Code Online (Sandbox Code Playgroud)
但这只会返回第一个被"抓住"的参数....思考?