Setup

Sys.setenv(JAVA_HOME = "/Library/Java/JavaVirtualMachines/temurin-11.jdk/Contents/Home")
options(java.parameters = "-Xmx16000m")
knitr::opts_chunk$set(
  root.dir = normalizePath("./"),
  echo = TRUE,
  message = TRUE,
  warning = FALSE,
  error = TRUE
)
xfun::pkg_load2("pdftools", "tabulapdf", "vroom", "tidyverse", "magrittr", "rJava", "shiny")
library(hablar)
library(here)
library(rmarkdown)
library(rmdformats)
library(tibble)
library(rlang)
library(bwu)

Patient

patient <- params$patient

Test

test <- params$test

Test Name

test_name <- params$test_name

Path

It appears you're mixing up two tasks: creating a file path and reading/writing to a file. The here::here() function is fantastic for creating paths that are relative to your project directory, ensuring reproducibility across different systems.

Let me break down the tasks and show you how to handle each:

1. Creating a File Path:

To create a file path using the here::here() function, you would do something like this:

file_path <- here::here("pdf", "pai.pdf")

This creates a file path that joins your project directory, the value stored in patient, "pre_csv", and "pai.pdf".

3. Reading from a File:

The readLines() function reads text lines from a file. The primary argument is the file path. So, to read from the file we wrote to in the previous step, you'd do:

read_content <- readLines(file_path)

Bringing it all together, using the two chunks you provided:

file_path <- here::here("pdf", "pai.pdf")
content <- "This is some sample content." # replace with your actual content
file_path <- here::here("pdf", "pai.pdf")
read_content <- readLines(file_path)

Remember to replace the placeholder content with your actual content. Additionally, ensure that the patient object exists and has been properly defined before these chunks are executed.

Upload/attach PDF

# file <- file.choose()
file <- file.path(params$file)

Export PAI CSV file from PARiConnect

Need to export csv file from PARiConnect before processing. "Export Client Data" tab, then click "Univ of Southern Cali" then "PAI". Specify date range to exclude entire database.

Import PAI CSV file from PARiConnect

Copy over to pre_csv.

plucked_table <-
  readr::read_csv(
    here::here("pre_csv", "pariconnectexportPAI.csv")
  )

Clinical and validity scales

Subset and rename columns

df_clinical <- plucked_table |>
  dplyr::select(
    SOM_T:AGG_P_T, ALC_Est, DRG_Est
  ) |>
  tidyr::gather() |>
  dplyr::rename(scale = key, score = value)

df_validity <- plucked_table |>
  dplyr::select(
    ICN_T:PIM_T,
    MAL_T:HRI_T
  ) |>
  tidyr::gather() |>
  dplyr::rename(scale = key, score = value)

df_inatt <- plucked_table |>
  dplyr::select(IINATTN_T) |>
  tidyr::gather() |>
  dplyr::rename(scale = key, score = value)

adhd_items <- c(
  "R13",
  "R15",
  "R18",
  "R21",
  "R34",
  "R57",
  "R94",
  "R112",
  "R139",
  "R167",
  "R169",
  "R171",
  "R195",
  "R198",
  "R201",
  "R213",
  "R233",
  "R236",
  "R254",
  "R282",
  "R299",
  "R311",
  "R322",
  "R325"
)

df_adhd <- plucked_table |>
  dplyr::select(adhd_items) |>
  tidyr::gather() |>
  dplyr::rename(scale = key, score = value)

Convert T scores to z scores to percentiles

df_clinical <- df_clinical |>
  dplyr::mutate(z = (score - 50) / 10) |>
  dplyr::mutate(percentile = trunc(pnorm(z) * 100)) |>
  dplyr::select(scale, score, percentile)

df_validity <- df_validity |>
  dplyr::mutate(z = (score - 50) / 10) |>
  dplyr::mutate(percentile = trunc(pnorm(z) * 100)) |>
  dplyr::select(scale, score, percentile)

df_inatt <- df_inatt |>
  dplyr::mutate(z = (score - 50) / 10) |>
  dplyr::mutate(percentile = trunc(pnorm(z) * 100)) |>
  dplyr::select(scale, score, percentile)

Change scale names

# Clinical Scales
df_clinical[1, 1] <- c("Somatic Complaints")
df_clinical[2, 1] <- c("Anxiety")
df_clinical[3, 1] <- c("Anxiety-Related Disorders")
df_clinical[4, 1] <- c("Depression")
df_clinical[5, 1] <- c("Mania")
df_clinical[6, 1] <- c("Paranoia")
df_clinical[7, 1] <- c("Schizophrenia")
df_clinical[8, 1] <- c("Borderline Features")
df_clinical[9, 1] <- c("Antisocial Features")
df_clinical[10, 1] <- c("Alcohol Problems")
df_clinical[11, 1] <- c("Drug Problems")
# Treatment consideration scales
df_clinical[12, 1] <- c("Aggression")
df_clinical[13, 1] <- c("Suicidal Ideation")
df_clinical[14, 1] <- c("Stress")
df_clinical[15, 1] <- c("Nonsupport")
df_clinical[16, 1] <- c("Treatment Rejection")
# Interpersonal scales
df_clinical[17, 1] <- c("Dominance")
df_clinical[18, 1] <- c("Warmth")
# Clinical subscales
df_clinical[19, 1] <- c("Conversion")
df_clinical[20, 1] <- c("Somatization")
df_clinical[21, 1] <- c("Health Concerns")
df_clinical[22, 1] <- c("Cognitive (A)")
df_clinical[23, 1] <- c("Affective (A)")
df_clinical[24, 1] <- c("Physiological (A)")
df_clinical[25, 1] <- c("Obsessive-Compulsive")
df_clinical[26, 1] <- c("Phobias")
df_clinical[27, 1] <- c("Traumatic Stress")
df_clinical[28, 1] <- c("Cognitive (D)")
df_clinical[29, 1] <- c("Affective (D)")
df_clinical[30, 1] <- c("Physiological (D)")
df_clinical[31, 1] <- c("Activity Level")
df_clinical[32, 1] <- c("Grandiosity")
df_clinical[33, 1] <- c("Irritability")
df_clinical[34, 1] <- c("Hypervigilance")
df_clinical[35, 1] <- c("Persecution")
df_clinical[36, 1] <- c("Resentment")
df_clinical[37, 1] <- c("Psychotic Experiences")
df_clinical[38, 1] <- c("Social Detachment")
df_clinical[39, 1] <- c("Thought Disorder")
df_clinical[40, 1] <- c("Affective Instability")
df_clinical[41, 1] <- c("Identity Problems")
df_clinical[42, 1] <- c("Negative Relationships")
df_clinical[43, 1] <- c("Self-Harm")
df_clinical[44, 1] <- c("Antisocial Behaviors")
df_clinical[45, 1] <- c("Egocentricity")
df_clinical[46, 1] <- c("Stimulus-Seeking")
df_clinical[47, 1] <- c("Aggressive Attitude")
df_clinical[48, 1] <- c("Verbal Aggression")
df_clinical[49, 1] <- c("Physical Aggression")
# Est Drug/Alc
df_clinical[50, 1] <- c("ALC Estimated Score")
df_clinical[51, 1] <- c("DRG Estimated Score")

# Validity Scales
df_validity[1, 1] <- c("Inconsistency")
df_validity[2, 1] <- c("Infrequency")
df_validity[3, 1] <- c("Negative Impression Management")
df_validity[4, 1] <- c("Positive Impression Management")
# Negative Distortion Indicators
df_validity[5, 1] <- c("Malingering Index")
df_validity[6, 1] <- c("Rogers Discriminant Function")
df_validity[7, 1] <- c("Negative Distortion Scale")
df_validity[8, 1] <- c("Hong Malingering Index")
df_validity[9, 1] <- c("Multiscale Feigning Index")
df_validity[10, 1] <- c("Malingered Pain-Related Disability Discriminant Function")
# Positive Distortion Indicators
df_validity[11, 1] <- c("Defensiveness Index")
df_validity[12, 1] <- c("Cashel Discriminant Function")
df_validity[13, 1] <- c("Positive Distortion Scale")
df_validity[14, 1] <- c("Hong Defensiveness Index")
# Non-systematic Distortion Indicators
df_validity[15, 1] <- c("Back Random Responding")
df_validity[16, 1] <- c("Hong Randomness Index")

# adhd
df_inatt[1, 1] <- c("Inattention (INATTN) Index")

Filter, group, select

df_clinical <- df_clinical |> dplyr::slice(
  c(
    1, 19:21,
    2, 22:24,
    3, 25:27,
    4, 28:30,
    5, 31:33,
    6, 34:36,
    7, 37:39,
    8, 40:43,
    9, 44:46,
    12, 47:49,
    10:11, 50:51,
    13:18
  )
)

