gpt4 book ai didi

r - 将输入值保存在 csv 文件中,在每次提交时添加一行, Shiny

转载 作者:行者123 更新时间:2023-12-02 01:16:52 27 4
gpt4 key购买 nike

我正在尝试构建一个 Shiny 的应用程序,类似于在选择输入数据后我可以单击“提交”按钮,自动上传一个 cvs 文件,添加一行新数据。我看到结构应该类似于调查的结构,这是我的代码,它给出了以下错误:

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

代码:

library(shiny)
library(googlesheets)
library(DT)


# Define UI for application that draws a histogram
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")
),

actionButton("Action", "Submit"), tags$hr()


)

library(shiny)
library("DT")

server <- function(input, output){
Results <- reactive(c(nome, cognome, email, gioiello, modello, materiale, Sys.Date()))

Data[nrow(Data) +1,] <- reactive( if(input$Action == 1) {Results()}) # Put data into next row of the "Data" when the action button is pressed
write.csv(Data, file = "/Users/cleliagasparri/app2/Data.csv") # Download new Data to replace Data.csv file in the shiny folder


##### Function 1, Create a data download option ####

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)

最佳答案

这可能不是最好的方法,但它确实有效。

我又添加了一个下载按钮,因此它可以与下载处理程序一起使用。为了使我在做什么更清楚,我添加了一个表输出。提交按钮现在将行附加到内部保存为数据框的表中。要保存文件,可以使用下载按钮。

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)

希望对您有所帮助!

关于r - 将输入值保存在 csv 文件中,在每次提交时添加一行, Shiny ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42260739/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com