【问题标题】:Shiny Inputs Append Data Frame闪亮的输入附加数据帧
【发布时间】:2018-10-19 22:06:17
【问题描述】:

我有一个要加载到 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
)

【问题讨论】:

  • 我最初的想法是 rbind 或附加响应式 data.frame ...您是否尝试过最初的想法?如果是这样,发生了什么?错误?不想要的结果?你的数据库在哪里?
  • @Parfait,我尝试创建一个反应式 data.frame 来接收输入值并将现有的 data.frame 与新的数据框合并,但它失败了。我从这个示例中查看了我的数据库,并创建了 Testdata 来模仿它。
  • 你能发布你的试用代码和错误信息/不想要的结果吗?

标签: r shiny


【解决方案1】:

只需将输入变量附加到用户行。请注意传递给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)

}

【讨论】:

  • 感谢@Parfait 的输入。当我尝试运行您建议的服务器部分时,我看到:“.getReactiveEnvironment()$currentContext() 中的错误:如果没有活动的反应上下文,则不允许操作。”我应该将 rbind 包裹在观察事件周围还是根据按下提交按钮的时间进行反应?
  • 任何时候你收到用户输入,它应该被包裹在reactive({})中。查看更新。
【解决方案2】:

我通过@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
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-17
    • 1970-01-01
    • 2013-09-19
    • 2014-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多