点击登录后(用户名:user1,密码=pass1)请点击左侧面板中的提交按钮。
这个闪亮的应用程序在登录后抛出错误,然后单击提交按钮错误消失,并且工作得很好。 对于当前日期,应用程序不会显示任何内容,对于任何其他日期,它会根据选定的过滤器提供来自 mtcars 数据集的聚合 mpg。
此错误可能是因为我将 null 与 if 语句中的值进行比较。
我不知道如何处理这个问题,并希望摆脱这个错误,因为它会分散用户的注意力。
library(shinydashboard)
library(shiny)
library(shinyauthr)
library(dplyr)
library(DT)
# dataframe that holds usernames, passwords and other user data
user_base <- tibble::tibble(
user = c("user1", "user2"),
password = sapply(c("pass1", "pass2"), sodium::password_store),
permissions = c("admin", "standard"),
name = c("User One", "User Two")
)
ui <- dashboardPage(
dashboardHeader(title = "Hi",tags$li(class = "dropdown", style = "padding: 8px;",
shinyauthr::logoutUI("logout"))),
dashboardSidebar(uiOutput("tarikh_1"),
uiOutput("cyls"),
submitButton("submit")),
dashboardBody(
shinyauthr::loginUI(id = "login"),
uiOutput("tabs")
))
server <- function(input,output,session){
data <- mtcars
output$tarikh_1 <- renderUI({
req(credentials()$user_auth)
dateInput("date1","Date_1",value = NULL)
})
output$cyls <- renderUI({
req(credentials()$user_auth)
selectInput("cyl","select cylinder",choices = c('4','6','8'),multiple = TRUE)
})
credentials <- shinyauthr::loginServer(
id = "login",
data = user_base,
user_col = user,
pwd_col = password,
sodium_hashed = TRUE,
log_out = reactive(logout_init())
)
# Logout to hide
logout_init <- shinyauthr::logoutServer(
id = "logout",
active = reactive(credentials()$user_auth)
)
table_1 <- reactive({
if(input$date1== Sys.Date()){
return()
}else{
f <- input$cyl
grps <- c('cyl')[c(!is.null(f))]
print(grps)
grps <- paste(grps,collapse = ",")
print(grps)
if(grps ==""){
data %>% summarise(Average_mileage = sum(mpg))
}else{
print(f)
if(is.null(f)) f <- c("4","6","8")
data %>% filter(cyl %in% f) %>% group_by(cyl) %>% summarise(Average_mileage = sum(mpg))
}}})
output$non_bin <- renderDataTable({
table_1()
})
output$tabs <- renderUI({
req(credentials()$user_auth)
tabsetPanel(type = "tab",
tabPanel("Aggregate_calculation",dataTableOutput("non_bin")))
})}
shiny::shinyApp(ui, server)
要处理 R Shiny 应用程序中的“长度 0”错误,请在处理之前检查您正在使用的对象或数据是否为空。使用 if (length(object) > 0) 等条件语句来确保对象具有数据。或者,Shiny 中的 req() 函数可以通过暂停反应式表达式直到满足某些条件(例如非零长度)来提供帮助,从而防止空数据错误。