Data.table:使用有光泽的变量动态选择组

时间:2017-10-05 20:27:44

标签: r shiny data.table

我正在尝试开发一个应用,允许用户选择在data.table的“按”部分中使用哪些变量。

将mtcars数据集加载到app中,在UI中会有一个选择:

size_t size = N;
size_t total_expected_events = 0;
for(size_t event_count = size; event_count > 1; event_count /= 64)
    total_expected_events++;
cl_event * events = malloc(total_expected_events * sizeof(cl_event));
cl_mem tmp;
double time = gettime();
size_t event_index = 0;
while (size > 1)
{
    // set the argument list for the kernel command
    clSetKernelArg(kernel, 0, sizeof(cl_mem), &input);
    clSetKernelArg(kernel, 1, sizeof(cl_mem), &output);
    global = size;
    local = 64;

    if(event_index == 0)
        // enqueue the kernel command for execution
        clEnqueueNDRangeKernel(command_queue, kernel, 1, NULL, &global, 
                           &local, 0, NULL, events);
    else
        clEnqueueNDRangeKernel(command_queue, kernel, 1, NULL, &global, 
                           &local, 1, events + (event_index - 1), events + event_index);
    size = size/64;
    tmp = output;
    output = input;
    input = tmp;
    event_index++;
}
clFinish(command_queue);
for(; event_index > 0; event_index--)
    clReleaseEvent(events[event_index-1]);
free(events);
cl_float answer[1];
clEnqueueReadBuffer(command_queue, tmp, CL_TRUE, 0, 
                    sizeof(cl_float), array, 0, NULL, NULL);

此选择输入将在UI中呈现,当用户选择服务器中的某些变量时,将使用如下:

vector.vars <- c('am', 'gear', 'carb')
selectInput("vars", "Select variables:", as.list(vector.vars), selected = 'am', multiple = TRUE)

在这种情况下,如果用户选择了&#39; am&#39; carb&#39;前面的脚本应该执行此操作:

output$table <- renderDataTable({
  d <- mtcars[, list(mpg_mean= mean(mpg), by=input$vars]
  })

并显示&#39; mpg&#39;的平均值。对于'am'的每个组合。和&#39; carb&#39;。

...但是by = input $ vars无法正常工作。

关于如何达到所需输出的任何想法, 提前谢谢!

1 个答案:

答案 0 :(得分:1)

这样做:

as.data.table(mtcars)[, .(mpg_mean = mean(mpg)), by = c(input$vars)]