Add/mutate columns

df_clinical <-
  bwu::gpluck_make_columns(
    data = df_clinical,
    raw_score = "",
    range = "",
    ci_95 = "",
    test = params$test,
    test_name = params$test_name,
    domain = "",
    subdomain = "",
    narrow = "",
    pass = "",
    verbal = "",
    timed = "",
    test_type = "rating_scale",
    score_type = "t_score",
    absort = paste0(
      tolower(test), "_", tolower(scale), "_", seq_len(nrow(df_clinical))
    ),
    description = "",
    result = ""
  )

df_validity <-
  bwu::gpluck_make_columns(
    data = df_validity,
    raw_score = "",
    range = "",
    ci_95 = "",
    test = params$test,
    test_name = params$test_name,
    domain = "Symptom Validity",
    subdomain = "",
    narrow = "",
    pass = "",
    verbal = "",
    timed = "",
    test_type = "symptom_validity",
    score_type = "t_score",
    absort = paste0(
      tolower(test), "_", tolower(scale), "_", seq_len(nrow(df_validity))
    ),
    description = "",
    result = ""
  )

df_inatt <-
  bwu::gpluck_make_columns(
    data = df_inatt,
    raw_score = "",
    range = "",
    ci_95 = "",
    test = params$test,
    test_name = params$test_name,
    domain = "ADHD",
    subdomain = "Attention Problems",
    narrow = "Inattention",
    pass = "",
    verbal = "",
    timed = "",
    test_type = "rating_scale",
    score_type = "t_score",
    absort = paste0(
      tolower(test), "_", tolower(scale), "_", seq_len(nrow(df_inatt))
    ),
    description = "",
    result = ""
  )

Rating Scale score ranges

df_clinical <- bwu::gpluck_make_score_ranges(
  table = df_clinical,
  test_type = "rating_scale"
)
df_validity <- bwu::gpluck_make_score_ranges(
  table = df_validity,
  test_type = "symptom_validity"
)
df_inatt <- bwu::gpluck_make_score_ranges(
  table = df_inatt,
  test_type = "rating_scale"
)

Domains

df_clinical <-
  df_clinical |>
  dplyr::mutate(
    domain = dplyr::case_when(
      scale == "Somatic Complaints" ~ "Psychiatric Disorders",
      scale == "Conversion" ~ "Psychiatric Disorders",
      scale == "Somatization" ~ "Psychiatric Disorders",
      scale == "Health Concerns" ~ "Psychiatric Disorders",
      scale == "Anxiety" ~ "Psychiatric Disorders",
      scale == "Cognitive (A)" ~ "Psychiatric Disorders",
      scale == "Affective (A)" ~ "Psychiatric Disorders",
      scale == "Physiological (A)" ~ "Psychiatric Disorders",
      scale == "Anxiety-Related Disorders" ~ "Psychiatric Disorders",
      scale == "Obsessive-Compulsive" ~ "Psychiatric Disorders",
      scale == "Phobias" ~ "Psychiatric Disorders",
      scale == "Traumatic Stress" ~ "Psychiatric Disorders",
      scale == "Depression" ~ "Psychiatric Disorders",
      scale == "Cognitive (D)" ~ "Psychiatric Disorders",
      scale == "Affective (D)" ~ "Psychiatric Disorders",
      scale == "Physiological (D)" ~ "Psychiatric Disorders",
      scale == "Mania" ~ "Psychiatric Disorders",
      scale == "Activity Level" ~ "Psychiatric Disorders",
      scale == "Grandiosity" ~ "Psychiatric Disorders",
      scale == "Irritability" ~ "Psychiatric Disorders",
      scale == "Paranoia" ~ "Psychiatric Disorders",
      scale == "Hypervigilance" ~ "Psychiatric Disorders",
      scale == "Persecution" ~ "Psychiatric Disorders",
      scale == "Resentment" ~ "Psychiatric Disorders",
      scale == "Schizophrenia" ~ "Psychiatric Disorders",
      scale == "Psychotic Experiences" ~ "Psychiatric Disorders",
      scale == "Social Detachment" ~ "Psychiatric Disorders",
      scale == "Thought Disorder" ~ "Psychiatric Disorders",
      scale == "Aggression" ~ "Psychiatric Disorders",
      scale == "Aggressive Attitude" ~ "Psychiatric Disorders",
      scale == "Verbal Aggression" ~ "Psychiatric Disorders",
      scale == "Physical Aggression" ~ "Psychiatric Disorders",
      scale == "Suicidal Ideation" ~ "Psychiatric Disorders",
      scale == "Stress" ~ "Psychosocial Problems",
      scale == "Nonsupport" ~ "Psychosocial Problems",
      scale == "Treatment Rejection" ~ "Psychosocial Problems",
      scale == "Dominance" ~ "Psychosocial Problems",
      scale == "Warmth" ~ "Psychosocial Problems",
      scale == "Borderline Features" ~ "Personality Disorders",
      scale == "Affective Instability" ~ "Personality Disorders",
      scale == "Identity Problems" ~ "Personality Disorders",
      scale == "Negative Relationships" ~ "Personality Disorders",
      scale == "Self-Harm" ~ "Personality Disorders",
      scale == "Antisocial Features" ~ "Personality Disorders",
      scale == "Antisocial Behaviors" ~ "Personality Disorders",
      scale == "Egocentricity" ~ "Personality Disorders",
      scale == "Stimulus-Seeking" ~ "Personality Disorders",
      scale == "Alcohol Problems" ~ "Substance Use",
      scale == "Drug Problems" ~ "Substance Use",
      scale == "ALC Estimated Score" ~ "Substance Use",
      scale == "DRG Estimated Score" ~ "Substance Use",
      TRUE ~ as.character(domain)
    )
  )

