Edit datas with a Shiny App

#1

I want to know if its possible to change some values of my database thanks to values entered in textbox in a shiny app.
In fact I have to control the chronology of date, some controls are made automatically but some should have been made case by case watching the other values of the row.

So the first selectinput is used for choose the row to edit.
The second is used for choose the date we want the choose (example : the date of phone call or the date of intervention)
The text input is used for give the right date to put in the case like that :

data$dateappel[row] <- rightdate

dateappel : choosed in the seconde selectinput
row : choosed in the first selectinput
rightdate : wrote in the textinput

I hope that I'm clear about what I want to do.

P.S : sorry if I do mistakes, I'm french and not really good in english ^^

0 Likes

#2

Yes, it's possible to make such changes, but, for us being able to help you, could you make a minimal REPRoducible EXample (reprex) about your issue?

Here is a very useful guide about how to make a reprex for a shiny app

1 Like

#3

Ok
Here are my datas :
datas

Here its how I found wrong date

lignes_error <- which(data$depart> data$dateappel & !is.na(data$depart) & !is.na(data$dateappel))

For now I edit row like that :

data$depart[1]<- "2018-02-03"
data$dateappel[4]<- "2018-07-03"

But I want that people can chose the row and "depart" or "dateappel" to edit the value of date.

do_it = length(lignes_error) > 0

selectInput("ligne", label = "Choix de la ligne :",
              choices = c(lignes_error))

selectInput("variable", label = "Choix de la variable date a modifier :",
              choices = c("dateappel","accouch"))

textInput("nouvelledate", label = "Nouvelle date", value = "Date au format yyyy-mm-jj")

output$date <- renderPrint({ input$nouvelledate })

In fact i'm using shiny in RmarkDown application for now.
So I do verifications whith logics and when I's not possible I include shiny widgets to let the people decide what we had to do for some values.

I don't know if It's clear...

0 Likes

#4

Well, actually, that is not your data, its a screenshot of your data and we can't copy that into our own RStudio sessions, it would be easier to help you if you could turn this into a proper REPRoducible EXample (reprex)

If you've never heard of a reprex before, you might want to start by reading this FAQ:

0 Likes

#5

Sorry, I try to do it better

---
title: "Untitled"
author: "Author"
date: "10 avril 2019"
output: html_document
runtime : shiny
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, message=FALSE, warning = FALSE)
```

```{r}
data <- data.frame(
  dateappel = c("2018-02-03","2018-02-05", "2018-02-15", "2018-07-04", "2018-07-10"),
  depart = c("2018-02-02", "2018-02-05", "2018-02-15", "2018-07-03", "2018-07-10")
)

data$dateappel <- as.Date(data$dateappel)
data$depart <- as.Date(data$depart)


lignes_error <- which(data$depart < data$dateappel & !is.na(data$depart) & !is.na(data$dateappel))
```

```{r}
do_it = length(lignes_error) > 0

selectInput("ligne", label = "Choix de la ligne :",
              choices = c(lignes_error))

selectInput("variable", label = "Choix de la variable date a modifier :",
              choices = c("dateappel","depart"))

textInput("nouvelledate", label = "Nouvelle date", value = "Date au format yyyy-mm-jj")

output$date <- renderPrint({ input$nouvelledate })
```

I don't really know how to do that with a RMD document...

0 Likes

#6

Please, did someone has a solution for me?

0 Likes

#7

Did someone is searching for a salution for me or not ? Just tell me if its possible to do want I want to do

0 Likes

#8

Here is a possibility

---
title: "Untitled"
author: "Author"
date: "10 avril 2019"
output: html_document
runtime : shiny
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, message=FALSE, warning = FALSE)
if (!require(kableExtra)) {
  install.packages("kableExtra", repos = "https://cloud.r-project.org")
  library(kableExtra)
}
```

```{r, echo=FALSE}
edit_data <- function(data, lignes_error) {
  shinyApp(
    ui = fluidPage(
      fluidRow(
        column(3, 
               selectInput("ligne", label = "Choix de la ligne :",
                    choices = c(lignes_error))),
        column(3, 
               selectInput("variable", label = "Choix de la variable date a modifier :",
                    choices = c("dateappel","depart"))),
        column(3, 
               dateInput("nouvelledate", label = "Nouvelle date"))
      ),
      actionButton("submit", "Submit"),
      tableOutput("data"),
      actionButton("reset", "Reset")
    ),
    server = function(input, output, session) {
      data_rx <- reactiveVal(data)
      
      observeEvent(input$reset, {
        data_rx(data)
      })
      
      observeEvent(input$submit, {
        df <- data_rx()
        df[input$ligne, input$variable] <- as.Date(input$nouvelledate)
        data_rx(df)
      })
      
      output$data <- function() {
        knitr::kable(data_rx(), "html") %>% 
          kable_styling("striped")
      }
    }
  )
}
```

```{r}
data <- data.frame(
  dateappel = c("2018-02-03","2018-02-05", "2018-02-15", "2018-07-04", "2018-07-10"),
  depart = c("2018-02-02", "2018-02-05", "2018-02-15", "2018-07-03", "2018-07-10")
)

data$dateappel <- as.Date(data$dateappel)
data$depart <- as.Date(data$depart)

lignes_error <- which(data$depart < data$dateappel & !is.na(data$depart) & !is.na(data$dateappel))

do_it = length(lignes_error) > 0

edit_data(data, lignes_error)
```
0 Likes

#9

I just try your solution and R return me an error

I don't understand

0 Likes

#10

Thanks for reporting this. It comes from install.packages("kableExtra"). I edited my answer to fix it with install.packages("kableExtra", repos = "https://cloud.r-project.org")

0 Likes

#11

Wow thank you really for that solution !

0 Likes

#12

Regarding

P.S : sorry if I do mistakes, I'm french and not really good in english

you might find the French speaking community to be quite helpful on their slack. Here is an invitation link: https://frama.link/r-grrr

2 Likes

#13

See also http://www.stencilled.me/post/2019-04-18-editable/

1 Like