Skip to content

Instantly share code, notes, and snippets.

View cecilialee's full-sized avatar

Cecilia Lee cecilialee

View GitHub Profile
@cecilialee
cecilialee / showNotification.R
Last active February 9, 2018 00:21
How to add a simple notification. #r #shiny
library(shiny)
shinyApp(
ui = fluidPage(
actionButton("show", "Show")
),
server = function(input, output) {
observeEvent(input$show, {
showNotification(
"Form Submitted",
@cecilialee
cecilialee / morph_api_httr_jsonlite.R
Last active February 9, 2018 00:21
Use morph.io API in R with httr. Then convert the content to a dataframe with jsonlite. Turn it to tibble for easier analysis. #r #httr #jsonlite
library(httr)
library(jsonlite)
resp <- GET("https://api.morph.io/cecilialee/morph-scraper/data.json",
query = list("query" = "select * from 'data'",
"key" = "Kiu/NDU9OrgwJCJ1X/+2")) # fake API
dshk <- content(resp, as = "text")
dshk_df <- fromJSON(dshk)
# library(tidyverse)
@cecilialee
cecilialee / list_to_tibbles.R
Created February 10, 2018 08:46
Convert list to tibbles in R. #r
library(tidyverse)
my_list %>%
do.call(rbind, .) %>%
as.tibble()
# https://sebastiansauer.github.io/convert_list_to_dataframe/
@cecilialee
cecilialee / insert_dynamic_n_ui_for.R
Last active February 10, 2018 10:08
Insert dynamic number of UI in Shiny. #r #shiny
# Using for loop
library(shiny)
ui = basicPage(
numericInput("n", "Number", 1, min = 1),
actionButton("add", "Add UI")
)
server = function(input, output, session) {
@cecilialee
cecilialee / get_gist.R
Last active February 10, 2018 10:08
Get full content of a single gist using GitHub's API in R. #r
# Get a single gist
library(httr)
url <- "https://api.github.com"
user <- "cecilialee"
r <- GET(sprintf("%s/users/%s/gists", url, user))
gists <- content(r)
@cecilialee
cecilialee / inputs_outputs_shiny_brackets.R
Last active February 10, 2018 11:20
Access inputs and output values in Shiny. #r #shiny
# Using double brackets
## Understanding inputs and outputs as a list. Their values are accessable via double brackets.
library(shiny)
ui = basicPage(
textInput("txt", "Text"),
textOutput("ptxt")
)
@cecilialee
cecilialee / actionButton_eventReactive.R
Last active February 12, 2018 09:26
Reactivity of a Shiny actionButton() can be achieved in a few ways. #r #shiny
library(shiny)
ui = basicPage(
numericInput("n", "N:", min = 0, max = 100, value = 50),
actionButton("submit", "Submit"),
textOutput("print")
)
server = function(input, output) {
ntext <- eventReactive(input$submit, {
@cecilialee
cecilialee / compare_python_r.md
Last active February 13, 2018 16:27
Compare common util functions in Python and R. #python #r

Concatenate strings

## py ##
a = 'Data Science'
b = '!'

'{}{}'.format(a, b)
#[1] 'Data Science!'
@cecilialee
cecilialee / scale_x_continuous.R
Last active February 14, 2018 05:27
Scale x continuous in ggplot. #r #tidyverse #ggplot
library(tidyverse)
mtcars %>%
ggplot() +
geom_point(aes(x = gear, y = wt)) +
scale_x_continuous(labels = round, breaks = 3:5)
@cecilialee
cecilialee / module_1.R
Last active February 14, 2018 08:13
Simple module in Shiny. #r #shiny
# UI ==========================================================================
textModalInput = function(id) {
ns <- NS(id)
tagList(
textInput(ns("txt"), "Write something")
)
}