Home > Mobile >  R function cat outputs to clipbaord
R function cat outputs to clipbaord

Time:10-21

I have written an R function to write some SAS code (yes, I know, I am lazy), and it prints text as follows:

proc_sql_missing <- function(cols, data){
  cat("\nproc sql;")
  cat(paste0("\n\tselect\tPat_TNO,\n\t\t\t", paste(names(data)[cols], collapse = ",\n\t\t\t")))
  cat("\n\tfrom", substitute(data))
  cat(paste0("\n\twhere\t", paste(names(data)[cols], collapse = "= . OR\n\t\t\t"), "= .;"))
  cat("\nquit;")
}

Which when called prints something similar to the following to the R output:

proc sql;
    select  Pat_TNO,
            fuq_pa_enjoy_hate,
            fuq_pa_bored_interest,
            fuq_pa_like_dislike,
            fuq_pa_pleasble_unpleasble,
            fuq_pa_absorb_notabsorb,
            fuq_pa_fun_notfun,
            fuq_pa_energizing_tiring,
            fuq_pa_depress_happy,
            fuq_pa_pleasant_unpleast,
            fuq_pa_good_bad,
            fuq_pa_invigor_notinvigor,
            fuq_pa_frustrate_notfrust,
            fuq_pa_gratifying_notgrat,
            fuq_pa_exhilarate_notexhil,
            fuq_pa_stimulate_notstim,
            fuq_pa_accom_notaccom,
            fuq_pa_refresh_notrefresh,
            fuq_pa_doing_notdoing
    from followup
    where   fuq_pa_enjoy_hate= . OR
            fuq_pa_bored_interest= . OR
            fuq_pa_like_dislike= . OR
            fuq_pa_pleasble_unpleasble= . OR
            fuq_pa_absorb_notabsorb= . OR
            fuq_pa_fun_notfun= . OR
            fuq_pa_energizing_tiring= . OR
            fuq_pa_depress_happy= . OR
            fuq_pa_pleasant_unpleast= . OR
            fuq_pa_good_bad= . OR
            fuq_pa_invigor_notinvigor= . OR
            fuq_pa_frustrate_notfrust= . OR
            fuq_pa_gratifying_notgrat= . OR
            fuq_pa_exhilarate_notexhil= . OR
            fuq_pa_stimulate_notstim= . OR
            fuq_pa_accom_notaccom= . OR
            fuq_pa_refresh_notrefresh= . OR
            fuq_pa_doing_notdoing= .;
quit;

Is there any way I can automatically copy this text to the clipboard so I can paste straight into SAS?

CodePudding user response:

capture.output to capture the output:

output <- capture.output(proc_sql_missing(cols, data))

Then you can use clipr::write_clip(output), or writeClipboard(output) on Windows.

CodePudding user response:

You can specify file = "clipboard" as an argument to cat, so if you change your function to

proc_sql_missing <- function(cols, data) {
  
  cat(
    paste(
      "\nproc sql;",
      paste0("\n\tselect\tPat_TNO,\n\t\t\t", 
             paste(names(data)[cols], collapse = ",\n\t\t\t")),
      paste("\n\tfrom", substitute(data)),
      paste0("\n\twhere\t", 
             paste(names(data)[cols], collapse = "= . OR\n\t\t\t"),
             "= .;"),
      "\nquit;",
      sep = "\n"),
  file = "clipboard"
  )
}

Then when you run, say,

proc_sql_missing(1:3, mtcars)

Then your clipboard will now contain


proc sql;

    select  Pat_TNO,
            mpg,
            cyl,
            disp

    from mtcars

    where   mpg= . OR
            cyl= . OR
            disp= .;

quit;
  •  Tags:  
  • r
  • Related