#' Function to import and concatenate datasets having the same format.
#' @param x Tibble for csv, excel, and text; Character vector for google.
#' @param spreadsheet Character string. For excel and google, specify the name of the spreadsheet to be imported.
#' @param filetype Character string. specify whether "", "", "", or "" are imported.
#' @return A tibble contaning the name of the source and all the related segment(s).
#' @importFrom tibble as_tibble
#' @importFrom tibble tibble
#' @importFrom tibble rownames_to_column
#' @importFrom purrr map
#' @importFrom purrr safely
#' @importFrom stringr str_sub
#' @importFrom dplyr mutate
#' @importFrom dplyr select
#' @importFrom dplyr filter
#' @importFrom dplyr mutate_all
#' @importFrom tidyr unnest
#' @importFrom RefManageR ReadBib
#' @importFrom googlesheets gs_title
#' @importFrom googlesheets gs_read_csv
#' @importFrom readxl read_excel
#' @importFrom utils read.delim
#' @importFrom readr read_csv
#' @export
datexp_import <- function(x,
spreadsheet = 1,
filetype) {
# Bind variables
name <- NULL
datapath <- NULL
extension <- NULL
data <- NULL
notimported <- NULL
V1 <- NULL
line <- NULL
if(length(x)>1 & filetype != "google") x <- as_tibble(x) else {
x <- tibble(name = x, datapath = x)
}
if (filetype == "csv") {
safe_read <- safely(read_csv, quiet = T)
import <- x %>%
select(name, datapath) %>%
mutate(extension = str_sub(datapath, -4)) %>%
filter(extension == ".csv") %>%
select(-extension) %>%
mutate(data = map(datapath, safe_read)) %>%
mutate(data = map(data, function(x) mutate_all(x$result, as.character))) %>%
mutate(notimported = map(data, is.null)) %>%
filter(notimported == FALSE) %>%
select(-notimported) %>%
unnest()
}
if (filetype == "excel") {
safe_read <- safely(read_excel, quiet = T)
import <- x %>%
select(name, datapath) %>%
mutate(extension = str_sub(datapath, -5)) %>%
filter(extension == ".xlsx") %>%
select(-extension) %>%
mutate(data = map(datapath, safe_read, sheet = spreadsheet)) %>%
mutate(data = map(data, function(x) mutate_all(x$result, as.character))) %>%
mutate(notimported = map(data, is.null)) %>%
filter(notimported == FALSE) %>%
select(-notimported) %>%
unnest()
}
if (filetype == "google") {
safe_title <- safely(gs_title, quiet = T)
safe_read <- safely(gs_read_csv, quiet = T)
import <- x %>%
mutate(data = map(datapath, safe_title)) %>%
mutate(data = map(data, function(x) x$result)) %>%
mutate(notimported = map(data, is.null)) %>%
filter(notimported == FALSE) %>%
select(-notimported) %>%
mutate(data = map(data, safe_read, ws = spreadsheet, col_types = cols(.default = "c"))) %>%
mutate(data = map(data, function(x) mutate_all(x$result, as.character))) %>%
mutate(notimported = map(data, is.null)) %>%
filter(notimported == FALSE) %>%
select(-notimported) %>%
unnest()
}
if (filetype == "bibtex") {
read <- function(x) {
ReadBib(x) %>%
as.data.frame() %>%
rownames_to_column("refid") %>%
as_tibble()
}
safe_read <- safely(read, quiet = T)
import <- x %>%
select(name, datapath) %>%
mutate(extension = str_sub(datapath, -4)) %>%
filter(extension == ".bib") %>%
select(-extension) %>%
mutate(data = map(datapath, safe_read)) %>%
mutate(data = map(data, function(x) mutate_all(x$result, as.character))) %>%
mutate(notimported = map(data, is.null)) %>%
filter(notimported == FALSE) %>%
select(-notimported) %>%
unnest()
}
if (filetype == "text") {
options(warn = -1)
safe_read <- safely(read.delim, quiet = T)
import <- x %>%
select(name, datapath) %>%
mutate(extension = str_sub(datapath, -4)) %>%
filter(extension == ".txt") %>%
select(-extension) %>%
mutate(data = map(datapath, safe_read, header = F)) %>%
mutate(data = map(data, function(x) mutate_all(x$result, as.character))) %>%
mutate(notimported = map(data, is.null)) %>%
filter(notimported == FALSE) %>%
select(-notimported) %>%
mutate(data = map(data, function(x)
x %>% rownames_to_column("line") %>%
mutate(V1 = as.character(x$V1)))
) %>%
unnest() %>%
select(source = name, line, segments = V1)
}
import
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.