0

I'm trying to overwrite some values in my dataframe. This code creates an empty dataframe

logs_data_frame <- data.frame(matrix(nrow = 1))
logs_data_frame$matrix.nrow...1. <- NULL

Then upon clicking a button, I make some columns where lots of them are assigned NA, but some get the value of their inputField:

observeEvent(input$bevestig, {
    logs_data_frame$session_id <- next_session_id_value + 1
    logs_data_frame$activity_type <- input$dropdown_type
    logs_data_frame$duration <- input$dropdown_duur
    logs_data_frame$intensity <- input$dropdown_intensiteit
    logs_data_frame$activity_start <- glue("{input$dropdown_gepland_uur}:{input$dropdown_gepland_min}:00")
    logs_data_frame$insuline_pump <- NA # Hoe deze opvragen zonder opslaan data frame instellingen
    logs_data_frame$previous_flow <- NA
    logs_data_frame$new_flow_number <- NA
    logs_data_frame$action_bolus <- NA
    logs_data_frame$action_basaal <- NA
    logs_data_frame$action_carbs <- NA
    logs_data_frame$recommendation_bolus <- NA
    logs_data_frame$recommendation_basaal <- NA
    logs_data_frame$recommendation_carbs <- NA
    logs_data_frame$date <- Sys.Date()
    logs_data_frame$pid <- as.numeric(str_sub(input$dropdown_participant, 5, nchar(input$dropdown_participant)))
    showNotification("Activiteit aangemaakt", type = "message")
    logs_data_frame[1] <- logs_data_frame
  })

Now this returns a dataframe with 1 row and the different columns

When I click another button, some values should change (glucose_reading, t_minus), but this just return a dataframe with column "glucose_reading" and 1 row: enter image description here code for this part:

observeEvent(input$showQuestions, {
    print(logs_data_frame)
    logs_data_frame[nrow(logs_data_frame), ] <- logs_data_frame
    logs_data_frame$glucose_reading[1] <- input$bg_current
    sub_times <- hms(chron(times = logs_data_frame$activity_start) - chron(times = glue("{input$hours_current}:{input$minutes_current}:00")))
    sub_times_to_minutes <- hour(sub_times) * 60 + minute(sub_times)
    logs_data_frame$t_minus[1] <- sub_times_to_minutes
    print(logs_data_frame)
  })

I also get the error: data frame with 0 columns and 1 row, which means that it is handling my dataframe just like it didn't exist?

Any help is much appreciated!

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
DJ Freeman
  • 399
  • 2
  • 14
  • Just checking before I delete the [python] tag... does this question have anything to do with Python? – Gregor Thomas Oct 03 '22 at 14:59
  • @GregorThomas Dataframes are also used in python and should be much alike, so I'm sure a python dev understands my problem – DJ Freeman Oct 03 '22 at 15:02
  • Yes, many languages have data frames, but they all behave a little differently. We use tags to narrow down the audience for questions. Please don't use a language tag unless the question involves code in that language. – Gregor Thomas Oct 03 '22 at 15:04
  • `logs_data_frame[nrow(logs_data_frame), ] <- logs_data_frame` seems like a mistake. It's assigning the whole data frame to the last row of the data frame. Probably would work okay as long as your data frame has 1 row, but will cause issues if it's ever bigger. And I'm not really sure what the point of it is... seems like it could be deleted. – Gregor Thomas Oct 03 '22 at 15:08
  • Is your data frame **always** one row? If so, there doesn't seem to be much point in it being a data frame at all, a `list` might be easier to work with. – Gregor Thomas Oct 03 '22 at 15:10
  • My Shiny is pretty rusty, but I think the issue may be that `logs_data_frame` isn't a global object. When you try to modify it inside your `observeEvent`, you're creating a brand new data frame with that name. Instead, you need to set it up as a reactive object and use `<<-` when you want to modify it. [This Q&A has a nice complete example](https://stackoverflow.com/a/65341796/903061). The section of the shiny docs [starting here](https://shiny.rstudio.com/articles/reactivity-overview.html) also seems like a good read: You've got an Observer, but Observers **don't** return values. – Gregor Thomas Oct 03 '22 at 15:20
  • @GregorThomas it will get bigger in later observeEvents. I already tried this with removing the line you just stated. But still same error that is why I tried to assign the dataframe to itself! I just tried countless things so don't blame me for trying this ;) . – DJ Freeman Oct 03 '22 at 15:33
  • I also made the variable global since it is at the top of my file, right above the server part – DJ Freeman Oct 03 '22 at 15:35
  • I'll try to use the reactiveVal function when I get back! Will let you know. Thanks in advance – DJ Freeman Oct 03 '22 at 15:43
  • No blame for trying things! – Gregor Thomas Oct 03 '22 at 15:43
  • I do think the issue is 100% Shiny related about the reactives and observers---all of your data frame code would work fine if it weren't in a shiny app. If you can't work it out easily, I would strongly suggest creating a minimal shiny app example and posting the code of the whole app. Create a data frame with one column, have a single field where the user adds one column, print the data frame. – Gregor Thomas Oct 03 '22 at 15:46
  • 1
    @GregorThomas I fixed it by doing <<- instead of <- . It also works with the reactive so thanks for the help! – DJ Freeman Oct 04 '22 at 09:03

0 Answers0