以下这篇文章:( ShinyR:为进一步使用数据库插入用户输入 ),我发现了一个解决我的问题,这个闪亮的应用强烈的启发( https://deanattali.com/2015/06/14/mimicking-google -形式-发亮/ )。
我设法让主的结果,我想(数据库结合我的观察和应用程序的用户的结果)。 不过我现在有两个新的问题:
首先,在我的代码看(在帖子的末尾完整版本)时,怎么可能。 下面是从代码使用来提取输入的服务器部分的行。 创建两个变量
- UDATA仅用来自用户的要被保存为单个文件的输入(%S_%s.csv)。
- MDATA其是输入(UDATA)和数据库(DB_Nonames)的组合,也被保存为单个文件(%S_%s_Merged.csv)。
这个问题在这里遇到了:只有一个文件被保存(%S_%s.csv),但它不仅是从用户(UDATA)的投入,但也完整的数据库(DB_Nonames)组成。 该错误信息是“说法‘MDATA’缺失,没有默认值”,就像它不存在,但它似乎在某种程度上UDATA得到了与数据库合并,同时仍然被称为UDATA(即使它不是假设)。
formData <- reactive({
Udata <- sapply(fieldsAll, function(x) input[[x]]) #fieldsAll is an array with all the fields completed by the user
Udata <- c(Udata, PSS = (input$SSS/input$SBL)) #Test to see if I can add a new field using inputs
Udata <- t(Udata) #transposition to get a line
Mdata <- rbind(DB_NoNames,Udata) #Merge of my database and the inputs from the user
})
saveData <- function(Udata,Mdata) {
fileName <- sprintf("%s_%s.csv", #First a .csv file with only the inputs from the user
humanTime(),
digest::digest(Udata)) #To get a unique fileName for each user using time of submit and values of the inputs
fileName2 <- sprintf("%s_%s_Merged.csv", #Second a .csv file with the database + the inputs
humanTime(),
digest::digest(Udata))
write.csv(x = Udata, file = file.path(responsesDir, fileName),
row.names = c(indnames))
write.csv(x = Mdata, file = file.path(responsesDir, fileName2),
row.names = c(indnames))
}
二,认识这个巫术后,怎么可能能解决吗?
非常感谢所有的,如果我不清楚,请让我知道,我会尽力的其他解释。
下面是完整的代码(大部分来自https://deanattali.com/2015/06/14/mimicking-google-form-shiny/其详细的解释相关)
#############################################
DB <- read.csv2("~/filepath/DB.csv", row.names = 1, sep=",", dec=".")
DB_NoNames <- DB
rownames(DB_NoNames) <- NULL
indnames <- c(rownames(DB),"USER")
fieldsMandatory <- c("SBL", "SSS")
labelMandatory <- function(label) {
tagList(label, span("*", class = "mandatory_star")
)}
appCSS <- ".mandatory_star { color: red; }
#error { color: red; }"
fieldsAll <- c("AGE", "SBL", "SSS")
responsesDir <- file.path("~/filepath/responses")
responsesDBDir <- file.path("~/filepath/ResponsesAndDb")
epochTime <- function() {
as.integer(Sys.time())
}
humanTime <- function() format(Sys.time(), "%Y%m%d-%H%M%OS")
loadData <- function() {
files <- list.files(file.path(responsesDir), full.names = TRUE)
data <- lapply(files, read.csv, stringsAsFactors = FALSE)
data <- dplyr::rbind_all(data)
data
}
adminUsers <- c("admin")
#############################################
shinyApp(
#############################################
ui = fluidPage(
shinyjs::useShinyjs(),
shinyjs::inlineCSS(appCSS),
titlePanel("Users'data"),
uiOutput("adminPanelContainer"),
div(
id = "form",
numericInput("AGE", "Age de la ferme", value = 1, min=0),
numericInput("SBL", labelMandatory("Surface brute en légumes (ha)"), value = 1, min=0),
numericInput("SSS", labelMandatory("Surface sous serre (ha)"), value = 0.3, min=0),
actionButton("submit", "Valider", class = "btn-primary"),
shinyjs::hidden(
span(id = "submit_msg", "Submitting..."),
div(id = "error",
div(br(), tags$b("Error: "), span(id = "error_msg"))
)
)
),
shinyjs::hidden(
div(
id = "thankyou_msg",
h3("Merci, vos données ont été enregistrées avec succès. Vous pouvez maintenant utiliser l'outil ou enregistrer de nouvelles données"),
actionLink("submit_another", "Enregistrer de nouvelles données")
)
)
),
#############################################
server = function(input, output, session) {
observe({
mandatoryFilled <-
vapply(fieldsMandatory,
function(x) {
!is.null(input[[x]]) && input[[x]] != ""
},
logical(1))
mandatoryFilled <- all(mandatoryFilled)
shinyjs::toggleState(id = "submit", condition = mandatoryFilled)
})
formData <- reactive({
Udata <- sapply(fieldsAll, function(x) input[[x]])
Udata <- c(Udata, PSS = (input$SSS/input$SBL))
Udata <- t(Udata)
Mdata <- rbind(DB_NoNames,Udata)
})
saveData <- function(Udata,Mdata) {
fileName <- sprintf("%s_%s.csv",
humanTime(),
digest::digest(Udata))
fileName2 <- sprintf("%s_%s_Merged.csv",
humanTime(),
digest::digest(Udata))
write.csv(x = Udata, file = file.path(responsesDir, fileName),
row.names = c(indnames))
# write.csv(x = Mdata, file = file.path(responsesDir, fileName2),
# row.names = c(indnames))
}
# action to take when submit button is pressed
observeEvent(input$submit, {
shinyjs::disable("submit")
shinyjs::show("submit_msg")
shinyjs::hide("error")
tryCatch({
saveData(formData())
shinyjs::reset("form")
shinyjs::hide("form")
shinyjs::show("thankyou_msg")
},
error = function(err) {
shinyjs::html("error_msg", err$message)
shinyjs::show(id = "error", anim = TRUE, animType = "fade")
},
finally = {
shinyjs::enable("submit")
shinyjs::hide("submit_msg")
})
})
observeEvent(input$submit_another, {
shinyjs::show("form")
shinyjs::hide("thankyou_msg")
})
output$responsesTable <- DT::renderDataTable(
loadData(),
rownames = FALSE,
options = list(searching = FALSE, lengthChange = FALSE)
)
output$downloadBtn <- downloadHandler(
filename = function() {
sprintf("mimic-google-form_%s.csv", humanTime())
},
content = function(file) {
write.csv(loadData(), file, row.names = FALSE)
}
)
output$adminPanelContainer <- renderUI({
if (!isAdmin()) return()
wellPanel(
h2("Previous responses (only visible to admins)"),
downloadButton("downloadBtn", "Download responses"), br(), br(),
DT::dataTableOutput("responsesTable")
)
})
isAdmin <- reactive({
is.null(session$user) || session$user %in% adminUsers
})
})