Skip to content

Instantly share code, notes, and snippets.

View narath's full-sized avatar

Narath Carlile narath

View GitHub Profile
@narath
narath / faraday_post.rb
Created May 8, 2017 17:24
How to do a x-www-form-urlencoded post with Faraday
require 'faraday'
require 'uri'
data = {
:your_data => "YOUR DATA"
}
url = "some url"
response = Faraday.post(url) do |req|
@narath
narath / login_helper.rb
Created October 28, 2017 22:30
LoginHelper for Basic Auth with Rspec and Rails
module LoginHelper
def login(user="test",pw="test")
request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(user,pw)
end
end
RSpec.configure do |config|
config.include LoginHelper, type: :controller
end
@narath
narath / lib.R
Created May 11, 2018 20:43
R utility methods
# Collected R utility methods
read_all_csv_files <- function(dir) {
save_dir <- getwd()
setwd(dir)
files <- list.files(pattern = "csv")
for (file in files) {
if (!exists("dataset")){
dataset <- read.csv(file)
} else {
@narath
narath / read_all_csv_files.r
Created March 21, 2019 19:22
R code to read all the csv files into a single dataset (only works when all csv files have the same format)
read_all_csv_files <- function(dir) {
save_dir <- getwd()
setwd(dir)
files <- list.files(pattern = "csv")
for (file in files) {
if (!exists("dataset")){
dataset <- read.csv(file)
} else {
temp_dataset <- read.csv(file, stringsAsFactors = FALSE)
dataset <- rbind(dataset, temp_dataset)
@narath
narath / r_keep_only_first_row.md
Created July 29, 2019 20:42
R: keep only the first row for duplicate data

How to keep only the first instance of a row

Derived from this helpful stackoverflow answer

dup_data %>% 
  group_by(MRN) %>%
  filter(Year == min(Year)) %>%
  slice(1) %>%
 ungroup()
@narath
narath / r_create_2x2_table_manually.md
Created July 30, 2019 19:15
R: Create 2x2 table manually
make_2x2 <- function(exp_yes, exp_no, notexp_yes, notexp_no, exposure) {
  as.table(matrix(c(exp_yes, exp_no, notexp_yes, notexp_no), nrow = 2, byrow = TRUE, dimnames = list(c(exposure, paste("No",exposure)), c("Yes","No"))))
}
chisq.test(make_2x2(10,990,100,9900,"Risk"))
### Keybase proof
I hereby claim:
* I am narath on github.
* I am narath (https://keybase.io/narath) on keybase.
* I have a public key ASCV2IYSNSJtU20wXHEULDadD1EPFt21sJp9ovywBkQfJQo
To claim this, I am signing this object:
@narath
narath / Capfile
Last active May 3, 2020 22:23
Digital Ocean Rails Droplet deployment with Rails 6 and Capistrano. See https://dev.narath.io/2020/04/19/deploying-rails-6-using-capistrano-to-a-digital-ocean-rails-droplet.html for instructions.
require "capistrano/setup"
require "capistrano/deploy"
require "capistrano/scm/git"
install_plugin Capistrano::SCM::Git
require "capistrano/rails"
require "capistrano/bundler"
require "capistrano/rvm"
require "capistrano/puma"
@narath
narath / .aptible.yml
Last active June 13, 2020 16:47
Running Rails on Aptible
before_release:
- bundle exec rake db:migrate
# It is better to use date_trunc which is supported in Postgresql
volume.experiences %>%
mutate(monthyear = date_trunc("month",date_of_experience)) %>%
group_by(monthyear) %>%
summarize(count=sum(count)) %>%
ggplot(aes(x=monthyear, y = as.integer(count))) +
geom_area(fill="lightblue", color="black") +
labs(x = "Date", y = "# of experiences")