我在整个应用程序中广泛使用std :: tr1 :: shared_ptr.这包括在函数参数中传递对象.考虑以下:
class Dataset {...}
void f( shared_ptr< Dataset const > pds ) {...}
void g( shared_ptr< Dataset const > pds ) {...}
...
Run Code Online (Sandbox Code Playgroud)
虽然通过shared_ptr传递数据集对象可以保证它在f和g中的存在,但是这些函数可能会被调用数百万次,这会导致很多shared_ptr对象被创建和销毁.这是最近一次运行的平坦gprof配置文件的片段:
Each sample counts as 0.01 seconds. % cumulative self self total time seconds seconds calls s/call s/call name 9.74 295.39 35.12 2451177304 0.00 0.00 std::tr1::__shared_count::__shared_count(std::tr1::__shared_count const&) 8.03 324.34 28.95 2451252116 0.00 0.00 std::tr1::__shared_count::~__shared_count()
因此,大约17%的运行时用于使用shared_ptr对象进行引用计数.这是正常的吗?
我的应用程序的很大一部分是单线程的,我正在考虑重写一些函数
void f( const Dataset& ds ) {...}
Run Code Online (Sandbox Code Playgroud)
并替换电话
shared_ptr< Dataset > pds( new Dataset(...) );
f( pds …Run Code Online (Sandbox Code Playgroud) 似乎summarise并且summarise_each正在对它们提供的回调函数进行不必要的额外调用.假设我们有以下内容
X <- data.frame( Group = rep(c("G1","G2"),2:3), Var1 = 1:5, Var2 = 11:15 )
Run Code Online (Sandbox Code Playgroud)
看起来像这样:
Group Var1 Var2
1 G1 1 11
2 G1 2 12
3 G2 3 13
4 G2 4 14
5 G2 5 15
Run Code Online (Sandbox Code Playgroud)
进一步假设我们有(可能很昂贵的)功能
f <- function(v)
{
cat( "Calling f with vector", v, "\n" )
## ...additional bookkeeping and processing...
mean(v)
}
Run Code Online (Sandbox Code Playgroud)
我们想要应用于每个组中的每个变量.使用dplyr,我们可以通过以下方式进行:
X %>% group_by( Group ) %>% summarise_each( funs(f) )
Run Code Online (Sandbox Code Playgroud)
但是,输出显示fG1中每个变量都被称为一个额外的时间:
Calling f with …Run Code Online (Sandbox Code Playgroud) 当scales参数设置为时,我似乎无法在刻面图上正确地着色轴文本"free".请考虑以下数据集:
library( ggplot2 )
X <- data.frame( V1 = LETTERS, V2 = runif( 26 ),
V3 = rep( c("F1", "F2"), each = 13 ) )
Run Code Online (Sandbox Code Playgroud)
我们可以在单个面上绘制数据,突出显示字母D,O,T,如下所示:
v <- ifelse( X$V1 %in% c( "D", "O", "T" ), "red", "black" )
g <- ggplot( X, aes( x = V1, y = V2 ) ) + geom_point() +
theme( axis.text.x = element_text( color = v ) )
Run Code Online (Sandbox Code Playgroud)
使用默认值scales = "fixed"正确地绘制切面图会突出显示两个面上的D,O,T.
g + facet_wrap( ~V3 )
Run Code Online (Sandbox Code Playgroud)
但是,切换scales …
考虑 1) 具有潜在大内存打印的自定义类,以及 2) 执行一些预处理的顶级函数,然后创建并返回自定义类的新对象。为了避免不必要的按值复制,函数分配对象并返回指向它的指针。
根据之前的讨论,返回指向新创建对象的指针的正确方法似乎是用Rcpp::XPtr<>. 但是,R 然后将其有效地视为externalptr,我正在努力寻找以现代RCPP_EXPOSED_CLASS和RCPP_MODULE做事方式进行转换的正确方法。
另一种方法是返回原始指针。但是我不能 100% 确定对象内存得到正确清理。我跑去valgrind测试内存泄漏,但没有找到。然而,谁来清理??
测试.cpp
#include <Rcpp.h>
// Custom class
class Double {
public:
Double( double v ) : value(v) {}
double square() {return value*value;}
private:
double value;
};
// Make the class visible
RCPP_EXPOSED_CLASS(Double)
// Option 1: returning raw pointer
Double* makeDouble( double x ) {
Double* pd = new Double(x);
return pd;
}
// Option 2: returning XPtr<>
SEXP …Run Code Online (Sandbox Code Playgroud) 我有一个方形图(宽高比1:1),我正在努力扩展为全宽mainPanel.由于默认情况下height参数400px,图表在面板中间显示为400x400图像:
我看,你可以改变height的参数plotOutput是"100%"或"auto"有面板中使用的图像的天然尺度.然而,plotOutput然后尝试从中检索高度参数renderPlot,该高度参数本身从其获得其height值,plotOutput从而产生"Catch-22"场景和高度图0px.
我正在试图做的是设定height的值renderPlot(或plotOutput)是等于width的mainPanel,但我不能确定如何访问该值.到目前为止,这是我的代码:
library( shiny )
library( ggplot2 )
server <- function(input, output)
{
output$plot1 <- renderPlot({
X <- data.frame( x=rnorm(input$n), y=rnorm( input$n ) )
ggplot( X, aes(x=x, y=y) ) + geom_point() + coord_fixed()
}, height=400 # how to get this value to be mainPanel width? …Run Code Online (Sandbox Code Playgroud) 每个篮子可容纳的水果总数为 10。对于每个篮子,如果计数为 10 并且缺少一个水果,我想为该篮子添加一行,该行表示该水果的计数为 0。这是生成数据帧的代码。
Basket <- c("A","A","B","B","C","C","C")
Fruit <- c("Apple","Orange","Apple","Orange","Orange","Apple", "Guava")
count <- c("5","5","7","3","2","6","4")
data <- data.frame(Basket,Fruit,count)
Basket Fruit count
1 A Apple 5
2 A Orange 5
3 B Apple 7
4 B Guava 3
5 C Orange 2
6 C Apple 6
7 C Guava 4
Run Code Online (Sandbox Code Playgroud)
我基本上希望它看起来像这样:
Basket Fruit count
1 A Apple 5
2 A Orange 5
4 A Guava 0
5 B Apple 7
6 B Orange 0
7 B Guava 3
8 C Orange …Run Code Online (Sandbox Code Playgroud) tidyr::expand() returns all possible combinations of values from multiple columns. I'm looking for a slightly different behavior, where all the values are in a single column and the combinations are to be taken across groups.
For example, let the data be defined as follows:
library( tidyverse )
X <- bind_rows( data_frame(Group = "Group1", Value = LETTERS[1:3]),
data_frame(Group = "Group2", Value = letters[4:5]) )
Run Code Online (Sandbox Code Playgroud)
We want all combinations of values from Group1 with values from Group2. My current clunky solution …
I would like to use `[` as a function argument in lapply to index full rows. It is fairly straightforward to do this with individual elements:
lapply( list(iris, mtcars), `[`, 1, 3 )
# [[1]]
# [1] 1.4
#
# [[2]]
# [1] 160
Run Code Online (Sandbox Code Playgroud)
However, I can't seem to figure out the prefix equivalent to indexing an entire row of a data frame:
lapply( list(iris, mtcars), `[`, 1 ) # Seems to index columns
lapply( list(iris, mtcars), `[`, i=1 ) …Run Code Online (Sandbox Code Playgroud) 我正在编写一个函数来使用 highcharter 重现几个图表,这些图表都将具有类似的格式(和其他内容)。如果名称发生更改,或者如果我想做一些不同的事情并且我正在接受这些参数,我希望能够选择数据集的不同列{{ }}。但是后来我收到了这个奇怪的错误:
Error: Problem with `mutate()` input `x`.
x Input `x` must be a vector, not a `formula` object.
i Input `x` is `~Year`.
Run Code Online (Sandbox Code Playgroud)
这是我的(最小可重现)代码:
library(dplyr)
library(highcharter)
plot_high_chart <- function(.data,
chart_type = "column",
x_value = Year,
y_value = total,
group_value = service) {
.data %>%
hchart(chart_type, hcaes(x = {{x_value}}, y = {{y_value}}, group = {{group_value}}))
}
data %>% plot_high_chart()
Run Code Online (Sandbox Code Playgroud)
这是dput数据的结果:
structure(list(Year = c(2016, 2017, 2017, 2018, 2018, 2018),
service = structure(c(10L, 3L, …Run Code Online (Sandbox Code Playgroud) 对于我的流程图,我有一个详细说明数据流的垂直图表。但是在向下箭头上,我想添加侧箭头来描述丢失数据的去向。我该怎么做呢?我在任何文档和示例中都看不到它,因为它往往涉及更复杂的事情,而且我知道这是一项非常基本的任务!
library(DiagrammeR)
grViz("digraph flowchart {
# node definitions with substituted label text
node [fontname = Helvetica, shape = rectangle, fixedsize = false, width = 1]
1 [label = 'data (100%)']
2 [label = 'data (90.4%)']
3 [label = 'data \\ndata (83.3%)']
4 [label = 'data (66%)']
7 [label = 'data (100%)']
8 [label = 'data (74.4%)']
9 [label = 'data (69.6%)']
10 [label = 'data (55.4%)']
1 -> 2 -> 3 -> 4;
7 -> 8 -> 9 -> 10 …Run Code Online (Sandbox Code Playgroud) r ×9
dplyr ×3
c++ ×2
data.table ×1
dataframe ×1
diagrammer ×1
facet-wrap ×1
ggplot2 ×1
graphviz ×1
performance ×1
rcpp ×1
rlang ×1
shared-ptr ×1
shiny ×1
tidyeval ×1
tidyr ×1