Subdomains

df_clinical <-
  df_clinical |>
  dplyr::mutate(
    subdomain = dplyr::case_when(
      scale == "Somatic Complaints" ~ "Somatic Symptom Disorder",
      scale == "Conversion" ~ "Somatic Symptom Disorder",
      scale == "Somatization" ~ "Somatic Symptom Disorder",
      scale == "Health Concerns" ~ "Somatic Symptom Disorder",
      scale == "Anxiety" ~ "Anxiety",
      scale == "Cognitive (A)" ~ "Anxiety",
      scale == "Affective (A)" ~ "Anxiety",
      scale == "Physiological (A)" ~ "Anxiety",
      scale == "Anxiety-Related Disorders" ~ "Anxiety",
      scale == "Obsessive-Compulsive" ~ "Anxiety",
      scale == "Phobias" ~ "Anxiety",
      scale == "Traumatic Stress" ~ "Anxiety",
      scale == "Depression" ~ "Depression",
      scale == "Cognitive (D)" ~ "Depression",
      scale == "Affective (D)" ~ "Depression",
      scale == "Physiological (D)" ~ "Depression",
      scale == "Mania" ~ "Mania/Hypomania",
      scale == "Activity Level" ~ "Mania/Hypomania",
      scale == "Grandiosity" ~ "Mania/Hypomania",
      scale == "Irritability" ~ "Mania/Hypomania",
      scale == "Paranoia" ~ "Schizophrenia Spectrum Disorders",
      scale == "Hypervigilance" ~ "Schizophrenia Spectrum Disorders",
      scale == "Persecution" ~ "Schizophrenia Spectrum Disorders",
      scale == "Resentment" ~ "Schizophrenia Spectrum Disorders",
      scale == "Schizophrenia" ~ "Schizophrenia Spectrum Disorders",
      scale == "Psychotic Experiences" ~ "Schizophrenia Spectrum Disorders",
      scale == "Social Detachment" ~ "Schizophrenia Spectrum Disorders",
      scale == "Thought Disorder" ~ "Schizophrenia Spectrum Disorders",
      scale == "Borderline Features" ~ "Borderline Personality",
      scale == "Affective Instability" ~ "Borderline Personality",
      scale == "Identity Problems" ~ "Borderline Personality",
      scale == "Negative Relationships" ~ "Borderline Personality",
      scale == "Self-Harm" ~ "Borderline Personality",
      scale == "Antisocial Features" ~ "Antisocial Personality",
      scale == "Antisocial Behaviors" ~ "Antisocial Personality",
      scale == "Egocentricity" ~ "Antisocial Personality",
      scale == "Stimulus-Seeking" ~ "Antisocial Personality",
      scale == "Aggression" ~ "Aggression",
      scale == "Aggressive Attitude" ~ "Aggression",
      scale == "Verbal Aggression" ~ "Aggression",
      scale == "Physical Aggression" ~ "Aggression",
      scale == "Alcohol Problems" ~ "Alcohol Use",
      scale == "Drug Problems" ~ "Drug Use",
      scale == "Suicidal Ideation" ~ "Depression",
      scale == "Stress" ~ "Social Environment",
      scale == "Nonsupport" ~ "Social Environment",
      scale == "Treatment Rejection" ~ "Treatment Considerations",
      scale == "Dominance" ~ "Interpersonal",
      scale == "Warmth" ~ "Interpersonal",
      scale == "ALC Estimated Score" ~ "Alcohol Use",
      scale == "DRG Estimated Score" ~ "Drug Use",
      TRUE ~ as.character(subdomain)
    )
  )

