Using double eval= before executing a code chunk

I want to display 2nd table with or without footnote depending on 1 flag: ftnt_disp or ftnt_hide. This triggered by the first table that must have data (2nd table would have data if first table has data).

3 steps of process

  1. set up first logical flag run_ecqm: run_ecqm=TRUE if first table ehosp has data and 2nd table has data for sure.
    All code chunks from here below are evaluate run_eqcm before executing. if run_ecqm==TRUE, new page created; table title and contents displayed; otherwise, nothing would happen!

  2. set up 2 logical flags: ftnt_disp and ftnt_hide based on first table has data and 2nd table variable rfi_ind_id=='16692'; if condition exist, ftnt_disp == TRUE display footnote; otherwise ftnt_hide == TURE display without footnote;

  3. prepare data and display table:
    if run_ecqm==TRUE and ftnt_disp==TRUE and ftnt_hide==FALSE, table displayed with footnote
    if run_ecqm==TRUE and ftnt_hide==TRUE and ftnt_disp==FALSE, table displayed without footnote
    if run_ecqm==FALSE, then ftnt_disp==FALSE and ftnt_hide==FALSE, nothing should happen

In the codes below, it works fine if first table has data, but doesn't work if first table no data - it seems code chunks ignoring the evaluation, and get into chunk and failed due to table or variable missing...Error message: "Error in if (bold) { : missing value where TRUE/FALSE needed". Suggestion? Thanks in advance!

---
title: ''
header-includes: 
- \usepackage{fancyhdr}
- \usepackage{comment}
- \usepackage{float}
output:
  pdf_document: 
  latex_engine: xelatex
classoption: landscape
---

