Changes
Page history
Update R
authored
May 15, 2023
by
Miki, Hiromitsu
Show whitespace changes
Inline
Side-by-side
R.md
View page @
4725bac0
# R
## Setup
### Install
...
...
@@ -13,8 +11,6 @@
ライブラリは
`%UserProfile%\AppData\Local\R\win-library\4.2`
にインストールされる。使用頻度の高い、以下のライブラリをインストールしておく。
*
`install.packages("data.table")`
*
`install.packages("reticulate")`
*
`install.packages("tidyverse")`
## Snippets
...
...
@@ -24,52 +20,43 @@
#### Make DataFrame (tibble) from text file exported by DLT Viewer
```
r
library
(
data.table
)
library
(
tidyverse
)
DLT_TXT_COLS
<-
c
(
"index"
,
"time"
,
"tick"
,
"count"
,
"ecuid"
,
"appid"
,
"ctxid"
,
"sessionid"
,
"type"
,
"subtype"
,
"mode"
,
"nargs"
,
"msg"
)
DLT_TXT_PATTERN
<-
r
"(^(\d+) (\d{4}/\d{2}/\d{2} \d{2}:\d{2}:\d{2}\.\d{6}) (\d+\.\d{4}) (\d+) (.+) (.+) (.+) (\d+) (.+) (.+) (verbose|non-verbose) (\d+) (.+)$)"
#' Make
data.ta
ble from DLT text file.
#' Make
tib
ble from DLT text file.
#' @param path A path to DLT file or directory which has DLT files
#' All files on the path will be read if path is directory.
#' @param as_list Used if directory path is set to `path`
#' Set TRUE if list grouped by file will be returned
read_dlt_txt
<-
function
(
path
)
{
if
(
dir.exists
(
path
))
{
pathlist
<-
list.files
(
path
,
full.names
=
TRUE
)
# Remove directories from pathlist
pathlist
<-
pathlist
[
!
dir.exists
(
pathlist
)]
return
(
purrr
::
map_dfr
(
pathlist
,
read_dlt_txt
)
)
files
<-
pathlist
[
!
dir.exists
(
pathlist
)]
purrr
::
map_dfr
(
files
,
read_dlt_txt
)
}
else
if
(
file.exists
(
path
))
{
df
<-
.read_base_txt
(
path
)
%>%
extract
(
value
,
DLT_TXT_COLS
,
DLT_TXT_PATTERN
)
df
[,
index
:=
as.integer
(
index
)]
df
[,
time
:=
as.POSIXct
(
time
)]
df
[,
tick
:=
as.numeric
(
tick
)]
df
[,
count
:=
as.integer
(
count
)]
df
[,
sessionid
:=
as.integer
(
sessionid
)]
df
[,
nargs
:=
as.integer
(
nargs
)]
return
(
df
)
.read_base_txt
(
path
)
%>%
extract
(
value
,
DLT_TXT_COLS
,
DLT_TXT_PATTERN
)
%>%
mutate
(
index
=
as.integer
(
index
))
%>%
mutate
(
time
=
as.POSIXct
(
time
))
%>%
mutate
(
tick
=
as.numeric
(
tick
))
%>%
mutate
(
count
=
as.integer
(
count
))
%>%
mutate
(
sessionid
=
as.integer
(
sessionid
))
%>%
mutate
(
nargs
=
as.integer
(
nargs
))
}
}
#' Make
data.ta
ble from text file.
#' Make
tib
ble from text file.
#' @param path A path to text file
.read_base_txt
<-
function
(
path
)
{
df
<-
fread
(
path
,
sep
=
""
,
header
=
F
,
col.names
=
c
(
"value"
),
encoding
=
"UTF-8"
)
df
[,
line
:=
1
:
.N
]
df
[,
file
:=
basename
(
path
)]
setcolorder
(
df
,
neworder
=
c
(
"file"
,
"line"
,
"value"
))
return
(
df
)
read_lines
(
path
)
%>%
as_tibble
()
%>%
mutate
(
line
=
row_number
(),
.before
=
value
)
%>%
mutate
(
file
=
basename
(
path
),
.before
=
value
)
}
# Make DF from txt exported by DLT Viewer
df
<-
read_dlt_txt
(
"filepath.dlt.txt"
)
```
...
...
...
...