testing

df_validity <-
  df_validity |>
  dplyr::mutate(
    subdomain = dplyr::case_when(
      scale == "Inconsistency" ~ "Response Inconsistency",
      scale == "Infrequency" ~ "Negative Impression",
      scale == "Negative Impression Management" ~ "Negative Impression",
      scale == "Positive Impression Management" ~ "Positive Impresion",
      # Negative Distortion Indicators
      scale == "Malingering Index" ~ "Negative Impression",
      scale == "Rogers Discriminant Function" ~ "Negative Impression",
      scale == "Negative Distortion Scale" ~ "Negative Impression",
      scale == "Hong Malingering Index" ~ "Negative Impression",
      scale == "Multiscale Feigning Index" ~ "Negative Impression",
      scale == "Malingered Pain-Related Disability Discriminant Function" ~ "Negative Impression",
      ## Positive Distortion
      scale == "Defensiveness Index" ~ "Positive Impression",
      scale == "Cashel Discriminant Function" ~ "Positive Impression",
      scale == "Positive Distortion Scale" ~ "Positive Impression",
      scale == "Hong Defensiveness Index" ~ "Positive Impression",
      # Non-systematic Distortion Indicators
      scale == "Back Random Responding" ~ "Response Inconsistency",
      scale == "Hong Randomness Index" ~ "Response Inconsistency",
      TRUE ~ as.character(subdomain)
    )
  )

Narrow subdomains

df_clinical <-
  df_clinical |>
  dplyr::mutate(
    narrow = dplyr::case_when(
      scale == "Somatic Complaints" ~ "Somatic Symptom Disorder",
      scale == "Conversion" ~ "Somatic Symptom Disorder",
      scale == "Somatization" ~ "Somatic Symptom Disorder",
      scale == "Health Concerns" ~ "Somatic Symptom Disorder",
      scale == "Anxiety" ~ "Anxiety",
      scale == "Cognitive (A)" ~ "Anxiety",
      scale == "Affective (A)" ~ "Anxiety",
      scale == "Physiological (A)" ~ "Anxiety",
      scale == "Anxiety-Related Disorders" ~ "Anxiety Disorder",
      scale == "Obsessive-Compulsive" ~ "Anxiety Disorder",
      scale == "Phobias" ~ "Anxiety Disorder",
      scale == "Traumatic Stress" ~ "Anxiety Disorder",
      scale == "Depression" ~ "Depression",
      scale == "Cognitive (D)" ~ "Depression",
      scale == "Affective (D)" ~ "Depression",
      scale == "Physiological (D)" ~ "Depression",
      scale == "Mania" ~ "Mania/Hypomania",
      scale == "Activity Level" ~ "Mania/Hypomania",
      scale == "Grandiosity" ~ "Mania/Hypomania",
      scale == "Irritability" ~ "Mania/Hypomania",
      scale == "Paranoia" ~ "Schizophrenia Spectrum Disorder",
      scale == "Hypervigilance" ~ "Schizophrenia Spectrum Disorder",
      scale == "Persecution" ~ "Schizophrenia Spectrum Disorder",
      scale == "Resentment" ~ "Schizophrenia Spectrum Disorder",
      scale == "Schizophrenia" ~ "Schizophrenia Spectrum Disorder",
      scale == "Psychotic Experiences" ~ "Schizophrenia Spectrum Disorder",
      scale == "Social Detachment" ~ "Schizophrenia Spectrum Disorder",
      scale == "Thought Disorder" ~ "Schizophrenia Spectrum Disorder",
      scale == "Borderline Features" ~ "Borderline Personality Disorder",
      scale == "Affective Instability" ~ "Borderline Personality Disorder",
      scale == "Identity Problems" ~ "Borderline Personality Disorder",
      scale == "Negative Relationships" ~ "Borderline Personality Disorder",
      scale == "Self-Harm" ~ "Borderline Personality Disorder",
      scale == "Antisocial Features" ~ "Antisocial Personality Disorder",
      scale == "Antisocial Behaviors" ~ "Antisocial Personality Disorder",
      scale == "Egocentricity" ~ "Antisocial Personality Disorder",
      scale == "Stimulus-Seeking" ~ "Antisocial Personality Disorder",
      scale == "Aggression" ~ "Aggression",
      scale == "Aggressive Attitude" ~ "Aggression",
      scale == "Verbal Aggression" ~ "Aggression",
      scale == "Physical Aggression" ~ "Aggression",
      scale == "Alcohol Problems" ~ "Alcohol Use Disorder",
      scale == "Drug Problems" ~ "Drug Use Disorder",
      scale == "Suicidal Ideation" ~ "Depression",
      scale == "Stress" ~ "Social Environment",
      scale == "Nonsupport" ~ "Social Environment",
      scale == "Treatment Rejection" ~ "Treatment Considerations",
      scale == "Dominance" ~ "Interpersonal",
      scale == "Warmth" ~ "Interpersonal",
      scale == "ALC Estimated Score" ~ "Alcohol Use Disorder",
      scale == "DRG Estimated Score" ~ "Drug Use Disorder",
      TRUE ~ as.character(narrow)
    )
  )
df_validity <-
  df_validity |>
  dplyr::mutate(
    narrow = dplyr::case_when(
      scale == "Inconsistency" ~ paste0(scale),
      scale == "Infrequency" ~ paste0(scale),
      scale == "Negative Impression Management" ~ paste0(scale),
      scale == "Positive Impression Management" ~ paste0(scale),
      # Negative Distortion Indicators
      scale == "Malingering Index" ~ paste0(scale),
      scale == "Rogers Discriminant Function" ~ paste0(scale),
      scale == "Negative Distortion Scale" ~ paste0(scale),
      scale == "Hong Malingering Index" ~ paste0(scale),
      scale == "Multiscale Feigning Index" ~ paste0(scale),
      scale == "Malingered Pain-Related Disability Discriminant Function" ~ paste0(scale),
      # Positive Distortion Indicators
      scale == "Defensiveness Index" ~ paste0(scale),
      scale == "Cashel Discriminant Function" ~ paste0(scale),
      scale == "Positive Distortion Scale" ~ paste0(scale),
      scale == "Hong Defensiveness Index" ~ paste0(scale),
      # Non-systematic Distortion Indicators
      scale == "Back Random Responding" ~ paste0(scale),
      scale == "Hong Randomness Index" ~ paste0(scale),
      TRUE ~ as.character(narrow)
    )
  )

Scale descriptions

