R语言工程化实践经验
前言
R语言是一门很好入门的语言,但用在生产环境下,如果没有规范的项目结构以及相应的开发流程,后项目的开发效率以及可维护性都会大打折扣。本文从笔者实践角度给大家分享一下。
相信大家一般在写R代码的时候,是这样写的。
library(package1)
library(package2)
library(...)
#### dababase connect ####
database_con1 <- dbConnect(SQLServer(),host="localhost", port=3306,dbname="databasename1",user="username1",password="password1")
database_con2 <- dbConnect(MySQL(),host="localhost", port=3306,dbname="databasename1",user="username1",password="password1")
...
#### import data ####
data1 <- dbGetQuery(database_con1, "select * from data1")
data2 <- dbGetQuery(database_con2, "select * from data2")
...
#### data processing ####
data1_result <- data1 %>%
mutate(...) %>%
group_by(...) %>%
summarise(...)
data2_result <- data2 %>%
mutate(...) %>%
group_by(...) %>%
summarise(...) %>%
arrange(...)
#### data output ####
dbWriteTable(database_con1,
"data1_table",
data1_result,
append = TRUE,
row.names = FALSE)
dbWriteTable(database_con2,
"data2_table",
data2_result,
append = TRUE,
row.names = FALSE)
相关文章