Reputation: 591
I have a database that I am loading into a Shiny application. I have Shiny inputs that the user must enter. I am trying to make it where when I press "Submit" that it appends the submission's data into the bottom of the data.frame.
I have the code below. I want my Testdata data.frame to append the input data. I am sure I am over thinking this but would love any help. My initial thought is to either rbind or append the reactive data.frame based off when the submit button is pressed but I can't figure it out.
library(shiny)
library(plotly)
library(DT)
shinyApp(
ui = navbarPage("Testing the saving to dataframes",
tabPanel("Plot",
mainPanel(
textInput("company", "Company Name", ""),
textInput("poc", "Point of Contact", ""),
textInput("sales_rep", "Sales Rep", ""),
sliderInput('chanceofsale',"Probability of Sale", min=1, max=10,value=5),
actionButton("submit", strong("Submit"))
)
),
tabPanel("Table",
DT::dataTableOutput("Test_Table")
)
),
server = function(input, output) {
Testdata = data.frame("company" = "company", "poc" = "poc","sales_rep" = "rep","chanceofsale" = "5")
output$Test_Table = renderDataTable({
datatable(Testdata
)
})
}
)
Edit: Code trying to bind the dataframes based off of feedback:
library(shiny)
library(plotly)
library(DT)
fieldsAll = c("company","poc","sales_rep","chanceofsale")
shinyApp(
ui = navbarPage("Testing the saving to dataframes",
tabPanel("Plot",
mainPanel(
textInput("company", "Company Name", ""),
textInput("poc", "Point of Contact", ""),
textInput("sales_rep", "Sales Rep", ""),
sliderInput('chanceofsale',"Probability of Sale", min=1, max=10,value=5),
actionButton("submit", ("Submit"))
)
),
tabPanel("Table",
DT::dataTableOutput("Combined_Table")
)
),
server = function(input, output) {
#Initial Dataframe
Testdata <- data.frame("company" = "company", "poc" = "poc",
"sales_rep" = "rep", "chanceofsale" = "5")
observeEvent(input$submit, {
Testdata <- rbind(Testdata,
data.frame("company" = input$company, "poc" = input$poc,
"sales_rep" = input$sales_rep,
"chanceofsale" = as.character(input$chanceofsale))
)
})
output$Test_Table = DT::renderDataTable(Testdata)
}#Server End
)
Upvotes: 1
Views: 3859
Reputation: 107767
Simply append user row with input variables. Do be aware columns in all datasets passed into rbind
must match exactly (i.e., no different named or omitted columns).
...
server = function(input, output) {
# DATABASE TABLE
Testdata <- data.frame("company" = "company", "poc" = "poc",
"sales_rep" = "rep", "chanceofsale" = "5")
user_data <- reactive({
data.frame("company" = input$company, "poc" = input$poc,
"sales_rep" = input$sales_rep,
"chanceofsale" = as.character(input$chanceofsale))
})
# APPEND USER ROW
Testdata <- rbind(Testdata, user_data())
output$Test_Table = DT::renderDataTable(Testdata)
}
Upvotes: 0
Reputation: 591
I figured out the solution with some input from @Parfait. I have attached the solution.
library(shiny)
library(plotly)
library(DT)
shinyApp(
ui = navbarPage("Testing the saving to dataframes",
tabPanel("Plot",
mainPanel(
textInput("company", "Company Name", ""),
textInput("poc", "Point of Contact", ""),
textInput("sales_rep", "Sales Rep", ""),
sliderInput('chanceofsale',"Probability of Sale", min=1, max=10,value=5),
actionButton("submit", ("Submit"))
)
),
tabPanel("Table",
"Testing Table",
DT::dataTableOutput("Test_Table"),
hr(),
hr(),
"Combined Table",
DT::dataTableOutput("Combined_table")
)
),
server = function(input, output) {
Testdata <- data.frame("company" = "company", "poc" = "poc",
"sales_rep" = "rep", "chanceofsale" = "5")
#Initial Dataframe
FinalData = eventReactive(input$submit,{
Testdata = rbind(Testdata,
data.frame("company" = input$company, "poc" = input$poc,
"sales_rep" = input$sales_rep,
"chanceofsale" = as.character(input$chanceofsale)))
})
#Initial Table
output$Test_Table = renderDataTable(Testdata)
#Combined Table
output$Combined_table = renderDataTable(FinalData())
}#Server End
)
Upvotes: 1