简体   繁体   中英

Shiny Inputs Append Data Frame

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
)

Simply append user row with input variables. Do be aware columns in all datasets passed into rbind must match exactly (ie, 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)

}

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
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM