簡體   English   中英

Jupyter Notebook rpy2 Rmagics:如何設置默認繪圖大小?

[英]Jupyter Notebook rpy2 Rmagics: How to set the default plot size?

我在Jupyter筆記本電腦中使用%%R細胞魔術。 我知道您可以使用以下方法設置每個單元的打印大小:

%%R -w 800 -h 600

但我想為整個筆記本/ R會話設置默認值。

我看了文檔 ,看了R Jupyter Notebook Kernel上的這篇博客文章 ,並看過Plot Size-在IPython Notebook中使用ggplot2(通過rmagic)按單元格設置繪圖大小,但是除非我錯過了顯而易見的東西,似乎沒有辦法為R magic中的情節設置默認情節大小。

這可能嗎? 是否存在隱藏設置,還是必須分別為每個單元格設置-w-h

好問題。 目前,我認為默認設置是R默認設置,但是使用jyputer默認設置是有意義的(如果有的話-我需要查一下)。

如果“ R magic”使用的是R中的默認繪圖設置,則可以使用以下技巧。不幸的是,它不能。

%%R
my_png <- function(...) {
    lst <- as.list(...)
    if (is.null(lst$w)) {
        lst$w <- 1000 # default width
    }
    if (is.null(lst$h)) {
        lst$h <- 700 # default height
    }
    do.call("png", lst)
}
options(device=my_png)

在bitbucket的rpy2跟蹤器上打開一個問題以請求該功能將很有意義。

舊問題,但我也有同樣的想法,找到了該頁面,然后設法進行了抓取,因此希望對某人有用。

解決方法是猴子修補rpy2 ,該方法直接調用R的png方法,而沒有設置默認參數的方法。 請注意,這種方法通常是一個壞主意 ,而且很脆弱,但無法避免。 rpy2發出功能請求以包含默認參數的機制可能是值得的。

所以這里是猴子補丁:

# these are the defaults we want to set:
default_units = 'in' # inch, to make it more easily comparable to matpplotlib
default_res = 100 # dpi, same as default in matplotlib
default_width = 10
default_height = 9
# try monkey-patching a function in rpy2, so we effectively get these
# default settings for the width, height, and units arguments of the %R magic command
import rpy2
old_setup_graphics = rpy2.ipython.rmagic.RMagics.setup_graphics

def new_setup_graphics(self, args):
    if getattr(args, 'units') is not None:
        if args.units != default_units: # a different units argument was passed, do not apply defaults
            return old_setup_graphics(self, args)
    args.units = default_units
    if getattr(args, 'res') is None:
        args.res = default_res
    if getattr(args, 'width') is None:
        args.width = default_width
    if getattr(args, 'height') is None:
        args.height = default_height        
    return old_setup_graphics(self, args)

rpy2.ipython.rmagic.RMagics.setup_graphics = new_setup_graphics

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM