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

环境为:
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

同样不输出结果文件。

解决方法:

针对RscriptRConsoleget(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.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值