在 R 中每小时获取在线数据 [英] get online data every hour in R

查看:48
本文介绍了在 R 中每小时获取在线数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取天文台每小时更新预测的数据.

I would like to get the Observatory data every hour they update the forecast.

我的一次性数据提取代码如下.

My one time data extract code is following.

library(RCurl)
web <- getURL("http://www.hko.gov.hk/contente.htm")
web <- unlist(strsplit(web, "\r\n"))
head(web)

temp <- unlist(strsplit(web[1245], "</span>"))
MINtemp <- vector()
MAXtemp <- vector()
for (i in 1:9){
    mintemp <- substr(temp[2*i-1], 
              nchar(temp[2*i-1])-1, 
              nchar(temp[2*i-1]))
    mintemp <- as.numeric(mintemp)
    MINtemp <- append(MINtemp, mintemp)

    maxtemp <- substr(temp[2*i], 
              nchar(temp[2*i])-1, 
              nchar(temp[2*i]))
    maxtemp <- as.numeric(maxtemp)
    MAXtemp <- append(MAXtemp, maxtemp)
}

status <- strsplit(
             substring(web[1242],12),
        "</a></td><td align")
status <- substring(unlist(status), 178)
weather <- vector()
for (i in 1:9){
    status[i] <- unlist(strsplit(status[i], "width"))[1]
    weather <- append(weather, 
                substr(status[i], 
                     1,
                     nchar(status[i])-3
                     )
                )
}

RH <- unlist(strsplit(web[1248], "</span>"))
MINRH <- vector()
MAXRH <- vector()
for (i in 1:9){
    minRH <- substr(RH[2*i-1], 
              nchar(RH[2*i-1])-1, 
              nchar(RH[2*i-1]))
    minRH <- as.numeric(minRH)
    MINRH <- append(MINRH, minRH)

    maxRH <- substr(RH[2*i], 
              nchar(RH[2*i])-1, 
              nchar(RH[2*i]))
    maxRH <- as.numeric(maxRH)
    MAXRH <- append(MAXRH, maxRH)
}

forecast <- paste("+", 1:9, "day(s)", sep=" ")
current <- as.character(rep(Sys.time(),9))
DATA <- data.frame(cbind(current,forecast,MINtemp, MAXtemp, MINRH, MAXRH, weather))
DATA

我得到的数据是

> DATA

                  current   forecast MINtemp MAXtemp MINRH MAXRH                                                                          weather
    1 2014-05-04 08:37:55 + 1 day(s)      21      25    80    95 Cloudy with a few showers and thunderstorms. Showers will be more frequent later
    2 2014-05-04 08:37:55 + 2 day(s)      22      25    75    90                        Cloudy with showers. A few squally thunderstorms at first
    3 2014-05-04 08:37:55 + 3 day(s)      21      24    75    95                                                        Cloudy with a few showers
    4 2014-05-04 08:37:55 + 4 day(s)      22      25    80    95                                                        Cloudy with a few showers
    5 2014-05-04 08:37:55 + 5 day(s)      23      26    80    95                              Cloudy with showers and a few squally thunderstorms
    6 2014-05-04 08:37:55 + 6 day(s)      23      26    80    95   Cloudy with showers. Showers will be heavy at times with squally thunderstorms
    7 2014-05-04 08:37:55 + 7 day(s)      22      25    80    95                                    Cloudy with showers and squally thunderstorms
    8 2014-05-04 08:37:55 + 8 day(s)      22      25    70    95                                                 Mainly cloudy with a few showers
    9 2014-05-04 08:37:55 + 9 day(s)      22      26    70    90                                                                    Mainly cloudy

我希望 R 脚本每小时自行运行一次.然后使用 rbind(DATA, data) 来累积数据集.我使用 CMD R BATCH 搜索类似的主题.虽然我可以在 R 中使用 Sys.sleep()while(substr(Sys.time(), 15,16)=="00") 那样做吗?

I would like the R script run by itself every hour. Then use rbind(DATA, data) to accumulate the dataset. I search similar topic using CMD R BATCH. While can i do that within R like using Sys.sleep() and while(substr(Sys.time(), 15,16)=="00")?

我搜索了类似的任务来安排此链接

I have searched similar task for scheduling This Link

我在目录 C:\Program Files\R\R-3.0.2\bin\Rscript.exe

然后我将 Rscipt 保存在 D:\mydocument\test.r 虽然我仍然不清楚如何完成任务.

and i saved my Rscipt at D:\mydocument\test.r While i still not clear that how to complete the task.

推荐答案

我认为合理的做法是编写一个脚本来完成您想做的所有事情(下载数据、清理、存储, ...) 而不是使用类似的东西:

I think the reasonable course of action is to write a script that does all you want to do (downloading data, cleansing, storing, ...) and than use something like:

  • Windows 下的任务计划程序链接
  • Linux 下的 Cronjob link
  • ICal、crontab、CronniX、Maintidget、Macaroni 和 MacJanitor、自动计划任务、启动编辑器、Mac 上的 launchctl link

... to schedule it - 意味着定期启动它,而不是运行一个 R 会话并一直等待,例如Sys.sleep(60*60).

... to schedule it - meaning to start it at regular intervals instead of having one R session run and waiting all the time via e.g. Sys.sleep(60*60).

这篇关于在 R 中每小时获取在线数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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