Skip to content

Instantly share code, notes, and snippets.

View benjaminguinaudeau's full-sized avatar

Benjamin Guinaudeau benjaminguinaudeau

View GitHub Profile
library(dplyr)
do_if <- function(.data, condition, call){
if(condition){
.x <- .data
call_str <- call %>%
as.character %>%
.[2]
@benjaminguinaudeau
benjaminguinaudeau / init.R
Last active August 14, 2019 16:37
This gist installs all the R-packages I use. This is most particularly useful when a fresh installation of R is required.
# The line below corresponds to all packages, I use at least twice in my documents
# This list has been created using pkguse (remotes::install_github("mkearney/pkguse"))
package <- c("base64enc", "blogdown", "corrr",
"countrycode", "cowplot", "devtools",
"dplyr", "dummies", "fs", "furrr",
"future", "ggcorrplot", "ggplot2", "glue",
"janitor", "kableExtra", "keras", "keyring",
"knitr", "lubridate", "magrittr", "numbers",
"pacman", "purrr", "R.utils", "R6", "readr",
"readxl", "reticulate", "rio", "rlang", "rmarkdown",
@benjaminguinaudeau
benjaminguinaudeau / try_n.R
Created August 22, 2019 14:51
This function does the same as try, but instead of trying only once, it will try the number of times specified by the parameter n.
try_n <- function(x, n = 2, .otherwise = "", quiet = T){
index <- 1
out <- try(1+a, silent = T)
while(index <= n & class(out) == "try-error"){
if(!quiet){message(glue::glue("Trial {index}"))}
out <- suppressWarnings(try(x, silent = T))
index <- index + 1
}
@benjaminguinaudeau
benjaminguinaudeau / tidy_pglm.R
Created June 26, 2020 15:43
tiidy pglm estimate and get simulations
tidy_pglm <- function(fit){
summary(fit)$estimate %>%
tibble::as_tibble() %>%
dplyr::mutate(param = rownames(summary(fit)$estimate)) %>%
dplyr::select(param, dplyr::everything()) %>%
janitor::clean_names(.)
}
predict_glm <- function(fit, new_data, form ){
import asyncio
import pyppeteer
class poker:
def __init__(self, endpoint):
self.endpoint = endpoint
loop = asyncio.new_event_loop()
loop.run_until_complete(self.start())
@benjaminguinaudeau
benjaminguinaudeau / send_mail_uni_konstanz.py
Last active August 18, 2020 09:36
Send email with uni konstanz account
#!/usr/bin/env python
import smtplib
from email.mime.text import MIMEText
def send_mail_starttls(sender = '', to = '', pwd = '', subject = '', msg = ''):
recipients = [to, sender]
msg = MIMEText(msg)
msg['Subject'] = subject
@benjaminguinaudeau
benjaminguinaudeau / ccs_ger_2017.R
Created August 31, 2020 08:36
This gist provides a dictionnary to match constituencies for Comparative Candidate Survey data for Germany 2017
ccs_const_dict <- tibble::tibble(
index = c(1, 10, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 11,
110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 12, 120, 121,
122, 123, 124, 125, 126, 127, 128, 129, 13, 130, 131, 132, 133,
134, 135, 136, 137, 138, 139, 14, 140, 141, 142, 143, 144, 145,
146, 147, 148, 149, 15, 150, 151, 152, 153, 154, 155, 156, 157,
158, 159, 16, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169,
17, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 18, 180,
181, 182, 183, 184, 185, 186, 187, 188, 189, 19, 190, 191, 192,
193, 194, 195, 196, 197, 198, 199, 2, 20, 200, 201, 202, 203,
@benjaminguinaudeau
benjaminguinaudeau / get_accessible_videos.R
Last active September 1, 2020 10:21
A function to get around the youtube api. Scrape number of videos for each account ( up to 20000) without any API access
# video_counts <- c("UCtahKSp0CdvDv8CMS7RXsiQ", "UC3XTzVzaHQEd30rQbuvCtTQ", "UC-i2qb4sL10OdR6PyMEU_5Q",
# "UCaXkIU1QidjPwiAYu6GcHjg", "UCn8zNIfYAQNdrFRrr8oibKw") %>%
# purrr::map_dfr(get_accessible_videos)
get_accessible_videos <- function(channel_id){
data <- glue::glue("https://www.youtube.com/channel/{channel_id}/videos") %>%
xml2::read_html() %>%
rvest::html_nodes("script") %>%
pacman::p_load(tidyverse)
source("https://gist.githubusercontent.com/benjaminguinaudeau/2db5647ac4691559ed0f5af6d67166ce/raw/72dd86762962fc6391df3a94c267e7a48ffb5712/get_accessible_videos.R")
reconnect <- function(){
file <- ""
trig <- try(stop(), silent = T)
cli::cli_h1("[ {Sys.time()} ] Reconnecting")
@benjaminguinaudeau
benjaminguinaudeau / get_proc.R
Created November 13, 2020 00:57
Retrieve current running process on linux/mac os
get_proc <- function(){
proc <- system("ps -aux", intern = T)
proc[1] %>% str_extract(".{12}")
end_first_column <- proc %>%
map_dbl(~str_length(str_extract(.x, "[^\\s]+"))) %>%
max