postgresql - 调度问题...使用 tasksheduleR 使用 rtweet 运行 R 脚本并导出到 postgreSQL 表
问题描述
我正在尝试安排 R 脚本运行,使用 rtweet 访问 Twitter API,然后使用 RPOSTgreSQL 每天一次将数据加载到表中。
我能够成功地使用 taskscheduleR 来创建任务。但是,当它运行时,我收到一条错误消息...
<credentials> oauth_token, oauth_token_secret
---
Requesting token on behalf of user...
Error: API user token required. see http://rtweet.info/articles/auth.html for instructions
Execution halted
这是我的整个代码,由于 API 凭据和数据库信息、密码等原因,有些东西被掩盖了
#load all packages
library(rtweet)
library(sqldf)
library(dplyr)
library("RPostgreSQL")
#connect to Twitter API
create_token(
app = "masked for stack",
consumer_key = "masked for stack",
consumer_secret = "masked for stack",
access_token = "masked for stack",
access_secret = "masked for stack")
## get user IDs of accounts following
followers=get_followers("masked for stack", n = 1000)
## lookup data on those accounts
followers_data=lookup_users(followers$user_id)
#add the date of the run
followers_data$date=Sys.Date()
#extract columns
twitter_followers=dplyr::select(followers_data,"date","screen_name","name","location","description","followers_count",
"friends_count","listed_count","statuses_count","favourites_count","verified")
# create a connection
# save the password that we can "hide" it as best as we can by collapsing it
pw <- {
"masked for stack"
}
# loads the PostgreSQL driver
drv <- dbDriver("PostgreSQL")
# creates a connection to the postgres database
# note that "con" will be used later in each connection to the database
con <- dbConnect(drv, dbname = "masked for stack",
host = "localhost", port = 5432,
user = "postgres", password = pw)
rm(pw) # removes the password
# writes df to the PostgreSQL database
dbWriteTable(con, "twitter_followers",
value = twitter_followers, append = TRUE, row.names = FALSE)
我的代码运行得非常好,我自己手动完成所有事情。似乎 rtweet 不喜欢被 Windows 调度程序运行。
有任何想法吗?`
解决方案
因此,经过数小时的挖掘,我发现 scheduleR 应用程序无法正常工作。它可能在 Windows 调度程序中创建了一个任务,当我的笔记本电脑使用交流电源时,它不会运行我不确定。无论哪种方式,我都做了这样的解决方法。
创建了一个文本文件并保存为 .bat 扩展名,格式如下:
"C:\Program Files\R\R-3.5.2\bin\x64\R.exe" CMD BATCH file_location.R
从那里我安排在 Windows 调度程序中。确保您完成所有选项并且不要选择基本任务。如果你做一个基本的,你可能不允许它在笔记本电脑电池电源上运行。
推荐阅读
- flutter - 如何在后台模式下访问sql
- r - r 中的估计处理图
- python - 各种长度数组的 numpy mean/std dev
- javascript - DOMStringMap 值到数组?
- spring-boot - spring boot 2.1.5.RELEASE 和 GraphQL 无法绑定“graphiql.endpoint”下的属性
- java - 使用 Java 中的自定义端点从 S3 读取镶木地板文件
- swift - 将精灵套件场景导出为全尺寸图像
- excel - Excel:如果单元格 B2 包含某个字符,则单元格 D2=E2 的值如果不是则 D2,那么如果我在 BX,我想增加到 E3?
- python - 定义 table_schema 时,无法将具有 NaN(或 None)值的 Pandas 数据帧插入 BigQuery 表
- python - /create/ 处的 NoReverseMatch