Skip to content

Instantly share code, notes, and snippets.

View aagarw30's full-sized avatar

Abhinav Agrawal aagarw30

View GitHub Profile
@aagarw30
aagarw30 / server.r
Last active January 10, 2016 16:55
renderUI with GGPLOT
library(shiny) # load the shiny package
library(ggplot2) # load the gglpot2 package if ploting using ggplot
# beginning of the shiny server function
shinyServer(function(output, input)({
# The following reactive function would return the column variable names corresponding to the dataset selected by the user.
var <- reactive({
switch(input$data1,
"mtcars" = names(mtcars),
@aagarw30
aagarw30 / avgbymutate.r
Created January 11, 2016 15:09
Mutate demo example code snippet
# Demonstrate how to use mutate to add a new variable (called average in this demo example) into dataframe which contains the results of a function (average or mean in this demo example) performed on each row of the specified variables in the dataframe.
library(dplyr)
# create a dummy dataframe for demostration purpose
mydata = data.frame(x=c("99.15%", "98.75%", "99.52%"),y=c("96.25%", "91.55%", NA), z=c("90.10%", NA, "87.50%"))
# look at the data
str(mydata)
@aagarw30
aagarw30 / mutate1.Rmd
Last active January 11, 2016 15:40
Mutate demo example#1
---
title: "Mutate demo example#1"
author: "Abhinav Agrawal"
date: "January 11, 2016"
output: html_document
---
```{r}
@aagarw30
aagarw30 / server.r
Last active January 15, 2016 16:27
R Shiny and Leaflet # 1 - Demo leaflet() function code snippet
library(shiny)
library(leaflet)
shinyServer(function(input, output) {
output$mymap <- renderLeaflet({
# define the leaflet map object
leaflet() # this will just define the map widget
})
})
@aagarw30
aagarw30 / server.r
Created January 15, 2016 16:30
R Shiny and Leaflet # 2 - Demo addTiles() function code snippet
library(shiny)
library(leaflet)
shinyServer(function(input, output) {
output$mymap <- renderLeaflet({
# define the leaflet map object
leaflet() %>%
addTiles() # this adds the base map tile - OSM (by default)
})
@aagarw30
aagarw30 / server.r
Last active January 23, 2016 08:04
R Shiny and Leaflet # 4 - A simple map which demos setView() and addMarkers() functions and mapping "Taj Mahal, Agra, India"
library(shiny)
library(leaflet)
## renderLeaflet() is used at server side to render the leaflet map
shinyServer(function(input, output) {
output$mymap <- renderLeaflet({
# define the leaflet map object
leaflet() %>%
addTiles() %>%
@aagarw30
aagarw30 / server.r
Last active January 23, 2016 08:05
R Shiny and Leaflet # 6 - Mapping my birth location - Demo addPopups() with minimal reproducible example
library(shiny)
library(leaflet)
shinyServer(function(input, output) {
output$mymap <- renderLeaflet({
# define the leaflet map object
leaflet() %>%
addTiles() %>%
setView(lng = 82.9739144,lat = 25.3176452, zoom = 2) %>%
# Add a pop up to map by using addPopups() function
addPopups(lng = 82.9739144, lat = 25.3176452, popup = "Varanasi - My Birth Place")
@aagarw30
aagarw30 / server.r
Last active January 23, 2016 08:05
R Shiny and Leaflet # 5 - Mapping my birth location - Demo addMarkers() with minimal reproducible example
library(shiny)
library(leaflet)
# we use the renderLeaflet() at the server side for leaflet maps.
shinyServer(function(input, output) {
output$mymap <- renderLeaflet({
# define the leaflet map object
leaflet() %>%
library(shiny)
library(data.table)
# use the below options code if you wish to increase the file input limit,
# in this example file input limit is increased from 5MB to 9MB
options(shiny.maxRequestSize = 700*1024^2)
shinyServer(function(input,output){
# This reactive function will take the inputs from UI.R and use them for read.table() to read the data from the file. It returns the dataset in the form of a dataframe.
# file$datapath -> gives the path of the file
@aagarw30
aagarw30 / server.r
Last active February 7, 2016 17:04
R Shiny and Leaflet # 3 - Demo of different types of base map tiles
library(shiny)
library(leaflet)
shinyServer(function(input, output) {
output$mymap <- renderLeaflet({
input$update # catching the action button event
isolate(leaflet() %>%
addProviderTiles(input$bmap))