Skip to content

Instantly share code, notes, and snippets.

@stla
Last active August 6, 2022 08:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stla/7d4face56a7d298df0b593225f83a70b to your computer and use it in GitHub Desktop.
Save stla/7d4face56a7d298df0b593225f83a70b to your computer and use it in GitHub Desktop.
Python utils in R
setwd("C:/SL/Python/RPythonUtils")
library(clipr)
list2list <- function(L, outfile = "list.txt") {
singletons_idxs <- which(lapply(L, length) == 1)
singletons <- L[singletons_idxs]
L <- replace(L, singletons_idxs, sprintf("c(%s)", singletons))
write_clip(L, breaks = "],\n", sep = ", ")
cat("[",
sub("\\)", "]\n]", gsub("\\)]", "]", gsub("c\\(", "[", read_clip()))),
sep = "\n", file = outfile
)
}
setwd("C:/SL/Python/RPythonUtils")
library(clipr)
library(magrittr)
matrix2list <- function(M, outfile = "haskell.txt") {
rownames(M) <- colnames(M) <- NULL
write_clip(M, sep = ", ")
clipboard <- c(
sprintf("%s],", head(read_clip(), -1)),
sprintf("%s]", tail(read_clip(), 1))
)
paste0(c("[", paste0(" [", clipboard))) %>%
cat(sep = "\n", file = outfile) %>%
cat("]", sep = "", file = outfile, append = TRUE)
}
locklines <- readLines("poetry.lock")
Lines <- grep("^\\[\\[package", locklines)
pkgs <- locklines[Lines + 1L]
pkgs <- vapply(strsplit(pkgs, "\""), `[`, character(1L), 2L)
versions <- locklines[Lines + 2L]
versions <- vapply(strsplit(versions, "\""), `[`, character(1L), 2L)
paste0(
"poetry run python -m pip install ",
gsub(
"-",
"_",
paste0(sprintf("%s==%s", pkgs, versions), collapse = " ")
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment