R 如何在输入表单数据时保存表单数据
我正在开发一个带有本地文件存储的数据库程序。我想在单击sumbit按钮(goButton)之前将表单条目保存在一个临时变量和相关的临时文件中(这样,如果窗口关闭,用户可以返回)。这是针对单个用户的。问题:1)当两个字段都输入了文本(但未按下按钮)时,为什么脚本会记录最后一次输入(名称)和id的空白条目?2) 有没有一种方法可以监控多个输入,而不必对每个输入进行接线和观察(实际代码有100多个字段)R 如何在输入表单数据时保存表单数据,r,shiny,R,Shiny,我正在开发一个带有本地文件存储的数据库程序。我想在单击sumbit按钮(goButton)之前将表单条目保存在一个临时变量和相关的临时文件中(这样,如果窗口关闭,用户可以返回)。这是针对单个用户的。问题:1)当两个字段都输入了文本(但未按下按钮)时,为什么脚本会记录最后一次输入(名称)和id的空白条目?2) 有没有一种方法可以监控多个输入,而不必对每个输入进行接线和观察(实际代码有100多个字段) 库(闪亮) 库(数据表) 图书馆(DT) #首次运行时取消下面的注释并运行 #temp我认为您需要
库(闪亮)
库(数据表)
图书馆(DT)
#首次运行时取消下面的注释并运行
#temp我认为您需要使用“帮助解决第二个问题,您可以使用reactive
将用户已经输入的变量保存到temp.rds中,是否愿意分享一个reactive示例?
library(shiny)
library(data.table)
library(DT)
# on first run uncomment below and run
# temp <- list(id = numeric(), name=character())
# saveRDS(temp,"temp.rds")
#import table data
mydata <- as.data.table(readRDS("mydata.rds"))
# read temp file
temp <- readRDS("temp.rds")
temp
# ui
ui <- fluidPage(dataTableOutput("table"), # datatable here
# form inputs
numericInput(inputId ='id', label = 'Enter new ID', value =temp$id),
textInput(inputId ='name', label = 'Enter new name', value = temp$name),
actionButton(inputId ="goButton", label = "Update Table"))
server <- function(input,output, session){
# when user submits form, delete temp file.
observeEvent(input$goButton,{
temp <- list(id = numeric(), name=character())
saveRDS(temp, "temp.rds")
temp
# updateTextInput()
})
# save form data as its entered
observeEvent(input$id, {
temp$id <- input$id
saveRDS(temp, "temp.rds")
})
observeEvent(input$name, {
temp$name <- input$name
saveRDS(temp, "temp.rds")
})
# DT output
output$table <- renderDataTable(df())
# update datatable
df <- eventReactive(input$goButton, {
if(input$id !="" && !(input$id %in% unique(mydata$id)) && input$goButton>0){
newrow = data.table(id = input$id,
val = input$name)
mydata <<- rbind(mydata, newrow)
}else{
if(input$id !="" && (input$id %in% unique(mydata$id)) && input$goButton>0){
mydata[id==input$id, val := input$name]
mydata <<- mydata
}
}
saveRDS(mydata,"mydata.rds")
mydata
#write report
}, ignoreNULL = FALSE)
}
shinyApp(ui,server)
observeEvent(input$goButton,{
temp <<- list(id = numeric(), name=character())
saveRDS(temp, "temp.rds")
temp
# updateTextInput()
})
observeEvent(input$id, {
temp$id <<- input$id
saveRDS(temp, "temp.rds")
})
observeEvent(input$name, {
temp$name <<- input$name
saveRDS(temp, "temp.rds")
})