df_clinical <-
  df_clinical |>
  dplyr::mutate(
    description = dplyr::case_when(
      scale == "Somatic Complaints" ~ "degree of concern about physical functioning and health matters and the extent of perceived impairment arising from somatic symptoms",
      scale == "Conversion" ~ "moderate elevations may be seen in neurological disorders with CNS impairment involving sensorimotor problems, MS, CVA/stroke, or neuropsychological associated with chronic alcoholism",
      scale == "Somatization" ~ "high scorers describe general lethargy and malaise, and the presentation is one of complaintiveness and dissatisfaction",
      scale == "Health Concerns" ~ "elevations indicate a poor health may be a major component of the self-image, with the person accustomed to being in the patient role",
      scale == "Anxiety" ~ "reflecting a generalized impairment associated with anxiety",
      scale == "Cognitive (A)" ~ "elevations indicate worry and concern about current (often uncontrollable) issues that compromise the person's ability to concentrate and attend",
      scale == "Affective (A)" ~ "high scorers experience a great deal of tension, have difficulty with relaxing and tend to be easily fatigued as a result of high-perceived stress",
      scale == "Physiological (A)" ~ "high scorers my not psychologically experience themselves as anxious, but show physiological signs that most people associate with anxiety",
      scale == "Anxiety-Related Disorders" ~ "reflecting multiple anxiety-disorder diagnoses and broad impairment associated with anxiety",
      scale == "Obsessive-Compulsive" ~ "scores marked rigidity and significant ruminative concerns",
      scale == "Phobias" ~ "indicate impairing phobic behaviors, with avoidance of the feared object or situation",
      scale == "Traumatic Stress" ~ "trauma (single or multiple) is the overriding focus of the person's life",
      scale == "Depression" ~ "person feels hopeless, discouraged and useless",
      scale == "Cognitive (D)" ~ "a higher scorer is likely to report feeling hopeless and as having failed at most important life tasks",
      scale == "Affective (D)" ~ "elevations suggest sadness, a loss of interest in normal activities and a loss if one's sense of pleasure in things that were previously enjoyed",
      scale == "Physiological (D)" ~ "elevations suggest a change in level of physical functioning, typically with a disturbance in sleep pattern, a decrease in energy and level of sexual interest and a loss of appetite and/or weight loss",
      scale == "Mania" ~ "scores are associated with disorders such as mania, hypomania, or cyclothymia",
      scale == "Activity Level" ~ "this activity level renders the person confused and difficult to understand",
      scale == "Grandiosity" ~ "person may have little capacity to recognize personal limitations, to the point where one is not able to think clearly about one's capabilities",
      scale == "Irritability" ~ "person is very volatile in response to frustration and his judgment in such situations may be poor",
      scale == "Paranoia" ~ "individuals are likely to be overtly suspicious and hostile",
      scale == "Hypervigilance" ~ "suggest a person who is pragmatic and skeptical in relationships",
      scale == "Persecution" ~ "suggest an individual who is quick to feel that they are being treated inequitably and easily believes that there is concerted effort among others to undermine their best interests",
      scale == "Resentment" ~ "increasing tendency to attribute any misfortunes to the neglect of others and to discredit the successes of others as being the result of luck or favoritism",
      scale == "Schizophrenia" ~ "associated with an active schizophrenic episode",
      scale == "Psychotic Experiences" ~ "person may strike others as peculiar and eccentric",
      scale == "Social Detachment" ~ "reflects a person who neither desires nor enjoys the meaning to personal relationships",
      scale == "Thought Disorder" ~ "suggest problems in concentration and decision-making",
      scale == "Borderline Features" ~ "behaviors typically associated with borderline personality disorder",
      scale == "Affective Instability" ~ "a propensity to experience a particular negative affect (anxiety, depression, or anger is the typical response)",
      scale == "Identity Problems" ~ "suggest uncertainty about major life issues and difficulties in developing and maintaining a sense of purpose",
      scale == "Negative Relationships" ~ "person is likely to be bitter and resentful about the way past relationships have gone",
      scale == "Self-Harm" ~ "reflect levels of impulsivity and recklessness that become more hazardous as scores rise",
      scale == "Antisocial Features" ~ "individuals are likely to be impulsive and hostile, perhaps with a history of reckless and/or antisocial acts",
      scale == "Antisocial Behaviors" ~ "scores suggest a history of difficulties with authority and with social convention",
      scale == "Egocentricity" ~ "suggest a person who tends to be self-centered and pragmatic in interaction with others",
      scale == "Stimulus-Seeking" ~ "patient is likely to manifest behavior that is reckless and potentially dangerous to himself and/or those around him",
      scale == "Aggression" ~ "scores are indicative of an individual who may be seen as impatient, irritable, and quick-tempered",
      scale == "Aggressive Attitude" ~ "suggest an individual who is easily angered and frustrated; others may perceive him as hostile and readily provoked",
      scale == "Verbal Aggression" ~ "reflects a person who is assertive and not intimidated by confrontation and, toward the upper end of this range, he may be verbally aggressive",
      scale == "Physical Aggression" ~ "suggest that losses of temper are more common and that the person is prone to more physical displays of anger, perhaps breaking objects or engaging in physical confrontations",
      scale == "Alcohol Problems" ~ "are indicative of an individual who may drink regularly and may have experienced some adverse consequences as a result",
      scale == "Drug Problems" ~ "scores are indicative of a person who may use drugs on a fairly regular basis and may have experienced some adverse consequences as a result",
      scale == "Suicidal Ideation" ~ "scores are typically of an individual who is seen in clinical settings",
      scale == "Stress" ~ "individuals may be experiencing a moderate degree of stress as a result of difficulties in some major life area",
      scale == "Nonsupport" ~ "social relationships are perceived as offering little support - family relationships may be either distant or combative, whereas friends are generally seen as unavailable or not helpful when needed",
      scale == "Treatment Rejection" ~ "average scores suggest a person who acknowledges major difficulties in their functioning, and perceives an acute need for help in dealing with these problems",
      scale == "Dominance" ~ "average scores reflect an individual who is likely to be able to adapt to different interpersonal situations, by being able to both take and relinquish control in these relationships as needed",
      scale == "Warmth" ~ "average scores reflect an individual who is likely to be able to adapt to different interpersonal situations, by being able to tolerate close attachment but also capable of maintaining some distance in relationships as needed",
      TRUE ~ as.character(description)
    )
  )

