Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Arguments imply differing number of rows. data.frame() Error

This question is related to this: How to change column values from date format to free-flowing text is dependent on "result" column in R shiny

I want to fill some inputs and add it to a datatable within a shiny app.

This is my code:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Basically this code works, but only if I have a date in Date of the result / Remarks for fail

If I start the app with empty field in Date of the result / Remarks for fail and push the ADD button the app stops and gives the error:

Arguments imply differing number of rows. data.frame() Error
library(shiny)
library(tidyverse)
library(DT)
library(shinyjs)

ui <- fluidPage(
  shinyjs::useShinyjs(),
  fluidRow(tabsetPanel(id='tabs', 
                       tabPanel("Tab1",
                                div(id = "form", 
                                    textInput("schoolId", 
                                              label="SchoolId *"),
                                    selectInput("userId", 
                                                label="UserId", 
                                                choices = c("UserA", "UserB", "UserC"),
                                                selected = "UserA"), 
                                    textInput("class", 
                                              label = "class"), 
                                    selectInput("result", 
                                                label="result", 
                                                choices = c("PASS", "FAIL" )),
                                    dateInput("resultdate", 
                                              value = NA, 
                                              label = "Date of the result / Remarks for fail", 
                                              format = "yyyy-mm-dd"),
                                    textInput("remarks_fail", 
                                              label =  "Remarks for fail")
                                    ),
                                actionButton("add", "Add")
                                ), 
                       tabPanel("Tab2", 
                                tabPanel("View", 
                                         conditionalPanel("input.add != 0", 
                                                          DTOutput("DT2"), hr(), downloadButton('downloadData', 'Download'))
                                         )
                                )
                       )
           )
  )

server <- function(input, output, session) {
  store <- reactiveValues()
  
  observeEvent(input$result, {
    if(input$result == "FAIL"){
      shinyjs::disable("resultdate")
      shinyjs::enable("remarks_fail")
    } else {
      shinyjs::enable("resultdate")
      shinyjs::disable("remarks_fail")
    }
  })
  
  observeEvent(input$add,{
    new_entry <- data.frame(SCHOOLID=input$schoolId, 
                            USERID=input$userId, 
                            CLASS= input$class, 
                            RESULT=input$result,
                            RESULT_DATE = input$resultdate,
                            REMARKS_FAIL = input$remarks_fail)
    
    if("value" %in% names(store)){
      store$value<-bind_rows(store$value, new_entry)
    } else {
      store$value<-new_entry
    }
    
    # If you want to reset the field values after each entry use the following two lines
    for(textInputId in c("schoolId", "class")) updateTextInput(session, textInputId, value = "")
    updateSelectInput(session, "userId", selected = "UserA")
    updateSelectInput(session, "result", selected = "PASS")
    updateDateInput(session, "resultdate")
  })
  
  output$DT2 <- renderDT({
    store$value
  })
  
}

shinyApp(ui, server)

How could I modify the code that it works also with an empty date field?

>Solution :

You could handle zero-length entry with ifelse:

observeEvent(input$add,{

  new_entry <- data.frame(
                  SCHOOLID=input$schoolId,
                  USERID=input$userId,
                  CLASS= input$class,
                  RESULT=input$result,
                  RESULT_DATE = ifelse(length(input$date)==0,NA_character_,input$date),
                  REMARKS_FAIL = input$remarks_fail)
#...
}

Full code:

library(shiny)
library(tidyverse)
library(DT)
library(shinyjs)

ui <- fluidPage(
  shinyjs::useShinyjs(),
  fluidRow(tabsetPanel(id='tabs', 
                       tabPanel("Tab1",
                                div(id = "form", 
                                    textInput("schoolId", 
                                              label="SchoolId *"),
                                    selectInput("userId", 
                                                label="UserId", 
                                                choices = c("UserA", "UserB", "UserC"),
                                                selected = "UserA"), 
                                    textInput("class", 
                                              label = "class"), 
                                    selectInput("result", 
                                                label="result", 
                                                choices = c("PASS", "FAIL" )),
                                    dateInput("resultdate", 
                                              value = NA, 
                                              label = "Date of the result / Remarks for fail", 
                                              format = "yyyy-mm-dd"),
                                    textInput("remarks_fail", 
                                              label =  "Remarks for fail")
                                ),
                                actionButton("add", "Add")
                       ), 
                       tabPanel("Tab2", 
                                tabPanel("View", 
                                         conditionalPanel("input.add != 0", 
                                                          DTOutput("DT2"), hr(), downloadButton('downloadData', 'Download'))
                                )
                       )
  )
  )
)

server <- function(input, output, session) {
  store <- reactiveValues()
  
  observeEvent(input$result, {
    if(input$result == "FAIL"){
      shinyjs::disable("resultdate")
      shinyjs::enable("remarks_fail")
    } else {
      shinyjs::enable("resultdate")
      shinyjs::disable("remarks_fail")
    }
  })
  
  observeEvent(input$add,{
    new_entry <- data.frame(
      SCHOOLID=input$schoolId,
      USERID=input$userId,
      CLASS= input$class,
      RESULT=input$result,
      RESULT_DATE = ifelse(length(input$date)==0,NA_character_,input$date),
      REMARKS_FAIL = input$remarks_fail)
    
    if("value" %in% names(store)){
      store$value<-bind_rows(store$value, new_entry)
    } else {
      store$value<-new_entry
    }
    
    # If you want to reset the field values after each entry use the following two lines
    for(textInputId in c("schoolId", "class")) updateTextInput(session, textInputId, value = "")
    updateSelectInput(session, "userId", selected = "UserA")
    updateSelectInput(session, "result", selected = "PASS")
    updateDateInput(session, "resultdate")
  })
  
  output$DT2 <- renderDT({
    store$value
  })
  
}

shinyApp(ui, server)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading