Skip to content

Instantly share code, notes, and snippets.

@rdabbler
Last active December 10, 2015 10:58
Show Gist options
  • Save rdabbler/4424361 to your computer and use it in GitHub Desktop.
Save rdabbler/4424361 to your computer and use it in GitHub Desktop.
Shiny App to Test User Input Forms (Using actionButtons)
pid project duedt stage updttime
1 Proj1 2013-12-01 Stg0 2012-12-20 18:50:00
2 Proj2 2013-06-01 Stg2 2012-12-20 18:50:00
3 Proj3 2014-03-01 Stg0 2012-12-20 18:50:00
4 Proj4 2015-08-01 Stg1 2012-12-20 18:50:00
5 Proj5 2013-12-01 Stg1 2012-12-20 18:50:00
library(shiny)
library(shinyIncubator)
library(plyr)
# load a very small projects file
# Eventually the idea is to have a database link to SQLite or access
projects=read.csv("projects.csv",sep=",",stringsAsFactors=FALSE)
# Define server logic
shinyServer(function(input, output) {
# if Projects -> View Projects is selected, this outputs the lists in dataframe projects
output$projlist=reactivePrint(function(){
if(!(input$menuproj == "View Projects")) return(NULL)
df=ddply(projects,c("project"),subset,updttime == max(updttime))
df2=df[order(df$project),]
})
# if Projects -> Add New Project is selected and
# a new project is entered in the text box and
# Add new project button is clicked
# projnew is the variable that is set to 1 when a project is added and reset to 0
# immediately after that. This ensures that subsequent typing in text box doesn't have any impact
# unless the add new project button is clicked again
projnew <<- 0
addproj=reactive(function(){
input$projnew
if(input$projnew > 0) {projnew <<- 1}
})
output$newprojout=reactivePrint(function(){
# call to reactive function that responds to clicking add new project button
addproj()
# Message when button is not clicked
if(projnew == 0) {
# I am not sure why the following line is needed here but somehow had problems without this line
input$projnew
msg="Click Add New Project Button to Submit Project"
return(msg)
}
# adding the new project to database when button is clicked
pidnew=nrow(projects)+1
updttime=as.character(Sys.time())
projects <<- rbind(projects,data.frame(
pid=pidnew,
project=input$newproj,
duedt=input$newduedt,
stage=input$newstg,
updttime=updttime
))
# resetting projnew to 0 so that any typing in text box has no impact
projnew <<- 0
# output
projects[nrow(projects),]
})
output$projnames=reactiveUI(function(){
if(input$menuproj == "Modify Existing Project") {
projname=unique(projects$project)
selectInput("modifproj","Modify Project",
choices=c("ChooseOne",projname))
}
})
# projmodif is the variable that is set to 1 when a project is modified and reset to 0
# immediately after that. This ensures that subsequent typing in text box doesn't have any impact
# unless button is clicked again
projmodif <<- 0
modifproj=reactive(function(){
input$projmodif
if(input$projmodif > 0) {projmodif <<- 1}
})
output$modifprojout=reactivePrint(function(){
# call to reactive function that responds to clicking modify project button
modifproj()
# Message when button is not clicked
if(projmodif == 0) {
# I am not sure why the following line is needed here but somehow had problems without this line
input$projmodif
msg="Click Modify Project Button to Modify Project"
return(msg)
}
# adding modified project data to the database
pidnew=nrow(projects)+1
updttime=as.character(Sys.time())
projects <<- rbind(projects,data.frame(
pid=pidnew,
project=input$modifproj,
duedt=input$modifduedt,
stage=input$modifstg,
updttime=updttime
))
projsub=projects[projects$project == input$modifproj,]
# resetting projnew to 0 so that any typing in text box has no impact
projmodif <<- 0
#output
projsub[nrow(projsub),]
})
})
library(shiny)
library(shinyIncubator)
# Objective: Test app to see if I can input data into a database
# Currently no database connection is set up and this app just starts with uploading a simple csv file
# The data gets added to a temporary dataframe
shinyUI(pageWithSidebar(
# Application title
headerPanel("Test Form Input to Database"),
sidebarPanel(
# main type of view to select
selectInput("menutype","Choose View Type",
choices=c("ChooseOne","Projects","Project-Steps","Tasks")),
# selection if Projects view is selected
conditionalPanel(
condition = "input.menutype == 'Projects'",
selectInput("menuproj","Choose an Option",
choices=c("ChooseOne","View Projects","Add New Project","Modify Existing Project"))
)
),
mainPanel(
# view if Projects -> View Projects is selected
conditionalPanel(
condition = "input.menuproj == 'View Projects'",
HTML("<h3> Project List </h3>"),
verbatimTextOutput("projlist")
),
# view if Projects -> Add Project is selected
conditionalPanel(
condition = "input.menuproj == 'Add New Project'",
HTML("<h3> Add a New Project </h3>"),
textInput("newproj","Input New Project Name"),
selectInput("newstg","Current Project Stage",
choices=c("ChooseOne","Stg0","Stg1","Stg2")),
textInput("newduedt","Next Due Date (YYYY-MM-DD)"),
br(),
actionButton("projnew","Add New Project"),
verbatimTextOutput("newprojout")
),
# view if Projects -> Modify existing project is selected
conditionalPanel(
condition = "input.menuproj == 'Modify Existing Project'",
HTML("<h3> Modify an Existing Project </h3>"),
uiOutput("projnames"),
selectInput("modifstg","Current Project Stage",
choices=c("ChooseOne","Stg0","Stg1","Stg2")),
textInput("modifduedt","Next Due Date (YYYY-MM-DD)"),
br(),
actionButton("projmodif","Modify Existing Project"),
verbatimTextOutput("modifprojout")
),
# view if Project-Steps is selected (to Do)
conditionalPanel(
condition="input.menutype == 'Project-Steps'",
HTML("<h3> To be Done </h3>")),
# view if Tasks is selected (to Do)
conditionalPanel(
condition="input.menutype == 'Tasks'",
HTML("<h3> To be Done </h3>"))
)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment