R语言 SQL RODBC包笔记
Posted 基督徒Isaac
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了R语言 SQL RODBC包笔记相关的知识,希望对你有一定的参考价值。
library(RODBC)
library(tidyverse)
myconn <-odbcConnect("master", uid="sa", pwd="***")
sqlTables(myconn) %>% tibble()
a <- sqlFetch(myconn, "USArrests");a %>% tibble()
b <- sqlQuery(channel = myconn,
query = "SELECT * FROM USArrests");b %>% tibble()
table(a == b)
c <- data.frame("Anci", 15, 100, 50, 12); c %>% tibble()
colnames(c) <- colnames(a); c %>% tibble()
sqlSave(myconn, c, "USArrests", append = T, rownames = FALSE)
sqlFetch(myconn, "USArrests") %>% tibble()
问题1: sqlSave表格增加行数据,导致系统崩溃
解答1:将参数 rownames = FALSE 添加到sqlSave
参考文献:
RODBC sqlSave崩溃R前端 - 堆栈内存溢出
https://stackoom.com/question/1vWY2
R包系列——RODBC包教程 - 知乎
https://zhuanlan.zhihu.com/p/32328584
library(RODBC)
library(tidyverse)
myconn <-odbcConnect(
"***",
uid = "***",
pwd = "***"
)
# 查看工作表
sqlTables(myconn) %>% colnames()
sqlTables(myconn)$TABLE_NAME %>% data.frame(TABLE_NAME = .) %>% View()
# 查看横剪
tblHJOps <- sqlQuery(channel = myconn,
query =
"SELECT * FROM tblHJOps"
)
tblHJBabyOps <- sqlQuery(channel = myconn,
query =
"SELECT * FROM tblHJBabyOps"
)
tblHJOps %>% write.csv("e:tblHJOps.csv", row.names = F, na = "")
tblHJBabyOps %>% write.csv("e:tblHJBabyOps.csv", row.names = F, na = "")
更新
---
title: "1号横剪2022年1.1-2.25生产状况"
output: html_document
---
```r setup, include = FALSE
knitr::opts_chunk$set(echo = FALSE, message = FALSE)
# include = FALSE 隐藏整个代码段的输出 只对本段有效
# echo = FALSE、message = FALSE 隐藏源代码、警告文本
# 后面的代码段,都会默认使用knitr::opts_chunk$set设置的选项
library(RODBC)
library(tidyverse)
library(lubridate) # 转化时区 force_tz() tzone='asia/shanghai'
library(anytime) # 获取日期 anydate() 时区CST
library(hms) # 获取时间 :as_hms()
myconn <-odbcConnect(
"HBS***",
uid = "HfM***",
pwd = "***r"
)
jitai <- sqlFetch(
channel = myconn,
sqtable = "tblHJOps"
)
yiyue <- jitai %>% filter(StartTime >= anytime::anytime("2022-1-1 07:45:00"))
baiye <-
function(x = lubridate::ymd_hms("2021.12.31 0:0:0"))
origin = hms::as_hms("07:45:00")
TimeStamp = c()
for (i in 1:NROW(x))
StartTime = lubridate::force_tz(x[i], tzone='asia/shanghai')
date = anytime::anydate(StartTime)
time = hms::as_hms(StartTime)
hour = (time - origin) / 3600
if (hour >= 0 & hour < 12)
stamp = hms::as_hms("07:45:00")
TimeStamp[i] = paste(date, stamp); TimeStamp[i]
else if (hour >= 12)
stamp = hms::as_hms("19:45:00")
TimeStamp[i] = paste(date, stamp); TimeStamp[i]
else if (hour < 0)
stamp = hms::as_hms("19:45:00")
TimeStamp[i] = paste(date - 1 , stamp); TimeStamp[i]
else
stamp = ""
TimeStamp[i] = ""
# 字符向量,需转化为时间
y = lubridate::force_tz(lubridate::ymd_hms(TimeStamp),
tzone='asia/shanghai')
return(y)
yiyue <- yiyue %>% mutate(
TimeStamp = StartTime %>% baiye()
)
panduan <-
function(X = lubridate::force_tz(lubridate::ymd_hms("2021.12.31 7:45:0"),
tzone='asia/shanghai'))
a = lubridate::force_tz(lubridate::ymd_hms("2021.12.31 7:45:0"),
tzone='asia/shanghai')
b = 1/2
d = c()
for (i in 1:NROW(X))
x = X[i]
e = as.numeric((x - a) / b) %% 12
if (e %in% c(0,2,5,7))
d[i] = "甲"
else if (e %in% c(8,10,1,3))
d[i] = "乙"
else if (e %in% c(4,6,9,11))
d[i] = "丙"
else
d[i] = ""
return(d)
yiyue <- yiyue %>% mutate(
banzu = TimeStamp %>% panduan()
)
baiye1 <- function(x = hms::as_hms("07:45:0"))
y = c()
for (i in 1:NROW(x))
a = hms::as_hms(x[i])
if (a == hms::as_hms("07:45:0"))
y[i] = "白班"
else if (a == hms::as_hms("19:45:0"))
y[i] = "夜班"
else
y[i] = ""
return(y)
yiyue <- yiyue %>% mutate(
banci = TimeStamp %>% baiye1()
)
日产量
chanliang <- yiyue %>%
group_by(TimeStamp, banci) %>%
summarise(ExitWeight = sum(ExitWeight) / 1000, .groups = "drop_last") %>%
mutate(TimeStamp = TimeStamp %>% anydate())
chanliang %>% ggplot(aes(
x = TimeStamp, y = ExitWeight,
group = banci,
colour = banci,
linetype = banci,
shape = banci
)) +
geom_line() + geom_point() +
theme_bw() +
labs(x = "日期", y = "产量(吨)") +
theme(legend.title = element_blank()) +
geom_hline(aes(yintercept = ExitWeight %>% mean()), colour = 3)
月产量
周产量
班组产量
tianshu <- yiyue %>%
group_by(banzu) %>%
summarise(days = TimeStamp %>% anydate() %>% n_distinct()) %>%
mutate(banzu = banzu %>% factor(levels = c("甲","乙","丙"))) %>%
arrange(banzu)
chanliang <- yiyue %>%
group_by(banzu) %>%
summarise(ExitWeight = sum(ExitWeight) / 1000) %>%
mutate(banzu = banzu %>% factor(levels = c("甲","乙","丙"))) %>%
arrange(banzu) %>%
mutate(xiaolv = (ExitWeight / tianshu[,2]) %>% unlist(use.names = FALSE))
chanliang %>% ggplot(aes(
x = banzu %>% factor(levels = c("甲","乙","丙")),
y = xiaolv * 30,
group = 1,
colour = banzu %>% factor(levels = c("甲","乙","丙")),
fill = banzu %>% factor(levels = c("甲","乙","丙")),
shape = banzu %>% factor(levels = c("甲","乙","丙"))
)) +
geom_line(size = 1) + geom_point(size = 3) +
geom_col(aes(y = ExitWeight), alpha = 0.1) +
theme_bw() +
theme(legend.position = 'none') +
labs(x = "班组", y = "产量(吨)") +
geom_text(aes(label = xiaolv %>% round(1)), vjust = 2)
水冷板
缺陷
以上是关于R语言 SQL RODBC包笔记的主要内容,如果未能解决你的问题,请参考以下文章
R语言︱ 数据库SQL-R连接与SQL语句执行(RODBCsqldf包)
R语言之安装RODBC错误信息configure: error: "ODBC headers sql.hand sqlext.h not found"