我正在尝试构建一个类似的闪亮应用程序,在选择输入数据后,我可以单击“提交”按钮,自动上传cvs文件,添加新数据行。 我看到结构应该类似于调查结构,这是我的代码,它给出了以下错误:
$1
代码:
Listening on http://127.0.0.1:3716
Warning: Error in <-: invalid type/length (closure/0) in vector allocation
Stack trace (innermost first):
42: server [/Users/cleliagasparri/app2/app.R#47]
1: runApp
Error in Data[nrow(Data) + 1, ] <- reactive(if (input$Action == 1) { :
invalid type/length (closure/0) in vector allocation
答案 0 :(得分:2)
这可能不是最好的方法,但它可以完成工作。
我已经添加了一个下载按钮,因此它适用于下载处理程序。为了使我正在做的更清楚,我添加了一个表输出。现在,提交按钮会将该行附加到表中,该表在内部保存为数据框。可以使用保存文件下载按钮。
library(shiny)
ui <- fluidPage(
textInput("nome", "Nome"),
textInput("cognome", "Cognome"),
textInput("email", "Email"),
radioButtons("gioiello", label = "Gioiello", choices = c("Orecchini" = 1, "Collana" = 2)
),
conditionalPanel(condition = "input.gioiello == 1",
selectInput(inputId = "modello",
label = "Modello",
choices = c("Serpenti" = 1, "Foglie" = 2, "Edere" = 3, # Responses
"Neither Agree nor Disagree" = 4, "Agree Somewhat" = 5, "Agree" = 6,
"Agree Strongly" = 7)
)),
conditionalPanel(condition = "input.gioiello == 2",
selectInput(inputId = "modello", # What we are calling the object
label = "Modello", # Label
choices = c("Serpenti" = 1, "Foglie" = 2, "Edere" = 3, # Responses
"Neither Agree nor Disagree" = 4, "Agree Somewhat" = 5, "Agree" = 6,
"Agree Strongly" = 7)
)),
radioButtons("materiale", label = "Materiale", choices = c("Oro", "Argento", "Bronzo rosa", "Bronzo giallo", "Rame")
),
#Table showing what is there in the data frame
tableOutput("table"),
#Button which appends row to the existing dataframe
actionButton("Action", "Submit"),
#Button to save the file
downloadButton('downloadData', 'Download')
)
library(shiny)
server <- function(input, output){
#Global variable to save the data
Data <- data.frame()
Results <- reactive(data.frame(input$nome, input$cognome, input$email, input$gioiello, input$modello, input$materiale, Sys.Date()))
#To append the row and display in the table when the submit button is clicked
observeEvent(input$Action,{
#Append the row in the dataframe
Data <<- rbind(Data,Results())
#Display the output in the table
output$table <- renderTable(Data)
})
output$downloadData <- downloadHandler(
# Create the download file name
filename = function() {
paste("data-", Sys.Date(), ".csv", sep="")
},
content = function(file) {
write.csv(Data, file) # put Data() into the download file
})
}
shinyApp(ui = ui, server = server)
希望它有所帮助!