remove columns with nas condition

I have a problem with my shiny code, I want to make a conditional that tells me the na of each column of a dataframe and that shows me the columns that meet the condition that the na are greater than 30 percent of the total rows

ui <- dashboardPage(
  dashboardHeader(title = titulo, titleWidth = 250, disable= F
  ),#header
  
  dashboardSidebar(width = 250, disable = F,
                   fluidPage( 
                     sidebarMenu( 
                       
                       menuItem("Carga",tabName = "carga", icon = icon("upload")),
                       menuItem("Selección",tabName = "selec")
                                
                     )#sidebarMenu
                   )# fluidPage     
  ), #dashboardSidebar

  dashboardBody(
    fluidPage(
      tabItems(
        
       
        tabItem(tabName = "carga",
                           tabPanel(strong("Ingreso"),
                                    h3("Carga de Datos"),
                                    tags$hr(),
                                    fluidRow(
                                      column(5,
                                             selectInput("separador",
                                                         h4("Separador"),
                                                         choices = list(" , " = ",",
                                                                        " ; " = ";",
                                                                        "Seleccione una opción"= 3
                                                         ),
                                                         selected = 3
                                             ),
                                             # tags$hr(),
                                             checkboxInput("header", "Encabezado", TRUE),
                                             selectInput("codificar",
                                                         h4("Elegir codificación"),
                                                         choices = list("Seleccione una opción"= 3, 
                                                                        " ISO-8859-1 " = "ISO-8859-1",
                                                                        " UTF-8 " = "UTF-8",
                                                                        " ASCII " = "ASCII",
                                                                        "  BIG5" = "BIG5",
                                                                        " GB18030 " = "GB18030",
                                                                        " GB2312 " = "GB2312",
                                                                        " ISO-2022-JP " = "ISO-2022-JP",
                                                                        " ISO-2022-KR " = "ISO-2022-KR",
                                                                        " ISO-8859-2 " = "ISO-8859-2",
                                                                        " ISO-8859-7 " = "ISO-8859-7",
                                                                        " SHIFT-JIS " = "SHIFT-JIS",
                                                                        " WINDOWS-1252 " = "WINDOWS-1252"
                                                         ),
                                                         selected = 3
                                             )
                                      ),#column
                                      column(5,
                                             fileInput("fichero",
                                                       h4("Archivo"),
                                                       buttonLabel = "Fichero",
                                                       placeholder = "selecciona un fichero",
                                                       accept = c(
                                                         "text/csv",
                                                         "text/comma-separated-values,text/plain",
                                                         ".csv"
                                                       ),
                                                       multiple = F)
                                      )#column
                                    )#fluidrown
                           ) #tabpanel ingreso
            
        ),#carga
        
        tabItem(tabName = "selec",
               
                           tabPanel(strong("Tabla"),
                                    sidebarLayout(
                                      sidebarPanel(
                                        uiOutput("uiSeleccion"),
        
                                      ),
                                      mainPanel(
                                        br(),
                                        box( height = "500",width = "15",solidHeader = T,
                                             dataTableOutput("tabla1"),style = "height:500px; overflow-y: scroll;overflow-x: scroll;"
                                        )#BOX
                                      )#mainPanel
                                    )#sidebarLayout
                           )#tabpanel seleccion
                           
               
        )#tabItem seleccion
      )#tabItems
    )#fluidpage
  )#dashboardBody
)#dashboardPage

server <- function(input, output, session) {
  
  ####-------------RENDER DE FILEINPUT CARGA ---------------------
  
  archivo1 <- reactive({
    file <- input$fichero
    if (is.null(file)|| input$separador == 3)
      return(NULL)
    archivo <- read.csv(file$datapath, 
                        sep = input$separador,
                        header = input$header,
                        encoding = input$codificar,
                        na = c("", "NA"),
                        stringsAsFactors = F
    )
    
  })
  

  ####------------ RENDER DE SELECCION-----------------------------------
  ####------------ render seleccion checkbox
  #faq-discussion 
  output$uiSeleccion <- renderUI({
   
    checkboxGroupInput("variables", "Columnas del dataset para  mostrar:",
                       names(archivo1()), selected = names(archivo1()))
 
  })
  
  ####------------ render archivo de seleccion
  
  archivo2 <- reactive({
    archivo1()[ ,input$variables, drop = F 
                ]})
  
  ####------------ render tabla de seleccion
  
  output$tabla1 <- renderDataTable({
    
    DT::datatable(archivo2())
  })
}

Where is the problem?

Your issue is not specific to shiny it can be addressed outside of a Shiny App context, see this example (BTW you should make your questions in the form of a REPRoducible EXample (reprex) as the one bellow).

library(dplyr)

sample_data <- head(iris)
sample_data$Sepal.Length <- NA

sample_data
#>   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1           NA         3.5          1.4         0.2  setosa
#> 2           NA         3.0          1.4         0.2  setosa
#> 3           NA         3.2          1.3         0.2  setosa
#> 4           NA         3.1          1.5         0.2  setosa
#> 5           NA         3.6          1.4         0.2  setosa
#> 6           NA         3.9          1.7         0.4  setosa

# Filter columns containing only NAs
sample_data %>% 
    select_if(~!all(is.na(.))) %>% 
    names()
#> [1] "Sepal.Width"  "Petal.Length" "Petal.Width"  "Species"

Created on 2020-01-24 by the reprex package (v0.3.0.9000)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.