R noob
R noob

Reputation: 513

Omit Empty Fileds on HTML output shiny app

I am working on a shiny app that outputs text in HTML. From my data some of the fields are empty/null. I am trying to find ways to omit the empty variables in the output. Below is an example of my shinyapp with some data missing:

library(shiny)
library(shinydashboard)
library(sqldf)

    missingdata=data.frame(
               id=c("001","002","003","004"),
               age=c("17",NA, "19", NA),
               bmi=c(NA, "16", "20",  NA),
              chol=c("49.8", "50.7", "52.0", "56.5")
              )


    ui <- dashboardPage(
           dashboardHeader(),
           dashboardSidebar(
             sidebarMenu(
               menuItem("TexSearch", tabName = "Tabs", icon = icon("object-ungroup"))) ),


       dashboardBody(
            tabItem(tabName = "Tabs",
               fluidRow(
                 column(width=3, 
                 box(
                   title="Search ",
                   solidHeader=TRUE,
                   collapsible=TRUE,
                   width=NULL
                   )),

          column( width=9,
                  tabBox(
                    width="100%",
                    tabPanel("tab1", 
                             htmlOutput("searchtext")
                    )))))))

 server <- function(input, output, session) {
     output$searchtext <- renderUI({

     searchdata <- reactive({
      sqldf(paste0("SELECT  *
                   FROM missingdata
                  "))
      }) 


    output = ""
     relevantdata <- searchdata()
      if (dim(relevantdata)[1] < 100) {
        output <-
         paste(output,
            "<b>Number of quotes: ",
            as.character(dim(relevantdata)[1]),
            "</b>.<br/>")
         for (i in seq(from = 1,
                  to = dim(relevantdata)[1])) {
         output <- paste(output,
                      paste("id: ", relevantdata[i, "id"]),
                      sep = "<br/><br/>")

        output <- paste(output,
                        paste("age: ", relevantdata[i, "age"]),
                        sep = "<br/><br/>")

      output <- paste(output,
                      paste("bmi: ", relevantdata[i, "bmi"]),
                      sep = "<br/><br/>")
      output <- paste(output,
                      paste("chol: ", relevantdata[i, "chol"]),
                      sep = "<br/><br/>")
          }

          }
       HTML(output)

      })

      }
shinyApp(ui, server)

How would I avoid printing the age and bmi fields when it is NA and only output them when they have values?

What I tried without luck is as below:

if (relevantdata$age == "NA"){
    return(NULL)
   }else{  
    output <- paste(output,
              paste("age: ", relevantdata[i, "age"]),
              sep = "<br/><br/>")
   }

And also the snippet below which resulted in no output at all:

   ifelse(is.na(relevantdata$age), return(NULL), output <- paste(output,
                                                          paste("age: ", relevantdata[i, "age"]),
                                                          sep = "<br/><br/>")) 

Upvotes: 0

Views: 52

Answers (1)

camnesia
camnesia

Reputation: 2323

You need to check if the current value i in the for loop is NA and not the whole column relevantdata$age.

ifelse(!is.na(relevantdata[i, "age"]),output <- paste(output, paste("age: ", relevantdata[i, "age"]),sep = "<br/><br/>") , NA)

Upvotes: 1

Related Questions