Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R中的基本日历显示_R_Date_Calendar - Fatal编程技术网

R中的基本日历显示

R中的基本日历显示,r,date,calendar,R,Date,Calendar,在R中是否有基本的日历显示,如Unix的cal程序 我意识到包装基本日期/时间函数(如工作日,seq.date,等等)并不难,但我觉得我不知道一些非常基本的东西,这些东西可能在已经存在的一个优秀的时间/日期包中得到解决。此外,它的开始总是很简单:指定一周中的几天,然后是闰年,为跨越多个月的日历布局(尽管我满足于只打印一个月),等等 我目前的用法是从R切换到Linux或Windows中的日历程序。在R中这样做会更容易 注1。我已经看过了Gtk2和TclTk可用的一些小部件——出于某种原因,支持系

在R中是否有基本的日历显示,如Unix的
cal
程序

我意识到包装基本日期/时间函数(如
工作日
seq.date
,等等)并不难,但我觉得我不知道一些非常基本的东西,这些东西可能在已经存在的一个优秀的时间/日期包中得到解决。此外,它的开始总是很简单:指定一周中的几天,然后是闰年,为跨越多个月的日历布局(尽管我满足于只打印一个月),等等

我目前的用法是从R切换到Linux或Windows中的日历程序。在R中这样做会更容易



注1。我已经看过了Gtk2和TclTk可用的一些小部件——出于某种原因,支持系统的安装效果不好(我认为这是严重的过度使用,从可移植性的角度来看,具有这种依赖性也不是很好)。在Windows中,我甚至尝试了Cygwin,以访问
cal
,但该安装似乎与Rtools冲突。简言之,目前添加多层材料并不是一种特别有效的途径我还没有在lubridate、xts、zoo和其他软件包中找到任何解决方案,尽管我可能遗漏了一些东西

这里有一个函数,可以执行基本的年度或月度日历:

cal <- function(month, year) {

        if(!require(chron)) stop('Unable to load chron package')

     if(missing(year) && missing(month)) {
         tmp <- month.day.year(Sys.Date())
         year <- tmp$year
         month <- tmp$month
     }


    if(missing(year) || missing(month)){  # year calendar
        if(missing(year)) year <- month
        par(mfrow=c(4,3))
        tmp <- seq.dates( from=julian(1,1,year), to=julian(12,31,year) )
        tmp2 <- month.day.year(tmp)
        wd <- do.call(day.of.week, tmp2)
        par(mar=c(1.5,1.5,2.5,1.5))
        for(i in 1:12){
            w <- tmp2$month == i
            cs <- cumsum(wd[w]==0)
            if(cs[1] > 0) cs <- cs - 1
            nr <- max( cs ) + 1
            plot.new()
            plot.window( xlim=c(0,6), ylim=c(0,nr+1) )
            text( wd[w], nr - cs -0.5 , tmp2$day[w] )
            title( main=month.name[i] )
            text( 0:6, nr+0.5, c('S','M','T','W','T','F','S') )
        }

    } else {  # month calendar

        ld <- seq.dates( from=julian(month,1,year), length=2, by='months')[2]-1
        days <- seq.dates( from=julian(month,1,year), to=ld)
        tmp <- month.day.year(days)
        wd <- do.call(day.of.week, tmp)
        cs <- cumsum(wd == 0)
        if(cs[1] > 0) cs <- cs - 1
        nr <- max(cs) + 1
        par(oma=c(0.1,0.1,4.6,0.1))
        par(mfrow=c(nr,7))
        par(mar=c(0,0,0,0))
        for(i in seq_len(wd[1])){ 
            plot.new()
            #box()
        }
        day.name <- c('Sun','Mon','Tues','Wed','Thur','Fri','Sat')
        for(i in tmp$day){
            plot.new()
            box()
            text(0,1, i, adj=c(0,1))
            if(i < 8) mtext( day.name[wd[i]+1], line=0.5,
                at=grconvertX(0.5,to='ndc'), outer=TRUE ) 
        }
        mtext(month.name[month], line=2.5, at=0.5, cex=1.75, outer=TRUE)
        #box('inner') #optional 
    }
}
cal(10,2011)
par(mfg=c(3,2))  # monday oct 10
text(.5,.5, 'Some\nText', cex=2)

par(mfg=c(2,3)) #Tues oct 4
text(1,1, 'Top Right', adj=c(1,1))

par(mfg=c(2,4)) # Wed oct 5
text(0,0, 'Bottom Left', adj=c(0,0))

par(mfg=c(6,2)) # oct 31
tmp.x <- runif(25)
tmp.y <- rnorm(25,tmp.x,.1)
par(usr=c( range(tmp.x), range(tmp.y) ) )
points(tmp.x,tmp.y)

@kohske——这取决于操作系统。可以在UNIX上工作,但不能在Windows上工作…完全正确。这就是为什么这是一个评论,而不是一个答案。@kohske Yep,除了不知道R之外,我不想承认我有时在Windows上运行R。我似乎永远无法完全掩饰我的羞耻+那太酷了。这也引入了很多我不知道的函数。看起来这是我能得到的最好答案,但我只需要一个。:)我鼓励你把它放在一个小软件包中——它非常有用,或者放在你自己的
TeachingDemos
软件包中。我很惊讶我还不熟悉这个。谢谢@格雷格,你说过你可以添加文本。去牢房。你介意给我们举几个例子来演示这种能力吗?一天的情节!我喜欢它。我可以记录我每天做的最好的情节,并在我自己的日历上展示。这可能(也应该?)激励我做更多更好的情节。我采纳了上述建议,并在TeachingDemos软件包中添加了一个修改版本(不再使用chron)(目前只有R-forge上的版本,还没有CRAN上的版本)。现在,它还返回一个函数,如果使用月号中的某一天运行,则会将当前打印帧设置为相应的日期。