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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章