小编ric*_*rey的帖子

向量中不同元素之间元素数量的计数

假设我有一个值向量,例如:

A C A B A C C B B C C A A A B B B B C A
Run Code Online (Sandbox Code Playgroud)

我想为每个元素创建一个新向量,该向量包含该元素上次出现以来的元素数。所以,对于上面的向量,

NA NA  2 NA  2  4  1  4  1  3  1  7  1  1  6  1  1  1  8  6
Run Code Online (Sandbox Code Playgroud)

(其中NA表示这是第一次看到该元素)。

比如第一个和第二个A分别在位置1和3,相差2;第三个和第四个 A 在位置 4 和 11,相差 7,依此类推。

是否有预先构建的管道兼容功能可以做到这一点?

我把这个函数混在一起来演示:

# For reproducibility
set.seed(1)

# Example vector
x = sample(LETTERS[1:3], size = 20, replace = TRUE)


compute_lag_counts = function(x, first_time = NA){ …
Run Code Online (Sandbox Code Playgroud)

grouping r vector difference

9
推荐指数
1
解决办法
238
查看次数

如何在 rmarkdown/knitr 中按顺序输出部分(基本 r)图?

假设我有以下 rmarkdown 代码:

---
title: "Untitled"
author: "Author"
date: "04/12/2019"
output: ioslides_presentation
---

## Slide title

```{r echo=FALSE}
plot(1:10, axes = FALSE, ty = "n")
axis(1)
## Next
axis(2)
## Next
points(1:10, 1:10)
```
Run Code Online (Sandbox Code Playgroud)

在每个## Next点,我想输出绘图的当前状态,以便我可以按顺序显示绘图的各个部分。我的最终目标是在ioslides演示文稿中创建后续幻灯片,其中包含顺序图,全部来自上面的代码(有一个警告,理想情况下,我不希望后面的代码行能够影响前面的代码行,就像上面可能发生的那样)。

我希望它与我目前解决它的方式具有相同的效果:

---
title: "Untitled"
author: "Author"
date: "04/12/2019"
output: ioslides_presentation
---

```{r setup, include=FALSE}

## Set up environment for running the code
env <- new.env()

## Source code to run, as a list of quotes

full_src <- list(
  quote({
    plot(1:10, …
Run Code Online (Sandbox Code Playgroud)

r knitr r-markdown

7
推荐指数
1
解决办法
482
查看次数

svg 设备、表达式() 和 unicode 字符的问题

我在使用带有数学表达式和 unicode 的 svg 设备时遇到困难(在本例中为左引号和右引号)。这只发生在我的 Windows 10 计算机上;我的 osx 计算机似乎不受影响。考虑以下 R 代码:

\n\n
output = tempfile(fileext = ".svg")\n\nsvg(file = output)\n\nplot(1, axes=FALSE, ty=\'n\')\n\n## Line A: \nmtext(side = 2, text = expression(paste("A")))\n## Line B:\nmtext(side = 3, text = expression(paste("\xe2\x80\x9cB")))\n## Line C: \nmtext(side = 4, text = expression(paste("C")))\n\naxis(1, at = par()$usr[1:2], lab = c("More", "Less"), tick = FALSE)\n\ndev.off()\n\n## Uncomment and run to open the SVG file to look at it \n# browseURL(output)\n
Run Code Online (Sandbox Code Playgroud)\n\n

(注意 B 行中的左引号)。运行C线后,我得到

\n\n
Error in mtext(side = 4, text …
Run Code Online (Sandbox Code Playgroud)

unicode svg r

6
推荐指数
1
解决办法
471
查看次数

在 Big Sur 上安装自制 ruby​​ 时出现“ld: library not found for -lSystem”

安装 OS11 Big Sur 使我的 ruby​​ 安装不再有效,所以我正在尝试重新安装它。我已rbenv按照此处的说明进行安装,rbenv-doctor脚本运行表明没有问题。但是当我尝试运行时rbenv install 2.7.2,出现与编译器设置相关的错误:

% rbenv install 2.7.2
Downloading ruby-2.7.2.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.7/ruby-2.7.2.tar.bz2
Installing ruby-2.7.2...
ruby-build: using readline from homebrew

BUILD FAILED (macOS 11.0.1 using ruby-build 20201005)

Inspect or clean up the working tree at /var/folders/yn/wzkl_t8567l9pvwhp0wsm40w0000gn/T/ruby-build.20201121103616.80606.FFBFEn
Results logged to /var/folders/yn/wzkl_t8567l9pvwhp0wsm40w0000gn/T/ruby-build.20201121103616.80606.log

Last 10 log lines:
tool/config.guess already exists
tool/config.sub already exists
checking build system type... x86_64-apple-darwin20.1.0
checking host system type... x86_64-apple-darwin20.1.0
checking target system type... x86_64-apple-darwin20.1.0
checking whether the …
Run Code Online (Sandbox Code Playgroud)

ruby macos homebrew rbenv

5
推荐指数
1
解决办法
1327
查看次数

闪亮的应用程序下载按钮和 future_promise:enc2utf8 中的错误:参数不是字符向量

我正在尝试创建一个闪亮的下载处理程序,但使用 future_promise() 因为写入文件可能需要一些时间。这是我想做的一个工作示例,但不使用异步框架:

一个有效的 .Rmd 闪亮应用程序:当您单击按钮时,它会将 10 个随机偏差写入文件并提供下载。我添加了 5 秒的延迟。

---
title: "download, no futures"
runtime: shiny
output: html_document
---

```{r setup, include=FALSE}
library(dplyr)
knitr::opts_chunk$set(echo = FALSE)
```

This version works.

```{r}
renderUI({
  
  button_reactive <- reactive({
    y = rnorm(10)
    Sys.sleep(5)
    tf = tempfile(fileext = ".txt")
    cat(c(y,'\n'), sep='\n', file = tf)
    d = readBin(con = tf, what = "raw", n = file.size(tf))
    return(list(fn = basename(tf), d = d))
  })
  
  output$button <- downloadHandler(
      filename = function() {
        button_reactive() %>%
          `[[`('fn')
      },
      content …
Run Code Online (Sandbox Code Playgroud)

r shiny r-future r-promises

5
推荐指数
1
解决办法
162
查看次数

编译包Rcpp,RcppProgress时,"2个用于体系结构的重复符号"

我怀疑这个问题源于对c ++的误解,但是因为我使用Rcpp,所以我已经标记了这个问题.我创建了两个函数,每个函数都在一个单独的.cpp文件中.

f1.cpp:

// [[Rcpp::depends(RcppProgress)]]
#include <progress.hpp>

using namespace Rcpp;

// [[Rcpp::export]]
NumericVector f1(int n)
{
    int i;
    Progress p(n, true);
    NumericVector x(n);

    for( i = 0 ; i < n ; i++ )
    {
      if (Progress::check_abort() )
        Rcpp::stop("Operation cancelled by interrupt.");

      p.increment(); // update progress

      x[i] = Rf_rnorm(0,1);
    }
    return x;
}
Run Code Online (Sandbox Code Playgroud)

f2.cpp:

// [[Rcpp::depends(RcppProgress)]]
#include <progress.hpp>

using namespace Rcpp;

// [[Rcpp::export]]
NumericVector f2(int n)
{
    int i;
    Progress p(n, true);
    NumericVector x(n);

    for( i = 0 ; i …
Run Code Online (Sandbox Code Playgroud)

c++ r ld rcpp

2
推荐指数
1
解决办法
402
查看次数

标签 统计

r ×5

c++ ×1

difference ×1

grouping ×1

homebrew ×1

knitr ×1

ld ×1

macos ×1

r-future ×1

r-markdown ×1

r-promises ×1

rbenv ×1

rcpp ×1

ruby ×1

shiny ×1

svg ×1

unicode ×1

vector ×1