【问题标题】:How to omit from showing in output empty results如何在输出空结果中省略显示
【发布时间】:2023-02-20 22:40:23
【问题描述】:

为用户创建了一个闪亮的应用程序来上传他们的时间序列数据帧并分别计算上传数据帧每一行中的异常值。

问题:在主面板中,输出显示每一行异常结果,但我只需要那些不是数字(0)的。仅显示具有离群值的那些以及出现离群值的数据帧行号。

ui <- fluidPage(

    titlePanel("Outlier analysis"),

    sidebarLayout(
        sidebarPanel(
            fileInput("Data",
                        "Upload file:")
        ),

        mainPanel(
          verbatimTextOutput("Outliers")
        )
    )
)


server <- function(input, output) {
  
      options(shiny.maxRequestSize=30*1024^2)
  
      mydata <- reactive({
        data <- input$Data
        data <- read_excel(data$datapath, col_names=FALSE)
        data <- data[-(1:2),]
        names(data) <- data[1,]
        data <- data[-1,]
        data <- data[,-1]
        data <- data.frame(data)
        })
      
      f <- function(data){
        x <- na.omit(as.numeric(data))
        mean <- mean(as.numeric(x), na.rm=T)
        sd <-   sd(as.numeric(x), na.rm=T)
        Tmin <- mean - (3*sd)
        Tmax <- mean + (3*sd)
        return(x[which(x < Tmin | x > Tmax)])
      }
      output$Outliers <- renderPrint({
        apply(mydata(), 1, f)
      })
}

输出:

[[1]]
numeric(0)

... 

[[30]]
numeric(0)

[[31]]
numeric(0)

[[32]]
numeric(0)

[[33]]
numeric(0)

[[34]]
numeric(0)

[[35]]
[1] 682.231 692.713 698.037 716.197 735.422

[[36]]
numeric(0)

...

期望的输出是这样的:

[[35]]
[1] 682.231 692.713 698.037 716.197 735.422

[[51]]
[1] 682.231 692.713 698.037 716.197 735.422

[[66]]
[1] 682.231 692.713 698.037 716.197 735.422

...

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    将您的 renderPrint 声明更改为以下内容。

    output$Outliers <- renderPrint({
      res <- apply(mydata(), 1, f)
      res[lengths(res) > 0]
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-30
      • 1970-01-01
      • 2013-03-15
      • 1970-01-01
      • 1970-01-01
      • 2022-12-10
      • 1970-01-01
      • 2015-09-06
      相关资源
      最近更新 更多