-
-
Save ramnathv/4728031 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Here a set of instructions corresponding to each code lesson is made into a dataset codelist | |
ID=as.numeric() # id of code lesson | |
instruct=as.character() # instructions for code lesson | |
subhead=as.character() # subheading for code lesson | |
i=1 | |
ID[i]=i | |
subhead[i]="Simple R Expressions: Addition" | |
instruct[i]=paste('Try simple math','Type the command below','','1+1','',sep="\n") | |
i=i+1 | |
ID[i]=i | |
subhead[i]="Simple R Expressions: Strings" | |
instruct[i]=paste('Type the following string','','"Hello, Shiny"','',sep="\n") | |
i=i+1 | |
ID[i]=i | |
subhead[i]="Simple R Expressions: Multiplication" | |
instruct[i]=paste('Multiply 6*7','',sep="\n") | |
i=i+1 | |
ID[i]=i | |
subhead[i]="Simple R Expressions: Logical" | |
instruct[i]=paste('Expressions can also return a logical value: TRUE or FALSE', | |
'Type the following expression which returns a logical value', | |
'','3 < 4',sep="\n") | |
i=i+1 | |
ID[i]=i | |
subhead[i]="Simple R Expressions: Logical" | |
instruct[i]=paste('Another logical value, for equality use double == sign','','Type 2+2 == 5',sep="\n") | |
i=i+1 | |
ID[i]=i | |
subhead[i]="Vectors with Sequence in R" | |
instruct[i]=paste('We want to get a vector from 1 to 20', | |
'We can use the seq function in R in the following way', | |
'', | |
'> seq(1,20)', | |
'',sep="\n") | |
i=i+1 | |
ID[i]=i | |
subhead[i]="Vectors with Sequence in R" | |
instruct[i]=paste('We want to get a vector with 10 equally spaced values from 1 to 20', | |
'We can use the seq function in R in the following way', | |
'', | |
'> seq(1,20,length.out=10)', | |
'',sep="\n") | |
i=i+1 | |
ID[i]=i | |
subhead[i]="Vectors with Sequence in R" | |
instruct[i]=paste('We want to get a vector from 1 to 20 that increments by 2', | |
'We can use the seq function in R in the following way', | |
'', | |
'> seq(1,20,by=2)', | |
'',sep="\n") | |
i=i+1 | |
ID[i]=i | |
subhead[i]="Data Summary in R" | |
instruct[i]=paste('We want review what is in dataset mtcars', | |
'We can use the function head for this', | |
'', | |
'> head(mtcars)', | |
'',sep="\n") | |
i=i+1 | |
ID[i]=i | |
subhead[i]="Data Summary in R" | |
instruct[i]=paste('We want review what is in dataset mtcars', | |
'We can use the function summary to get more information on the dataset', | |
'', | |
'> summary(mtcars)', | |
'',sep="\n") | |
i=i+1 | |
ID[i]=i | |
subhead[i]="Plotting in R using ggplot2" | |
instruct[i]=paste('This part is based on code from Hadley Wickham\'s R book', | |
'ggplot2: Elegant Graphics for Data Analysis (http://ggplot2.org/book)', | |
'As in the book, we will work with a sample of 1000 records from diamonds dataset (store in data dsmall)', | |
'First, we view what is there in data', | |
'', | |
'> head(dsmall)', | |
'',sep="\n") | |
i=i+1 | |
ID[i]=i | |
subhead[i]="Plotting in R using ggplot2: Scatter Plot" | |
instruct[i]=paste('We will create a scatter plot of price vs carat in mtcars data using ggplot2', | |
'', | |
'> p=qplot(carat,price,data=dsmall); print(p)', | |
'',sep="\n") | |
i=i+1 | |
ID[i]=i | |
subhead[i]="Plotting in R using ggplot2: Scatter Plot" | |
instruct[i]=paste('We will color the scatter plot of price vs carat based on cut', | |
'', | |
'> p=qplot(carat,price,data=dsmall,color=cut); print(p)', | |
'',sep="\n") | |
i=i+1 | |
ID[i]=i | |
subhead[i]="Plotting in R using ggplot2: Box Plot" | |
instruct[i]=paste('We will draw a box plot of price vs cut', | |
'', | |
'> p=qplot(cut,price,data=dsmall,geom="boxplot"); print(p)', | |
'',sep="\n") | |
i=i+1 | |
ID[i]=i | |
subhead[i]="Plotting in R using ggplot2: Bar Plot" | |
instruct[i]=paste('We will draw a bar graph of cut', | |
'', | |
'> p=qplot(cut,data=dsmall,geom="bar"); print(p)', | |
'',sep="\n") | |
i=i+1 | |
ID[i]=i | |
subhead[i]="Plotting in R using ggplot2: Bar Plot" | |
instruct[i]=paste('We will draw a bar graph of cut and fill each bar based on clarity', | |
'', | |
'> p=qplot(cut,data=dsmall,geom="bar",fill=clarity); print(p)', | |
'',sep="\n") | |
codelist=data.frame(cbind(ID,instruct,subhead)) | |
codelist$instruct=as.character(codelist$instruct) | |
codelist$subhead=as.character(codelist$subhead) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(shiny) | |
library(datasets) | |
library(ggplot2) | |
library(stringr) | |
# source the file that contains the instructions for each R code snippet | |
source("instructR.R") | |
# subset of diamonds dataset from ggplot2 package | |
# reference: | |
# ggplot2: Elegant Graphics for Data Analysis (http://ggplot2.org/book) | |
dsmall=diamonds[sample(nrow(diamonds),1000),] | |
shinyServer(function(input,output) { | |
# Get subheading from codelist for a given value in code lesson input box | |
output$subhead = reactiveText(function(){ | |
idx=input$ID | |
codelist$subhead[idx] | |
}) | |
# Get instructions on what code to type for a given value in code lesson input box | |
output$instruct = reactivePrint(function(){ | |
idx=input$ID | |
writeLines(codelist$instruct[idx]) | |
}) | |
# Run the code typed in the input text box | |
# if the code text doesn't have plot substring run this piece to print out R output | |
output$coderun = reactivePrint(function(){ | |
plotloc=str_locate(input$code,"plot") | |
if(is.na(plotloc[1]) & (input$code != "")){ | |
eval(parse(text=input$code)) | |
} | |
}) | |
# if the code text has plot substring then run this piece to print out R plot | |
output$codeplot = reactivePlot(function(){ | |
plotloc=str_locate(input$code,"plot") | |
if(!(is.na(plotloc[1])) & (input$code != "")){ | |
eval(parse(text=input$code)) | |
} | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(shiny) | |
shinyUI(pageWithSidebar( | |
headerPanel("Attempt at Interactive R Learning"), | |
sidebarPanel( | |
helpText('TryR (http://tryr.codeschool.com) is a cool site set up to learn R. | |
I just wanted to play with Shiny to see if some basic form of that could be done in Shiny'), | |
br(), | |
br(), | |
numericInput("ID", "Code Lesson", 1), | |
submitButton("Update"), | |
br(), | |
helpText('To go to next code lesson, increment counter above and hit the update button. | |
A set of instructions for the lesson with appear on the right. You can directly go to | |
any lesson by typing a lesson number above and hitting the update button. Since there | |
are 16 code lessons, the maximum number you can enter above is 16'), | |
br(), | |
helpText('In the right side main panel, you will notice NULL appearing. Please ignore it. | |
It is a result of my lack of R programming skills and I couldn\'t figure | |
out how to get rid of it') | |
), | |
mainPanel( | |
# subheading for the code lesson | |
h3(textOutput("subhead")), | |
# instructions for the code lesson | |
verbatimTextOutput("instruct"), | |
br(), | |
# text box where R code can be typed | |
textInput("code","Type code here and click update button on the left to run code"), | |
# Output from R code typed if the code does not contain a plot substring | |
verbatimTextOutput("coderun"), | |
# Plot output from R code typed if the code contains a plot substring | |
plotOutput("codeplot") | |
) | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment