R语言dplyr包连接数据库

1、根据dplyr的文档,从dplyr 0.6.0开始,许多databse和sql函数从dplyr移动到dbplyr。
如果要在数据库后端使用dplyr,请运行install.packages(“dbplyr”)。
https://stackoverflow.com/questions/45840462/error-the-dbplyr-package-is-required-to-communicate-with-database-backends

2、https://blog.csdn.net/xmuecor/article/details/78477078
this is Introduction to dbplyr
注意:这个博客是重点

Getting started

To use databases with dplyr you need to first install dbplyr:

install.packages("dbplyr")

You’ll also need to install a DBI backend package. The DBI package provides a common interface that allows dplyr to work with many different databases using the same code. DBI is automatically installed with dbplyr, but you need to install a specific backend for the database that you want to connect to.

翻译:要用dplyr先安装dbplyr。
您还需要安装一个DBI后端包。dbi包提供了一个公共接口,允许dplyr使用相同的代码处理许多不同的数据库。DBI与DBPLYR一起自动安装,但您需要为要连接的数据库安装
特定的后端。

注意:在安装dbplyr时,自动安装DBI,所以只需要额外安装RMySQL即可

install.packages("RMySQL")

以下为两种实例:(存在一个数据库dbname = “mkfriends”,里面有表img表,存在imgid和userid列)

#连接数据库
library(DBI)
library(RMySQL)
conn <- dbConnect(MySQL(), dbname = "mkfriends", username="root", password="root", host="127.0.0.1", port=3306)
dbReadTable(conn, "img")  #读表

library(dplyr)
my_db <- src_mysql ( "mkfriends",host = "localhost" ,port =3306, user = "root" , password = "root" )
my_tbl <- tbl ( my_db , "img" )
#可以直接使用dplyr语法对数据库转换的tbl对象进行操作。
select(my_tbl,imgid,userid)

其它:
https://blog.csdn.net/yjz_sdau/article/details/51510323

你可能感兴趣的:(学术工具,dplyr)