我一直试图通过R从PostgreSQL数据库查询数据.我尝试用一些不同的软件包(RODBC, RJDBC, DBI, RPostgres等等)对猫进行换肤,但我似乎不断收到驱动程序错误.奇怪的是,我从来没有遇到过使用相同的驱动程序/ URL和设置从SQLWorkbench/J连接到Postgres的麻烦.
我已尝试使用postgresql-9.2-1002.jdbc4.jar和postgresql-9.3-1100.jdbc41.jar,以及"PostgreSQL"R中的通用驱动程序.两个jar文件分别是(i)我一直使用SQLWorkbench/J的驱动程序和(ii)相同驱动程序的稍新版本.然而,当我尝试使用它时......
drv_custom <- JDBC(driverClass = "org.postgresql.Driver", classPath="/Users/xxxx/postgresql-9.3-1100.jdbc41.jar")
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
Error in .jfindClass(as.character(driverClass)[1]) : class not found
Run Code Online (Sandbox Code Playgroud)
好的,接下来我尝试使用通用驱动程序:
drv_generic <- dbDriver("PostgreSQL")
Run Code Online (Sandbox Code Playgroud)
奇怪的是,它不希望我输入用户名:
>con <- dbConnect(drv=drv_generic, "jdbc:postgresql://xxx.xxxxx.com", port=xxxx, uid="xxxx", password="xxxx")
>Error in postgresqlNewConnection(drv, ...) : unused argument (uid = "xxxx")
Run Code Online (Sandbox Code Playgroud)
所以我没有用户/ uid尝试:
con < - dbConnect(drv_generic,"jdbc:postgresql://padb-01.jiwiredev.com:5439",password ="paraccel")
并得到一个错误....
Error in postgresqlNewConnection(drv, ...) :
RS-DBI driver: (could not connect jdbc:postgresql://padb-01.xxx.com:5439@local on dbname "jdbc:postgresql://xxxx.xxxx.com:5439")
Run Code Online (Sandbox Code Playgroud)
显然语法错了?
然后我回过头来尝试"自定义"驱动程序(前面的.jar文件之一),但没有指定driverClass.
drv_custom1 < - JDBC(classPath ="/ Users/xxxx/postgresql-9.2-1002.jdbc4.jar") …
我正在组合两个具有相同列的表.它们来自两个不同的来源,如治疗组和对照组.我想知道每一行的来源.
有可能通过某种方式修改我的查询吗?
--Combine them
create table x as (
select * from y
union all
select * from z);
Run Code Online (Sandbox Code Playgroud) 我第一次使用R来学习如何编写分类算法.我主要是从O'reilly书中学习黑客机器学习.
R,R文档,电子书以及关于编写函数的书中有大量信息.但是,本书中有一些代码片段使用单词function,其方式似乎不符合我对函数使用的理解.例如:
all.spam <- sapply(spam.docs, function(p) get.msg(paste(spam.path,p,sep="")))
Run Code Online (Sandbox Code Playgroud)
其中get.msg是用户定义的函数,sapply来自base R,而p未在其代码中的任何位置定义.书中没有任何解释,我也尝试搜索并询问我公司的其他R用户无济于事.
任何见解?提前致谢.顺便说一句,在RStudio中,当以这种方式使用时,单词功能是亮蓝色.