Skip to content

Instantly share code, notes, and snippets.

@leungi
leungi / shiny_updateTextInput_module.R
Created May 13, 2021 22:09 — forked from strboul/shiny_updateTextInput_module.R
Shiny updateTextInput between modules by passing the session object in between
library(shiny)
# MODULES ----------------------------------------------------------------------
text_ui <- function(id) {
ns <- shiny::NS(id)
shiny::tagList(
textInput(ns("text"), "Write here"),
verbatimTextOutput(ns("display"))
)
@leungi
leungi / gist:dc3c4b6d16b7a42386a52f8391f956ee
Created October 22, 2020 22:44 — forked from zerolab/gist:1633661
Convert smart quotes with regular ones (and vice-versa)
<?php
//Quotes: Replace smart double quotes with straight double quotes.
//ANSI version for use with 8-bit regex engines and the Windows code page 1252.
preg_replace('[\x84\x93\x94]', '"', $text);
//Quotes: Replace smart double quotes with straight double quotes.
//Unicode version for use with Unicode regex engines.
preg_replace('[\u201C\u201D\u201E\u201F\u2033\u2036]', '"', $text);
@leungi
leungi / RBERT_demo.R
Created October 1, 2019 23:40
RBERT demo application
# install from https://github.com/jonathanbratt/RBERT
library(RBERT)
# |- Python ----
reticulate::use_condaenv("r-tensorflow-1.11")
reticulate::py_config()
# |- model ----
# path to downloaded BERT checkpoint
BERT_PRETRAINED_DIR <- file.path(
@leungi
leungi / Bayesian_power_analysis.R
Last active July 28, 2019 21:40
Bayesian_power_analysis
# |- table.express ----
library(tidyverse)
library(data.table)
library(brms)
library(broom)
# plot theme
theme_set(theme_dark() +
theme(legend.position = "none",
panel.grid = element_blank(),
@leungi
leungi / table-express_demo.R
Last active July 1, 2019 15:50
Translating data.table to dplyr with table.express
# https://www.johnmackintosh.com/2019-06-30-datatable-by-a-dummy/
# |- install latest version of table.express ----
devtools::install_github('asardaes/table.express')
# |- load library ----
library(dplyr)
library(table.express)
library(data.table)
@leungi
leungi / FindIntersections
Created November 20, 2018 19:43
Finding intersections between 2 lines
# https://stackoverflow.com/questions/20519431/finding-point-of-intersection-in-r
set.seed(1)
x1=rnorm(100,0,1)
x2=rnorm(100,1,1)
# Find points where x1 is above x2.
above<-x1>x2
# Points always intersect when above=TRUE, then FALSE or reverse
intersect.points<-which(diff(above)!=0)
# Find the slopes for each line segment.
@leungi
leungi / spotfire_alert_missing_packages.TERR
Created October 10, 2018 18:03
dashboard-feedback-for-missing-packages-using-inline-terr
# https://datashoptalk.com/dashboard-feedback-for-missing-packages-using-inline-terr/
# In text area, insert Calculated Value control in <p></p> tag
<h3>Installed Packages</h3>
<p>List of missing required packages for this analysis:</P>
<p>
<SpotfireControl id="e96d46e334a9400c86485e41f584ff82" />
</p>
<p><b>Note:</b> Field will show Empty if all packages installed.</p>
@leungi
leungi / rename_nested_tibble_column.R
Created September 11, 2018 14:31
purrr::map2() is known to not function well with dplyr::rename(). Defining lambda in call to map2() solves the problem.https://github.com/tidyverse/purrr/issues/541
library(tidyverse)
data <- tibble::tribble(
~phase, ~fc_dt, ~oil, ~q, ~gas, ~t, ~mean,
"water", "4/1/2017", 4.602535714, 1.70775, 73.55432143, 1L, 10.60021233,
"water", "5/1/2017", 4.414064516, 5.845258065, 48.88796774, 2L, 7.86293583,
"water", "6/1/2017", 3.215866667, 4.149533333, 43.78976667, 3L, 6.47610122
)
nest_data <- data %>% nest(-phase)