|
| 1 | +# Baseline characteristics |
| 2 | + |
| 3 | + |
| 4 | +<!-- `.md` and `.py` files are generated from the `.qmd` file. Please edit that file. --> |
| 5 | + |
| 6 | +!!! tip |
| 7 | + |
| 8 | + To run the code from this article as a Python script: |
| 9 | + |
| 10 | + ```bash |
| 11 | + python3 examples/example-baseline.py |
| 12 | + ``` |
| 13 | + |
| 14 | +This article reproduces `vignettes/example-basechar.Rmd` in the r2rtf |
| 15 | +package. |
| 16 | + |
| 17 | +## Imports |
| 18 | + |
| 19 | +``` python |
| 20 | +from importlib.resources import files |
| 21 | + |
| 22 | +import pandas as pd |
| 23 | + |
| 24 | +import rtflite as rtf |
| 25 | +``` |
| 26 | + |
| 27 | +## Ingest data |
| 28 | + |
| 29 | +Load data from CSV file: |
| 30 | + |
| 31 | +``` python |
| 32 | +data_path = files("rtflite.data").joinpath("baseline.csv") |
| 33 | +df = pd.read_csv(data_path, na_filter=False) |
| 34 | +print(df) |
| 35 | +``` |
| 36 | + |
| 37 | + var 1 1_pct 2 2_pct 3 3_pct 9999 9999_pct var_label |
| 38 | + 0 Female 53 10.4 50 9.8 40 7.9 143 28.1 Gender |
| 39 | + 1 Male 33 6.5 34 6.7 44 8.7 111 21.9 Gender |
| 40 | + 2 <65 14 2.8 8 1.6 11 2.2 33 6.5 Age (Years) |
| 41 | + 3 65-80 42 8.3 47 9.3 55 10.8 144 28.3 Age (Years) |
| 42 | + 4 >80 30 5.9 29 5.7 18 3.5 77 15.2 Age (Years) |
| 43 | + 5 Age (Years) |
| 44 | + 6 Subjects with data 86 84 84 254 Age (Years) |
| 45 | + 7 Mean 75.2 75.7 74.4 75.1 Age (Years) |
| 46 | + 8 SD 8.6 8.3 7.9 8.2 Age (Years) |
| 47 | + 9 Median 76.0 77.5 76.0 77.0 Age (Years) |
| 48 | + 10 Range 52 to 89 51 to 88 56 to 88 51 to 89 Age (Years) |
| 49 | + 11 White 78 15.4 78 15.4 74 14.6 230 45.3 Race |
| 50 | + 12 Black 8 1.6 6 1.2 9 1.8 23 4.5 Race |
| 51 | + 13 Other 0 0.0 0 0.0 1 0.2 1 0.2 Race |
| 52 | + |
| 53 | +Create header data frames: |
| 54 | + |
| 55 | +``` python |
| 56 | +header1 = pd.DataFrame([["", "Placebo", "Drug Low Dose", "Drug High Dose", "Total"]]) |
| 57 | +header2 = pd.DataFrame([["", "n", "(%)", "n", "(%)", "n", "(%)", "n", "(%)"]]) |
| 58 | +``` |
| 59 | + |
| 60 | +## Compose RTF |
| 61 | + |
| 62 | +Create RTF document: |
| 63 | + |
| 64 | +``` python |
| 65 | +doc = rtf.RTFDocument( |
| 66 | + df=df, |
| 67 | + rtf_title=rtf.RTFTitle( |
| 68 | + text=["Demographic and Anthropometric Characteristics", "ITT Subjects"] |
| 69 | + ), |
| 70 | + rtf_column_header=[ |
| 71 | + rtf.RTFColumnHeader(df=header1, col_rel_width=[3] + [2] * 4), |
| 72 | + rtf.RTFColumnHeader( |
| 73 | + df=header2, |
| 74 | + col_rel_width=[3] + [1.2, 0.8] * 4, |
| 75 | + border_top=[""] + ["single"] * 8, |
| 76 | + border_left=["single"] + ["single", ""] * 4, |
| 77 | + ), |
| 78 | + ], |
| 79 | + rtf_body=rtf.RTFBody( |
| 80 | + page_by=["var_label"], |
| 81 | + col_rel_width=[3] + [1.2, 0.8] * 4 + [3], |
| 82 | + text_justification=["l"] + ["c"] * 8 + ["l"], |
| 83 | + text_format=[""] * 9 + ["b"], |
| 84 | + border_left=["single"] + ["single", ""] * 4 + ["single"], |
| 85 | + border_top=[""] * 9 + ["single"], |
| 86 | + border_bottom=[""] * 9 + ["single"], |
| 87 | + ), |
| 88 | +) |
| 89 | + |
| 90 | +doc.write_rtf("output.rtf") |
| 91 | +``` |
| 92 | + |
| 93 | +## Convert to PDF |
| 94 | + |
| 95 | +``` python |
| 96 | +converter = rtf.LibreOfficeConverter() |
| 97 | +converter.convert( |
| 98 | + input_files="output.rtf", output_dir=".", format="pdf", overwrite=True |
| 99 | +) |
| 100 | +``` |
| 101 | + |
| 102 | +<embed src="../images/example-baseline/output.pdf" style="width:100%; height:400px" type="application/pdf"> |
0 commit comments