Skip to content

Instantly share code, notes, and snippets.

library(dplyr)
library(magrittr)
set.seed(1237)
set_df <- function() {
data.frame(MRN = sample(1:5, 10, replace = TRUE), source = sample(letters[1:20], 10), value = sample(1:10, 10))
}
df1 <- set_df()
df2 <- set_df()
@jaehyeon-kim
jaehyeon-kim / html_example.html
Last active July 14, 2016 00:42
adding gist to WordPress
<head>
<title>meteor-poc</title>
</head>
<body>
<div class="container-fluid">
<h2>Old Faithful Geyser Data</h2>
<div class="row">
<div class="col-sm-4">
<div class="well">
if(!require(shiny)) install.packages("shiny")
if(!require(shinythemes)) install.packages("shinythemes")
library(shiny)
library(shinythemes)
source("ui.R")
source("server.R")
shinyApp(ui, server)
@jaehyeon-kim
jaehyeon-kim / ui.R
Created April 14, 2016 23:13
Shiny login trial
ui_login <- function() {
fluidRow(
tags$head(tags$style(HTML(".container-fluid {margin: 25px;}"))),
column(2, offset = 5,
div(id = "login_div",
wellPanel(
h4("LOGIN"),
textInput("username", "User name"),
passwordInput("password", "Password"),
br(),
hospay <- data.frame(foo = "abc", Payment = c("$1,000","$200"), Lower.estimate = "$100", Higher.estimate = "$100", stringsAsFactors = FALSE)
cols <- grep("Payment|*estimate", names(hospay))
hospay[,cols][] <- lapply(hospay[,cols], function(x) as.numeric(gsub("[$,]", "", x)))
str(hospay)
#'data.frame': 2 obs. of 4 variables:
#$ foo : chr "abc" "abc"
#$ Payment : num 1000 200
#$ Lower.estimate : num 100 100
#$ Higher.estimate: num 100 100
sudo apt-get update
sudo apt-get install openssh-server
sudo ufw allow 22
### S3 Class
## setting up independent classes and methods
# setting up line by line
die <- list(trial = character(0))
class(die) <- "Die"
coin <- list(trial = character(0))
class(coin) <- "Coin"
# setting up by constructors
### S3 Class
## set up classes and methods
die <- list(trials = character(0))
class(die) <- "Die"
coin <- list(trials = character(0))
class(coin) <- "Coin"
reset <- function(obj) {
UseMethod("reset", obj)
### Subsetting applications
## lookup tables (character subsetting)
x <- c("m","f","u","f","f","m","m")
lookup <- c(m = "Male", f = "Female", u = NA)
lookup[x]
unname(lookup[x])
## matching and merging by hand (integer subsetting)
grades <- c(1, 2, 2, 3, 1)
info <- data.frame(grade = 3:1, desc = c("Excellent", "Good", "Poor"), fail = c(F, F, T))
## linear regression
ols <- lm(speed ~ dist, data = cars)
olsCoef <- ols$coefficients
# prediction is made by X * beta or beta_0 + beta_1*x_1+ ... + beta_k*x_k
olsX <- model.matrix(speed ~ dist, data = cars)
olsPred <- olsX %*% olsCoef
as.vector(head(olsPred))
[1] 8.615041 9.939581 8.946176 11.926392 10.932987 9.939581