Skip to content

Instantly share code, notes, and snippets.

@rdabbler
Last active December 10, 2015 08:58
Show Gist options
  • Save rdabbler/4410797 to your computer and use it in GitHub Desktop.
Save rdabbler/4410797 to your computer and use it in GitHub Desktop.
Shiny App to Test User Input with a Form
library(shiny)
# load a very small projects file
# Eventually the idea is to have a database link to SQLite or access
projects=data.frame(pid=c(1,2,3,4,5),project=c("proj1","proj2","proj3","proj4","proj5"))
# 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)
projects
})
# if Projects -> Add New Project is selected and
# a new project is entered in the text box and
# Update choice is selected in dropdown this this adds the new project to the dataframe projects
output$newprojout=reactivePrint(function(){
if(input$projupdt == "Update") {
pidnew=nrow(projects)+1
projects <<- rbind(projects,data.frame(pid=pidnew,project=input$newproj))
}
projects[nrow(projects),]
})
})
library(shiny)
# 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 dataframe
# 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 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 Project'",
HTML("<h3> Add or Modify a Project </h3>"),
selectInput("projupdt","Choose option Update to Add/Modify Project",
choices=c("Choose","Update")),
textInput("newproj","Add New Project"),
verbatimTextOutput("newprojout")
),
# 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