Action Button to Add Data to Dataframe -- Warning: Error in data.frame: arguments imply differing number of rows: 0, 1

Hello Everyone,

I am fairly new to R and would love some assistance and some advice with a Shiny dashboard that allows clients to input attendance data.

I have recently began an RShiny dashboard that is meant to create a Client Inputted Attendance Tracking App. The goal of this app is for the client to enter the RShiny Dashboard and then select individuals who attend the event and the date in question (this way, the client can go back to previous days and add in attendance that was forgotten as an example). The data is then graphed in a Ggplot to visualize how frequently individuals attend events over time.

I have the following example data frames:

'''

 Playername <- c("Joe", "Fred", "Greg")
 Handedness <- c("L", "R", "L")

 Roster <- data.frame(Playername, Handedness)

 Playername2 <- c("Joe", "Fred")
 Date <- c("2021-10-18", "2021-10-19")

 Attendance <- data.frame(Playername2, Date)

'''

Below is a simplified version of the User Interface.

'''

 tweaks <- 
   list(tags$head(tags$style(HTML("
                             .multicol { 
                               height: 420px;
                               -webkit-column-count: 2; /* Chrome, Safari, Opera */ 
                               -moz-column-count: 2;    /* Firefox */ 
                               column-count: 2; 
                               -moz-column-fill: auto;
                               -column-fill: auto;
                             } 
                             ")) 
 ))

 controls <-
   list(#h3("Attendance"), 
        tags$div(align = 'left', 
                 class = 'multicol', 
                 checkboxGroupInput(inputId  = 'AttendanceInput', 
                                    label    = "Select Players", 
                                     choices  = c(Roster$Playername),
                                    inline   = FALSE)))


 ui <-navbarPage(title= "title",

 tabPanel(title = "ATTENDANCE",
         fluidPage(tweaks,
                   fluidRow(column(width = 4,
                                    dateInput("dateinputa", "Select Attendance Data", format = "yyyy/mm/dd"),
                                    (controls),
                                    actionButton("submit","SUBMIT")),
                       
              column(3,
                    h4("Filter Attendance Data", align = "center"),
                    box(collapsible = FALSE,
                       width= 12,
                       dateRangeInput("daterange", "Select Date Range",
                                      start = "2021-01-01",
                                      end = Sys.Date(),
                                      min = "2021-01-01",
                                      max = Sys.Date(),
                                      format = "yyyy/mm/dd",
                                      separator = "-"),
                   selectInput("Playernameinput", "Select Player", choices = Roster$Playername)
 ))), # closes column

 fluidRow(
     column(12,
       box(width=12,
       plotlyOutput(outputId = "plot", height = "500px") %>% withSpinner(type = 1, color = "#B62B34")),
   
   fluidRow(box(width= 12,
                dataTableOutput("AttendanceTable") %>% withSpinner(type = 1, color = "#B62B34"))
   )
 )) 

 )))

'''

Below is the Server function. The goal with the server is to observe the "submit button" event and 1) Add the inputted data into the attendance data frame, 2) write the data frame csv (so that it can be used in the future to save the attendance data - I think this is a good way to achieve this goal), 3) re-read the csv attendance data frame so that the ggolot can be updated with the new data.

Server:

'''

 server <- function(session, input, output) {

     v <- reactiveValues()
     v$AttendanceDF <- read_csv("Data/Source Data/Attendance.csv",
                            col_types = cols(Date = col_date(format = "%m/%d/%Y"), 
                                       Playername = col_character()))

   observeEvent(input$submit,{
         req(input$dateinputa,input$AttendanceInput)
         tmpDF <- data.frame(Date = input$dateinput,Playername = input$AttendanceInput)
         v$AttendanceDF <- rbind(v$AttendanceDF,tmpDF)
         write.csv(AttendanceDF, "Data/Source Data/Attendance.csv")
         v$AttendanceDF <- read_csv("Data/Source Data/Attendance.csv",
                               col_types = cols(Date = col_date(format = "%m/%d/%Y"), 
                                                Playername = col_character()))
  })

 output$AttendanceTable <- DT::renderDataTable({
    v$AttendanceDF
  })
}

'''

Based on the code I have written, I get the following error code: Warning: Error in data.frame: arguments imply differing number of rows: 0, 1

I would be very very appreciative for some help and advice with this situation. thank you so much for your time and knowledge.

is this a typo, and supposed to be input$dateinputa?

1 Like

My apologies. That is a typo. I meant to fix it, but must of not saved the edit. Thank you for your keen eye!

I was thinking to try and help you by debugging, but I'm afraid you've made it very awkward by how you have provided your set up. Put simply you've left it to us to figure what libraries you are dependent on

library(shiny)
library(shinycssloaders)
library(tidyverse)
library(plotly)

Then I tried to run the app. I have to add the shiny boiler plate shinyApp(ui, server)
ok not so bad, lets get into the app.

the app immediately errors

Warning: Error in : 'Data/Source Data/Attendance.csv' does not exist in current working directory ('C:/Users/NirGraham/Documents/R')

ok. this app assumes all sorts of things about paths on my local machine , I give up.

Here is a general Shiny debugging and reprex guide you might benefit from, and my advice is
a) use shiny::devmode() before running your app, as it often improved the error messages shiny gives you.
b) your error relates to data.frame construction so identify where in your code a data.frame might be being constructed, and use print()'s or browser()'s to snoop on the objects involved in the data.frame creation, they might be malformed / not what you expect.
Often in shiny an input isnt set and should be skipped with a silent failure by use of req() to avoid the bad cases.

Good luck !

1 Like

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.