Skip to content

Instantly share code, notes, and snippets.

View cecilialee's full-sized avatar

Cecilia Lee cecilialee

View GitHub Profile
@cecilialee
cecilialee / brushing_plots.R
Last active February 24, 2018 07:26
Select rows from plot with brush and hover in Shiny. #r #shiny
# Load packages
library(shiny)
library(ggplot2)
library(tidyverse)
library(DT)
# Load data
load(url("http://s3.amazonaws.com/assets.datacamp.com/production/course_4850/datasets/movies.Rdata"))
# Define UI for application that plots features of movies
@cecilialee
cecilialee / selectize_multiple_app.R
Created February 24, 2018 02:32
Selectize and multiple selectInput in Shiny. #r #shiny
library(shiny)
library(ggplot2)
library(dplyr)
library(DT)
load(url("http://s3.amazonaws.com/assets.datacamp.com/production/course_4850/datasets/movies.Rdata"))
all_studios <- sort(unique(movies$studio))
# UI
ui <- fluidPage(
sidebarLayout(
@cecilialee
cecilialee / dc_ui.R
Created February 23, 2018 09:08
Create a division in ui.R showing one dc chart in dcr. #r #shiny #dcr
library(shiny)
library(dcr)
ui = fluidPage(
chartOutput("plot"),
fluidRow(column(6, dc_ui("chart1", text = "cyl",
show_filter = TRUE,
reset_text = TRUE)),
column(6, dc_ui("chart2", text = "gear by carb",
show_filter = TRUE,
@cecilialee
cecilialee / use_dc_code_dcr.R
Last active February 23, 2018 08:08
Use dc.js code directly in dcr. #r #shiny #dcr
library(dcr)
dcr <- dcr(mtcars)
chart <- dcrchart(type = "rowChart", id = "chart1", dimension = "cyl",
reduce = reduceCount(), width = 400, height = 250,
colors = dc_code("d3.scale.category10()"))
dcr + chart
@cecilialee
cecilialee / dbDisconnect_onSessionEnded.R
Last active February 23, 2018 07:47
Disconnect database on session ended in Shiny. #r #shiny #dbi
library(shiny)
library(DBI)
library(RPostgreSQL)
ui = basicPage(
)
server = function(input, output, session) {
con <- dbConnect(PostgreSQL(),
dbname = "myDB",
@cecilialee
cecilialee / create_circle.js
Last active February 22, 2018 23:41
Create a circle in D3. #javascript #d3
// create y in linear scale
var y = d3.scaleLinear().domain([15, 90]).range([250, 0]);
// create x in log scale
var x = d3.scaleLog().domain([250, 100000]).range([0, 600]);
// create r in radius scale
var r = d3.scaleSqrt().domain([52070, 1380000000]).range([10, 50]);
// append circle and attr to svg
@cecilialee
cecilialee / load_every_file.R
Created February 22, 2018 10:41
Load every file in a folder in Shiny. #r #shiny
library(shiny)
for (file in list.files("modules", full.names = TRUE)) source(file)
@cecilialee
cecilialee / select_today.sql
Created February 22, 2018 07:38
Select where date is today in SQL. #sql
select *
from my_status
where date(enter_time) = date(now());
@cecilialee
cecilialee / return_module.R
Created February 20, 2018 10:28
Store the returned values from a module in reactiveValues in Shiny. #r #shiny
# UI ==========================================================================
returnUI = function(id) {
ns <- NS(id)
tagList(
textInput(ns("txt"), "Write something")
)
}
@cecilialee
cecilialee / input_module.R
Last active February 20, 2018 10:19
Return reactiveValues in Shiny module. #r #shiny
inputModuleUI <- function(id){
ns <- NS(id)
wellPanel(textInput(ns('text1'), "First text"),
textInput(ns('text2'), "Second text"))
}
inputModule <- function(input, output, session){
vals <- reactiveValues()
observe({vals$text1 <- input$text1})
observe({vals$text2 <- input$text2})