Skip to content

Instantly share code, notes, and snippets.

@geosmart
Created September 30, 2021 07:08
Show Gist options
  • Save geosmart/fdda8e576204ad157c367bf533b56110 to your computer and use it in GitHub Desktop.
Save geosmart/fdda8e576204ad157c367bf533b56110 to your computer and use it in GitHub Desktop.
r read write csv by args
library(optparse)
# Rscript test.r --in.csv1 data/mock.csv --in.csv2 data/mock.csv --out.csv1 data/out.csv
# read param
option_list <- list(
make_option(c("-i", "--in.csv1"), type = "character", default = "", action = "store", help = "This is first!"),
make_option(c("-f", "--in.csv2"), type = "character", default = "", action = "store", help = "This is first!"),
make_option(c("-t", "--out.csv1"), type = "character", default = "", action = "store", help = "This is first!")
)
args = parse_args(OptionParser(option_list = option_list, usage = "This Script is a test for arguments!"))
print(args$in.csv1)
# test
args$in.csv1 = "data/mock.csv"
args$in.csv2 = "data/out.csv"
# read input
df <- read.csv(args$in.csv1)
head(df)
# # write out put
write.csv(df, file = args$in.csv2, row.names = F, fileEncoding = "UTF-8", quote = FALSE)
# write list to csv
list_data <- list(
c("3-D", "4-F", "4-H'er", "4-H", "A battery", "a bon march"),
c("AN", "N", "N", "A", "h", "v")
)
names(list_data) <- c("word", "pos")
# do your convert
df_data <- as.data.frame(lapply(list_data,unlist))
write.csv(df_data, file = args$in.csv2, row.names = F, fileEncoding = "UTF-8", quote = FALSE)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment