1

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:

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?

1 Answer 1

2

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 =as.Date(ifelse(length(input$resultdate)==0,NA_character_,input$resultdate),origin='1970-01-01'),
#...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.