您的位置:首页 > 运维架构

R语言画图-Error in get(getOption("device"))

2016-12-16 22:45 435 查看
环境为:

R 3.3.1, Rscript 3.3.1 64位

windows 7 64位

当运行如下示例程序画loglog图:

##  Create a data frame with artificial data.

d.f <- data.frame( x = 1:100, y = 1:100 )

##  Open a new default device.

get( getOption( "device" ) )()

##  Plot the data, hiding the points for now to prevent the calls to
##  abline() from drawing over the points.

plot( y ~ x, data = d.f, type = "n", log = "xy", main = "Log-log Plot" )

##  Put grid lines on the plot, using a light blue color ("lightsteelblue2").

abline( h = seq( 0, 100, 10 ), lty = 3, col = colors()[ 440 ] )
abline( v = seq( 0, 100, 10 ), lty = 3, col = colors()[ 440 ] )

##  Draw the points over the grid lines.

points( y ~ x, data = d.f )

##  Redraw the plot box over the grid lines.

box()


1 . 在RGui中的R Console中运行情况:

报错:
Error in get(getOption("device")) : invalid first argument


但是成功打开device并画图。

> ##  Create a data frame with artificial data.
>
> d.f <- data.frame( x = 1:100, y = 1:100 )
>
> ##  Open a new default device.
>
> get( getOption( "device" ) )()
Error in get(getOption("device")) : invalid first argument
>
> ##  Plot the data, hiding the points for now to prevent the calls to
> ##  abline() from drawing over the points.
>
> plot( y ~ x, data = d.f, type = "n", log = "xy", main = "Log-log Plot" )
>
> ##  Put grid lines on the plot, using a light blue color ("lightsteelblue2").
>
> abline( h = seq( 0, 100, 10 ), lty = 3, col = colors()[ 440 ] )
> abline( v = seq( 0, 100, 10 ), lty = 3, col = colors()[ 440 ] )
>
> ##  Draw the points over the grid lines.
>
> points( y ~ x, data = d.f )
>
> ##  Redraw the plot box over the grid lines.
>
> box()


2 . 在
RStudio
(Version 0.99.903)中,
get( getOption( "device" ) )()
返回

NULL
,并成功画图,单独运行该语句依然返回
NULL
但是能成功打开device

> ##  Create a data frame with artificial data.
>
> d.f <- data.frame( x = 1:100, y = 1:100 )
>
> ##  Open a new default device.
>
> get( getOption( "device" ) )()
NULL
>
> ##  Plot the data, hiding the points for now to prevent the calls to
> ##  abline() from drawing over the points.
>
> plot( y ~ x, data = d.f, type = "n", log = "xy", main = "Log-log Plot" )
>
> ##  Put grid lines on the plot, using a light blue color ("lightsteelblue2").
>
> abline( h = seq( 0, 100, 10 ), lty = 3, col = colors()[ 440 ] )
> abline( v = seq( 0, 100, 10 ), lty = 3, col = colors()[ 440 ] )
>
> ##  Draw the points over the grid lines.
>
> points( y ~ x, data = d.f )
>
> ##  Redraw the plot box over the grid lines.
>
> box()
>
>


3 . 将示例代码存入脚本文件,在windows 7 x64
cmd
中使用
Rscript
运行脚本文件,报错:

Error in get(getOption("device")) :


不输出结果文件。

Mac OS X 10.9.4中用
Rscript
运行脚本报错为:

Error in get(getOption("device")) : invalid first argument
Execution halted


同样不输出结果文件。

解决方法:

针对
Rscript
RConsole
get(getOption("device"))
改为
dev.new()
RConsole
不再报错,
Rscript
运行脚本正常默认输出pdf文件也不报错

参照:

[1] http://r.789695.n4.nabble.com/R-2-8-0-get-platform-device-with-get-getOption-quot-device-quot-td920607.html

The “device” option can now be a function, and is one in some standard setups. Consequentially, get(getOption(“device”)) will fail; programmers should usually use dev.new() instead.

[2] https://stat.ethz.ch/pipermail/r-devel/2008-April/049319.html

2.7.0 provides a function dev.new()

.

As from R 2.5.0, options(“device”) can be a character string or a function. If the latter, this construct does not work.

[3] R in Action, Second Edition. Robert I. Kabacoff. May 2015. ISBN 9781617291388. 608 pages. printed in black & white.

First, you can open a new graph window before creating a new graph:

dev.new()


statements to create graph 1

dev.new()


statements to create a graph 2

etc.

Each new graph will appear in the most recently opened window.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  r语言
相关文章推荐