R markdown循环中的绘图不起作用

时间:2018-05-02 21:26:32

标签: r for-loop r-markdown

我尝试在for中使用R markdown循环,但由于某种原因它无效。这是代码:

```{r}
for (i in 1:length(endothelial_ids)) {
    id <- endothelial_ids[i]
    sym <- neuron_symbols[i]
    VlnPlot(object = seurat_object, features.plot = id, do.return = TRUE) + labs(title = sym)
    renderPlot({
      fp <- FeaturePlot(object = seurat_object, features.plot = id, cols.use = c("grey", "blue"),
                            reduction.use = "tsne", do.return = TRUE)
      lapply(fp, function(x){x + labs(title = sym)})
    })
}
```

没有for循环,代码正常工作,生成的.html报告文件看起来不错。

在控制台中,我看到几个警告:

  

输出文件:/private/var/folders/hq/5ygqq7yn2hd0ln4kznxvx5j80000gn/T/Rtmpj7bedl/cell_type_assignments.knit.md

     

file.rename中的警告(from,to):     无法重命名文件&#39; / private / var / folders / hq / 5ygqq7yn2hd0ln4kznxvx5j80000gn / T / Rtmpj7bedl / file613d370b37d8_files / figure-html&#39; to&#39; / private / var / folders / hq / 5ygqq7yn2hd0ln4kznxvx5j80000gn / T / Rtmpj7bedl // private / var / folders / hq / 5ygqq7yn2hd0ln4kznxvx5j80000gn / T / Rtmpj7bedl / file613d370b37d8_files / figure-html&#39;,reason&#39; No这样的文件或目录&#39;   file.rename(from,to)中的警告:     无法重命名文件&#39; / private / var / folders / hq / 5ygqq7yn2hd0ln4kznxvx5j80000gn / T / Rtmpj7bedl // private / var / folders / hq / 5ygqq7yn2hd0ln4kznxvx5j80000gn / T / Rtmpj7bedl / file613d370b37d8_files / figure-html&#39;到&#39; / private / var / folders / hq / 5ygqq7yn2hd0ln4kznxvx5j80000gn / T / Rtmpj7bedl / file613d370b37d8_files / figure-html&#39;,原因&#39;没有这样的文件或目录&#39;

     

创建的输出:/private/var/folders/hq/5ygqq7yn2hd0ln4kznxvx5j80000gn/T/Rtmpj7bedl/file613d370b37d8.html

任何建议都将不胜感激。

  

更新

如果我将VlnPlot包裹在print()函数中,则会绘制它,但不会FeaturePlot

  print(VlnPlot(object = seurat_object, features.plot = id, do.return = TRUE) + labs(title = sym))
  print(renderPlot({
  fp <- FeaturePlot(object = seurat_object, features.plot = id, cols.use = c("grey", "blue"),
                            reduction.use = "tsne", do.return = TRUE)
  lapply(fp, function(x){x + labs(title = sym)})
  }))

代替FeaturePlot我在报告中看到以下内容:

enter image description here

如果我删除renderPlot()函数并将lapply()包裹在print()中,它会打印两个图而不是一个:原始错误标题和最终正确标题。因此,我需要一种方法来避免打印仅从代码中发生的原始文件:

fp <- FeaturePlot(object = seurat_object, features.plot = id, cols.use = c("grey", "blue"),
                            reduction.use = "tsne", do.return = TRUE)

0 个答案:

没有答案