Hi everybody
I'm Cedric from Cameroon. I'm novice with shinyapp and i've found the article on Persistent data storage extremely helpfull.
I'm actually trying to build an interactive map where people can enter data online to update the map.
I've used the article to solve the problem related to remote data storage, but i"m facing another issues: permanently deleted bad data from the datatableOutput and on the map
In fact it's possible that user make mistake and submit bad information and want to delete it online by selecting the corresponding row on the datatable.
I don't know how to implement it in my shiny app or whether it is possible.
If it is not possible how can i prevent such problem
Here is the link of the alpha version of my shiny app already deployed with shinyapp.io: https://mnca.shinyapps.io/Shiny/
Here is an example of code which mimic what i'm trying to do:for local storage
library(shiny)
outputDir <- "responses"
saveData <- function(data) {
data <- t(data)
# Create a unique file name
fileName <- sprintf("%s_%s.csv", as.integer(Sys.time()), digest::digest(data))
# Write the file to the local system
write.csv(
x = data,
file = file.path(outputDir, fileName),
row.names = FALSE, quote = TRUE
)
}
loadData <- function() {
# Read all the files into a list
files <- list.files(outputDir, full.names = TRUE)
data <- lapply(files, read.csv, stringsAsFactors = FALSE)
# Concatenate all data together into one data.frame
data <- do.call(rbind, data)
data
}
# Define the fields we want to save from the form
fields <- c("References", "Categories", "Countries", "Regions", "Divisions", "Localites",
"Lon", "Lat", "Start", "End", "Species", "Method", "Family", "Insecticide",
"Levels", "Exposed", "Mortality", "Survivor", "Resistance", "Code", "SMR",
"NMR", "Frequency", "CSMR")
# Shiny app with 3 fields that the user can submit data for
ui <- fluidPage(
wellPanel(
h3(strong("Enter new data")),
fluidRow(
column(3, textInput("References", "References", "")),
column(3, textInput("Categories", "Categories", "")),
column(3, textInput("Countries", "Countries", "")),
column(3, selectInput("Regions", "Regions",
choices = list("Adamaoua", "Centre", "Est", "Extreme Nord",
"Littoral", "Nord", "Nord Ouest", "Ouest",
"Sud", "Sud Ouest"),
selected = "Adamaoua"))
),
fluidRow(
column(3, textInput("Divisions", "Divisions", "")),
column(3, textInput("Localites", "Localites", "")),
column(3, numericInput("Lon", "Longitude", 0.0000)),
column(3, numericInput("Lat", "Latitude", 0.0000))
),
fluidRow(
column(3, numericInput("Start", "Year of beginning", 2018)),
column(3, numericInput("End", "Year of end", 2018)),
column(3, selectInput("Species", "Species",
choices = list("An coluzzii", "An funestus", "An gambiae",
"An rufipes"),
selected = "An gambiae")),
column(3, textInput("Method", "Method of the test of sensitivity", ""))
),
fluidRow(
column(3, textInput("Family", "Class of insecticide", "")),
column(3, selectInput("Insecticide", "Type of insecticide",
choices = list("Bendiocarb", "Bifenthrin", "Chlorpyriphos Methyl",
"Cyfluthrin", "DDT", "DEF+DDT", "DEF+Deltamethrine",
"DEF+Permethrine", "Deltamethrine", "DEM+DDT",
"DEM+Deltamethrine", "DEM+Permethrine", "Dieldrin",
"Etofenprox", "Fenitrothion", "Lamdacyalothrine ",
"Malathion", "PBO+DDT", "PBO+Deltamethrine",
"PBO+Etofenprox", "PBO+Lambdacyhalothrin", "PBO+Permethrine",
"Permethrine", "Phenithrothion", "Propoxur"),
selected = "Permethrine")),
column(3, numericInput("Levels", "% concentration of insecticide", 0.0000)),
column(3, numericInput("Exposed", "Number of mosquitoes exposed", 1))
),
fluidRow(
column(3, numericInput("Mortality", "% Mortality", 0.0000)),
column(3, numericInput("Survivor", "% Survivor", 0.0000)),
column(3, selectInput("Resistance", "Status of resistance",
choices = list("Resistance probable", "Resistant",
"Sensible"),
selected = "Resistant")),
column(3, selectInput("Code", "Code",
choices = list("R", "RP", "S"),
selected = "R"))
),
fluidRow(
column(3, textInput("SMR", "Status of the mechanism of resistance", "")),
column(3, textInput("NMR", "Name of the mechanism of resistance", "")),
column(3, numericInput("Frequency", "% Frequency", 0.0000)),
column(3, selectInput("CSMR", "Code of the status",
choices = list("Detecte", "Non detecte"),
selected = "Detecte"))
), hr(),
actionButton("submit", "Submit", class = "btn-primary")
),
wellPanel(h3(strong("Data Table")), br(),
fluidRow(
DT::dataTableOutput("responses", width = 300)
), hr(),
actionButton("delete", "Delete row selected", class = "btn-primary",
icon=icon("times", class = NULL, lib = "font-awesome"))
)
)
server <- function(input, output, session) {
# Whenever a field is filled, aggregate all form data
formData <- reactive({
data <- sapply(fields, function(x) input[[x]])
data
})
# When the Submit button is clicked, save the form data
observeEvent(input$submit, {
saveData(formData())
#reset all the widget input to default values
updateTextInput(session, "References", "References", "")
updateTextInput(session, "Categories", "Categories", "")
updateTextInput(session, "Countries", "Countries", "")
updateSelectInput(session, "Regions", "Regions", selected = "Adamaoua")
updateTextInput(session, "Divisions", "Divisions", "")
updateTextInput(session, "Localites", "Localites", "")
updateNumericInput(session, "Lon", "Longitude", 0.0000)
updateNumericInput(session, "Lat", "Latitude", 0.0000)
updateNumericInput(session, "Start", "Year of beginning", 2018)
updateNumericInput(session, "End", "Year of end", 2018)
updateSelectInput(session, "Species", "Species", selected = "An gambiae")
updateTextInput(session, "Method", "Method of the test of sensitivity", "")
updateSelectInput(session, "Insecticide", "Type of insecticide", selected = "Permethrine")
updateNumericInput(session, "Levels", "% concentration of insecticide", 0.0000)
updateNumericInput(session, "Exposed", "Number of mosquitoes exposed", 1)
updateNumericInput(session, "Mortality", "% Mortality", 0.0000)
updateNumericInput(session, "Survivor", "% Survivor", 0.0000)
updateSelectInput(session, "Resistance", "Status of resistance", selected = "Resistant")
updateSelectInput(session, "Code", "Code", selected = "R")
updateTextInput(session, "SMR", "Status of the mechanism of resistance", "")
updateTextInput(session, "NMR", "Name of the mechanism of resistance", "")
updateNumericInput(session, "Frequency", "% Frequency", 0.0000)
updateSelectInput(session, "CSMR", "Code of the status", selected = "Detecte")
})
# when the clear button is clicked, delete the fields and the corresponding data file in the outputDir
observeEvent(input$delete, {
xxx
})
# Show the previous responses
# (update with current response when Submit is clicked)
output$responses <- DT::renderDataTable({
input$submit
loadData()
})
}
# Create the app object
shinyApp(ui = ui, server = server)
Please, can someone help me in any way
Sincerly