闪亮输入附加数据框

时间:2018-05-09 17:47:41

标签: r shiny

我有一个数据库,我正在加载到Shiny应用程序中。我有用户必须输入的Shiny输入。当我按下"提交"它将提交的数据附加到data.frame的底部。

我有以下代码。我希望我的Testdata data.frame附加输入数据。我相信我在想这个但是会喜欢任何帮助。我最初的想法是,当按下提交按钮时,rbind或附加反馈data.frame,但我无法弄明白。

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
      )
    })
  }
)

编辑:代码尝试根据反馈绑定数据框:

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
)

2 个答案:

答案 0 :(得分:0)

只需在输入变量附加用户行即可。请注意,传递到rbind的所有数据集中的列必须完全匹配(即,没有不同的命名或省略的列)。

...
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)

}

答案 1 :(得分:0)

我通过@Parfait的一些输入找出了解决方案。我附上了解决方案。

    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
)