在 R Studio 中从 mclapply 打印 [英] Printing from mclapply in R Studio

查看:71
本文介绍了在 R Studio 中从 mclapply 打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 RStudio 中使用 mclapply 并希望从每个进程向控制台输出一个输出,但这似乎以某种方式被抑制(例如这里提到的:mclapply 是否保证按顺序返回其结果?).我怎样才能让 R Studio 打印类似的东西

I am using mclapply from within RStudio and would like to have an output to the console from each process but this seems to be suppressed somehow (as mentioned for example here: Is mclapply guaranteed to return its results in order?). How could I get R Studio to print something like

x <- mclapply(1:20, function(i) cat(i, "\n"))

到控制台?

我尝试过 print()、cat()、write() 但它们似乎都不起作用.我还尝试明确设置 mc.silent = FALSE 没有效果.

I've tried print(), cat(), write() but they all seem not to work. I also tried to set mc.silent = FALSE explicitly without an effect.

推荐答案

使用 GUI 进行并行处理是有问题的.我编写了很多并行代码,但它不断使我同事的计算机崩溃,因为他坚持使用 Rstudio 而不是控制台 R.

Parallel processing with GUI's is problematic. I write a lot of parallel code and it's constantly crashing my colleague's computer because he insists on using Rstudio instead of console R.

据我所知,RStudio不会将分叉进程的输出传播到 RStudio 控制台.如果您这样做,最好通过 shell 启动 R."

From what I read, RStudio "does not propagate the output of forked processes to the RStudio console. If you are doing this, it is best to start R via a shell."

这对于 RStudio 人员来说是一种解决方法,因为当人们尝试从一堆不同的进程输出到 GUI 时,并行处理通常会破坏 GUI.它在控制台中工作(尽管通常不按顺序),但并行处理专家在听到来自分叉线程的任何 I/O 时会捏住鼻子.

This makes sense as a workaround for the RStudio people because parallel processing typically breaks GUI's when people try to output to the GUI from a bunch of different processes. It works in the console (albeit often not in order) but parallel processing gurus will pinch their noses when they hear about any I/O from a forked thread.

如果你必须有来自分叉线程的输出,请将它们保存在一个字符串中并返回它.然后从主进程收集并输出.或者只是使用控制台进行并行运行.我告诉我的同事是使用 lapply() 在 RStudio 中完成他所有的调试和开发,然后切换到控制台进行实际运行.

If you must have output from forked threads, save them in a string and return it. Then collect and output from the main process. Or just use a console for your parallel runs. What I tell my colleague is to do all his debugging and development in RStudio using lapply(), then switch to a console for the real run.

这篇关于在 R Studio 中从 mclapply 打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