0

I am stuck and need help.

I am working with this gist https://gist.github.com/gluc/d39cea3d11f03542970b

Basically in a shiny app it provides the possibility to make CRUD maneuvers and it works perfect.

I now managed to store the data on googlesheets and also to load the data in with: read_sheet function from googlesheets4 package.

The issue is if I want to delete the last row of the table within the shiny app the first row is deleted after pressing the delete button.

This problem occurs only if I load in preexistend data (in my case from googlesheets). I have encountered the problem: The id is not updating when I click the rows in the shiny app table.

I have used browser() and went through each function but I can't find the problem.

If I click on first row and delete the first row everything works perfect, but clicking the second or any other row except the first, always the first row is deleted.

Update: I think the main issue is that after reading in the data from gogglesheets the disabled Id field is not navigating with the table, see picture.

If I click row 3 then the Id field should be 3 but it stays at 1 all the time. Therefore any action (for example deleting) on the table removes row one.

I can't get the link between the dataframe that is loaded at the beginning in the environment and the data that is defined by the CRUD application. The idea of akrun is perfect and should work but it does not:

I assign at the beginning of the code the read_sheet(...) table to responses like:

df_id_read_sheet <- read_sheet("......")
responses <- df_id_read_sheet

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66
  • Lines 93:111 seem to go to effort to remove indexing by `id`, then 167:168 sez 3 fields, but has 2 in shiny app. Perhaps modify, comment out 93:111 to assess impact. – Chris Mar 26 '22 at 16:23
  • 1
    @TarJae Did you meant `DeleteData <- function(data) { if(nrow(responses) > 1) { responses <<- responses[-1, ] } else { responses <<- responses[row.names(responses) != unname(data["id"]), ] } }` – akrun Mar 26 '22 at 19:29
  • I have tried but same behavior. If no data is read in from google sheets everything is perfect. If I read in the data. Data is shown correct. But any how the navigation is not working because the disabled id field stays always at 1 . I will make a screen shot video and edit now. Thank you very much for your valuable time! – TarJae Mar 26 '22 at 19:42
  • 1
    Sorry, I didn't test it with googlesheets. It was working with the app without the google sheets – akrun Mar 26 '22 at 19:53

1 Answers1

1

After one night without sleep. I found the solution. And it was as simple as I thought. Following one of the first advice by dear @akrun I checked with str the structure of the loaded googlesheet.

The sheet is imported as tibble after transforming it to data.frame and assigning to responses the complete code worked as expected. The isssue arised because of the rownames.

Using browser() immediately after assigning the sheet to responses I was able to see and check the structure within the shiny app on my console.

I really love debuging with broswer() as it enables me to see what is going on in shiny apps also.

Here is the simple yet hard discovered solution: At the beginning of your shiny app code:

library(magritter)

df_id_read_sheet <- read_sheet("put your sheet id here")
responses <- df_id_read_sheet
responses %<>% 
  as.data.frame()
TarJae
  • 72,363
  • 6
  • 19
  • 66