[英]Shiny Inputs Append Data Frame
I have a database that I am loading into a Shiny application.我有一个正在加载到 Shiny 应用程序中的数据库。 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.
我试图让它在我按下“提交”时将提交的数据附加到 data.frame 的底部。
I have the code below.我有下面的代码。 I want my Testdata data.frame to append the input data.
我希望我的 Testdata data.frame 附加输入数据。 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.
我最初的想法是在按下提交按钮时 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
)
})
}
)
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).请注意传递给
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)
}
I figured out the solution with some input from @Parfait.我从@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
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.