# TODO:
df_validity <-
  df_validity |>
  dplyr::mutate(
    description = dplyr::case_when(
      scale == "Inconsistency" ~ paste0(scale),
      scale == "Infrequency" ~ paste0(scale),
      scale == "Negative Impression Management" ~ paste0(scale),
      scale == "Positive Impression Management" ~ paste0(scale),
      # Negative Distortion Indicators
      scale == "Malingering Index" ~ paste0(scale),
      scale == "Rogers Discriminant Function" ~ paste0(scale),
      scale == "Negative Distortion Scale" ~ paste0(scale),
      scale == "Hong Malingering Index" ~ paste0(scale),
      scale == "Multiscale Feigning Index" ~ paste0(scale),
      scale == "Malingered Pain-Related Disability Discriminant Function" ~ paste0(scale),
      # Positive Distortion Indicators
      scale == "Defensiveness Index" ~ paste0(scale),
      scale == "Cashel Discriminant Function" ~ paste0(scale),
      scale == "Positive Distortion Scale" ~ paste0(scale),
      scale == "Hong Defensiveness Index" ~ paste0(scale),
      # Non-systematic Distortion Indicators
      scale == "Back Random Responding" ~ paste0(scale),
      scale == "Hong Randomness Index" ~ paste0(scale),
      TRUE ~ as.character(description)
    )
  )

Slice

slice_clinical <- c(1:51)
df_clinical <- df_clinical |> dplyr::slice(slice_clinical)

Glue results

patient <- params$patient

df_clinical <-
  df_clinical |>
  dplyr::mutate(result = glue::glue("{patient}'s score on {.data$scale}
  ({.data$description}) was {.data$range}."))

df_validity <-
  df_validity |>
  dplyr::mutate(result = glue::glue("{patient}'s score on {.data$scale}
  ({.data$description}) was {.data$range}."))

df_inatt <-
  df_inatt |>
  dplyr::mutate(result = glue::glue("{patient}'s score on {.data$scale}
  ({.data$description}) was {.data$range}."))

Finalize and save

relocate variables

df <-
  df_clinical |>
  dplyr::relocate(c(raw_score, score, percentile, range, ci_95), .before = test)

df_validity <-
  df_validity |>
  dplyr::relocate(c(raw_score, score, percentile, range, ci_95), .before = test)

df_intatt <-
  df_inatt |>
  dplyr::relocate(c(raw_score, score, percentile, range, ci_95), .before = test)

Write out final csv

readr::write_csv(
  df_clinical,
  here::here("csv", "pai_clinical.csv"),
  col_names = TRUE,
  na = ""
)

readr::write_csv(
  df_validity,
  here::here("csv", "pai_validity.csv"),
  col_names = TRUE,
  na = ""
)

readr::write_csv(
  df_inatt,
  here::here("csv", "pai_inatt.csv"),
  col_names = TRUE,
  na = ""
)

Pluck Text

library(pdftools)
library(stringr)

# save file as path
file <- fs::as_fs_path(params$file)

Extract and save dirty text

get_text <- function(file) {
  txt <- pdftools::pdf_text(file) |>
    stringr::str_split("\n") |>
    unlist()
}
pai_text <- get_text(file)

readr::write_lines(pai_text, here::here("pai_text_dirty.md"), sep = "\n")
readr::write_lines(pai_text, here::here("pai_text_dirty.txt"), sep = "\n")
readr::write_lines(pai_text, here::here("pai_text_dirty.typ"), sep = "\n")

Squish text and save

Replace long spaces with a col break symbol. Save text to markdown, quarto, and typst formats.

pai_text_squished <-
  stringr::str_replace_all(pai_text, "\\s{2,}", "- ") |>
  stringr::str_remove_all(",")
pai_text_squished

# save
readr::write_lines(pai_text_squished, here::here("pai_text_squished.md"), sep = "\n")
readr::write_lines(pai_text_squished, here::here("pai_text_squished.txt"), sep = "\n")
readr::write_lines(pai_text_squished, here::here("pai_text_squished.typ"), sep = "\n")


brainworkup/npsych.data documentation built on April 14, 2025, 6:14 p.m.