這個問題與此有關:如何將列值從日期格式更改為自由流動的文本取決于 R 閃亮中的“結果”列
我想填寫一些輸入并將其添加到閃亮應用程式中的資料表中。
這是我的代碼:
基本上這段代碼有效,但前提是我有一個日期Date of the result / Remarks for fail
如果我在空白欄位中啟動應用程式Date of the result / Remarks for fail并按下ADD按鈕,應用程式將停止并給出錯誤:
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)
我怎樣才能修改它也適用于空日期欄位的代碼?
uj5u.com熱心網友回復:
您可以使用以下方法處理零長度條目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'),
#...
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/430473.html
上一篇:資料子集的ggplot2線圖