```{r prelim, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)
knitr::opts_chunk$set(warning = FALSE)
knitr::opts_chunk$set(message = FALSE)

library(gridExtra)
library(tinytex)
library(knitr)
library(kableExtra)
run_ecqm <- FALSE
ftnt_disp <- FALSE
ftnt_hide <- FALSE
selhco <- "4754"
bname <- "Test"
title2 <- "Testing double  eval= logic on empty datasets"

ehosp <- data.frame(hco_id=character(),
rfi_ind_id=character(),
type=character()
)

ehospsub <- data.frame(hco_id=character(),
rfi_ind_id=character(),
pctle_char=character()
)

# set up first levle flag - CAN'T FIND ehosp dataset???
if(nrow(ehosp) != 0){run_ecqm <- TRUE} else {run_ecqm <- FALSE}
cat("\n\n\\pagebreak\n")

r if(!run_ecqm) {"\\begin{comment}"}
HCO ID r selhco: r bname
r title2
r if(!run_ecqm) {"\\end{comment}"}

# set up 2nd level flags
if(nrow(ehospsub)>0) {
  epc2 <- ehospsub %>% filter(rfi_ind_id=='16692')
  if(nrow(epc2) > 0)
  { ftnt_disp <- TRUE
    ftnt_hide <- FALSE
  } else if(nrow(epc2) == 0){
    ftnt_disp <- FALSE
    ftnt_hide <- TRUE
  } }else {
    ftnt_disp <- FALSE
    ftnt_hide <- FALSE
  }
ehospsubt <- ehospsub %>%
  select(
  "Name"=hco_id, 
  "MID"=rfi_ind_id, 
  "Percentile"=pctle_char) 
# IGNORED 2 FLAGS AND GET INTO - flag set up correct!
ehospsubt %>%
  kable(digits=1,"latex") %>% 
  kable_styling('bordered', position = "left") %>%
  row_spec(0, bold=TRUE) %>% 
  column_spec(1, width="11cm",border_left = TRUE) %>% 
  column_spec(2, width="1.8cm") %>%
  column_spec(3,width="1.8cm",border_right = TRUE) %>% 
  footnote(
           general = "NA * indicates percentile not applicable ",
           general_title = "Note: ", 
           footnote_as_chunk = T, title_format = c("italic", "bold")
           )
ehospsubt <- ehospsub %>%
  select(
  "Name"=hco_id, 
  "MID"=rfi_ind_id, 
  "Percentile"=pctle_char) 
ehospsubt %>%
  kable(digits=1,"latex") %>% 
  kable_styling('bordered', position = "left") %>%
  row_spec(0, bold=TRUE) %>% 
  column_spec(1, width="11cm",border_left = TRUE) %>% 
  column_spec(2, width="1.8cm") %>%
  column_spec(3,width="1.8cm",border_right = TRUE) 

This is correct codes - Sorry!


title: ''
header-includes:

  • \usepackage{fancyhdr}
  • \addtolength{\headheight}{.8cm}
  • \pagestyle{fancyplain}
  • \renewcommand{\headrulewidth}{0pt}
  • \usepackage{comment}
  • \usepackage{float}
    output:
    pdf_document:
    latex_engine: xelatex
    classoption: landscape

knitr::opts_chunk$set(echo = FALSE)
knitr::opts_chunk$set(warning = FALSE)
knitr::opts_chunk$set(message = FALSE)

library(gridExtra)
library(stringr)
library(tinytex)
library(knitr)
library(kableExtra)
run_ecqm <- FALSE
ftnt_disp <- FALSE
ftnt_hide <- FALSE
selhco <- "4754"
bname <- "Test"
title2 <- "Test double eval logic on empty datasets"

ehosp <- data.frame(hco_id=character(),
rfi_ind_id=character(),
type=character()
)

ehospsub <- data.frame(hco_id=character(),
rfi_ind_id=character(),
pctle_char=character()
)

# set up first levle flag
if(nrow(ehosp) != 0){run_ecqm <- TRUE} else {run_ecqm <- FALSE}
cat("\n\n\\pagebreak\n")

r if(!run_ecqm) {"\\begin{comment}"}
HCO ID r selhco: r bname
r title2
r if(!run_ecqm) {"\\end{comment}"}

# set up 2nd level flags
if(nrow(ehospsub)>0) {
  epc2 <- ehospsub %>% filter(rfi_ind_id=='16692')
  if(nrow(epc2) > 0)
  { ftnt_disp <- TRUE
    ftnt_hide <- FALSE
  } else if(nrow(epc2) == 0){
    ftnt_disp <- FALSE
    ftnt_hide <- TRUE
  } }else {
    ftnt_disp <- FALSE
    ftnt_hide <- FALSE
  }
ehospsubt <- ehospsub %>%
  select(
  "Name"=hco_id, 
  "MID"=rfi_ind_id, 
  "Percentile"=pctle_char) 
# IGNORED 2 FLAGS AND GET INTO - flag set up correct!
ehospsubt %>%
  kable(digits=1,"latex") %>% 
  kable_styling('bordered', position = "left") %>%
  row_spec(0, bold=TRUE) %>% 
  column_spec(1, width="11cm",border_left = TRUE) %>% 
  column_spec(2, width="1.8cm") %>%
  column_spec(3,width="1.8cm",border_right = TRUE) %>% 
  footnote(
           general = "NA * indicates percentile not applicable ",
           general_title = "Note: ", 
           footnote_as_chunk = T, title_format = c("italic", "bold")
           )
ehospsubt <- ehospsub %>%
  select(
  "Name"=hco_id, 
  "MID"=rfi_ind_id, 
  "Percentile"=pctle_char) 
ehospsubt %>%
  kable(digits=1,"latex") %>% 
  kable_styling('bordered', position = "left") %>%
  row_spec(0, bold=TRUE) %>% 
  column_spec(1, width="11cm",border_left = TRUE) %>% 
  column_spec(2, width="1.8cm") %>%
  column_spec(3,width="1.8cm",border_right = TRUE) 

I want to display 2nd table with or without footnote depending on 1 flags: ftnt_disp or ftnt_hide. This triggered by the first table that must have data (2nd table would have data if first table has data).
3 steps of process

  1. set up first logical flag run_ecqm: run_ecqm=TRUE if first table ehosp has data and 2nd table has data for sure.
    All code chunks below are evaluate run_eqcm before executing. if run_ecqm=TRUE, new page created; table title and contents displayed; otherwise, nothing should happen!
  2. set up 2 logical flags: ftnt_disp and ftnt_hide based on first table has data and 2nd table variable rfi_ind_id=='16692'; if condition exist, ftnt_disp == TRUE display footnote; otherwise ftnt_hide == TURE display without footnote;
  3. prepare data and display table:
    if run_ecqm==TRUE and ftnt_disp==TRUE, table displayed with footnote
    if run_ecqm==TRUE and ftnt_hide==TRUE, table displayed without footnote
    if run_ecqm==FALSE, then ftnt_disp==FALSE and ftnt_hide==FALSE, nothing should happen

In the codes below, it works fine if first table has data, but doesn't work if there is no rows in first table - it seems code chunks ignoring the evaluation, and get into chunk and
failed due to table or variable missing...Error message: "Error in if (bold) { : missing value where TRUE/FALSE needed". Suggestion? Thanks in advance!


title: ''
header-includes:

  • \usepackage{fancyhdr}
  • \addtolength{\headheight}{.8cm}
  • \pagestyle{fancyplain}
  • \renewcommand{\headrulewidth}{0pt}
  • \usepackage{comment}
  • \usepackage{float}

geometry: margin=2cm
output:
pdf_document:
latex_engine: xelatex
classoption: landscape

knitr::opts_chunk$set(echo = FALSE)
knitr::opts_chunk$set(warning = FALSE)
knitr::opts_chunk$set(message = FALSE)
library(tidyverse)
library(gridExtra)
library(stringr)
library(tinytex)
library(knitr)
library(kableExtra)
run_ecqm <- FALSE
ftnt_disp <- FALSE
ftnt_hide <- FALSE
selhco <- "4754"
bname <- "Test"
title2 <- "Test double eval logic on empty datasets"

ehosp <- data.frame(hco_id=character(), 
                        rfi_ind_id=character(),
                        type=character()
                        )  

ehospsub <- data.frame(hco_id=character(), 
                        rfi_ind_id=character(),
                        pctle_char=character()
)
# set up first levle flag
if(nrow(ehosp) != 0){run_ecqm <- TRUE} else {run_ecqm <- FALSE}
cat("\n\n\\pagebreak\n")

r if(!run_ecqm) {"\\begin{comment}"}
HCO ID r selhco: r bname
r title2
r if(!run_ecqm) {"\\end{comment}"}

# set up 2nd level flags
if(nrow(ehospsub)>0) {
  epc2 <- ehospsub %>% filter(rfi_ind_id=='16692')
  if(nrow(epc2) > 0)
  { ftnt_disp <- TRUE
    ftnt_hide <- FALSE
  } else if(nrow(epc2) == 0){
    ftnt_disp <- FALSE
    ftnt_hide <- TRUE
  } }else {
    ftnt_disp <- FALSE
    ftnt_hide <- FALSE
  }
ehospsubt <- ehospsub %>%
  select(
  "Name"=hco_id, 
  "MID"=rfi_ind_id, 
  "Percentile"=pctle_char) 
# IGNORED 2 FLAGS AND GET INTO - flag set up correct!
ehospsubt %>%
  kable(digits=1,"latex") %>% 
  kable_styling('bordered', position = "left") %>%
  row_spec(0, bold=TRUE) %>% 
  column_spec(1, width="11cm",border_left = TRUE) %>% 
  column_spec(2, width="1.8cm") %>%
  column_spec(3,width="1.8cm",border_right = TRUE) %>% 
  footnote(
           general = "NA * indicates percentile not applicable ",
           general_title = "Note: ", 
           footnote_as_chunk = T, title_format = c("italic", "bold")
           )
ehospsubt <- ehospsub %>%
  select(
  "Name"=hco_id, 
  "MID"=rfi_ind_id, 
  "Percentile"=pctle_char) 
ehospsubt %>%
  kable(digits=1,"latex") %>% 
  kable_styling('bordered', position = "left") %>%
  row_spec(0, bold=TRUE) %>% 
  column_spec(1, width="11cm",border_left = TRUE) %>% 
  column_spec(2, width="1.8cm") %>%
  column_spec(3,width="1.8cm",border_right = TRUE)