Tajda
Tajda

Reputation: 29

Export results of a function to .rtf

I'm running the be2x2 function from the BE package and I'm having trouble printing out the results in .rtf The function runs fine when I leave out the argument for printing the results as an rtf file and I get the results printed to the console it just doesn't work when I try to include the rtfName argument. I get an error of:

Error in printCoefmat(x, digits = digits, signif.stars = signif.stars, : formal argument "na.print" matched by multiple actual arguments

My code looks like this:

library(BE)
be2x2(dta, Columns = c("AUClast", "Cmax", "Tmax"), 
      rtfName="rtffile.rtf")

I tried different ways of naming the .rtf file - with or without the suffix .rtf I also tried defining the file seperately:

rtffile <- RTF("rtf")

and that didn't work either, I get the same error.

I've also tried including the output path to my directory, but that didn't help either. I'm assuming there is a mistake in my phrasing, but I just can't figure out what it could be, any help would be highly appreciated!

Here is a sample of my data:

   RowNum SUBJ GRP PRD TRT   Cmax Tmax   AUClast
1       1    A  TR   1   T 122.20 1.50 364.74595
2       2    B  RT   2   T 102.00 1.50 404.94560
3       3    C  RT   2   T 201.50 0.66 702.83140
4       4    E  TR   1   T  59.47 3.00 233.25030
5       5    F  RT   2   T  66.40 1.00 247.41105
6       6    G  TR   1   T  54.19 1.50 177.92155
7       7    H  RT   2   T 100.90 1.00 246.38780
8       8    I  TR   1   T  89.51 1.50 407.99185
9       9    K  RT   2   T 154.80 1.50 315.47610
10     10    L  TR   1   T  56.88 1.00 140.12540
11     11    M  TR   1   T  23.15 4.00 165.36500
12     12    N  RT   2   T  37.76 0.66  87.98820
13     13    O  RT   2   T  43.30 1.00 182.77325
14     14    P  TR   1   T  68.25 0.66 122.47815
15     15    Q  RT   2   T  27.54 1.50  67.98150
16     16    R  TR   1   T  60.43 2.00 274.57910
17     17    A  TR   2   R 126.20 1.50 375.42600
18     18    B  RT   1   R 206.90 1.50 595.03875
19     19    C  RT   1   R 122.80 1.50 471.16440
20     20    E  TR   2   R  37.26 1.00 190.39375
21     21    F  RT   1   R  84.67 2.00 257.45585
22     22    G  TR   2   R  55.27 1.50 175.37495
23     23    H  RT   1   R 218.70 1.00 381.82400
24     24    I  TR   2   R 181.90 0.66 360.82750
25     25    K  RT   1   R  59.68 1.50 218.47035
26     26    L  TR   2   R  25.56 2.00  91.80655
27     27    M  TR   2   R  57.05 1.50 269.01575
28     28    N  RT   1   R  47.20 0.66 105.56250
29     29    O  RT   1   R  70.88 1.50 290.14200
30     30    P  TR   2   R  97.46 1.50 230.48885
31     31    Q  RT   1   R  88.38 1.50 143.54870
32     32    R  TR   2   R  98.82 2.00 344.48280

Upvotes: 1

Views: 252

Answers (1)

Ian Campbell
Ian Campbell

Reputation: 24790

This appears to be a bug on line 41 of the be2x2 function. Since the package is published by Kyun-Seop Bae under the GPL-3 license, here is a copy of the function with the bug fixed:

be2x2 = function(Data, Columns = c("AUClast", "Cmax", "Tmax"), rtfName="")
{
  if ("data.frame" %in% class(Data)) {
    bedata = Data
  } else if ("character" %in% class(Data)) {
    bedata = read.csv(Data)
  } else {
    stop("Data should be data.frame or file name!")
  }
  bedata = bedata[order(bedata$GRP, bedata$PRD, bedata$SUBJ),];
  if(!assert(bedata)) {
    cat("\n Drop-outed subjects should not be included!\n");
    return(NULL);
  }
  nCol = length(Columns)
  if (nCol == 0) stop("Input Error. Please, check the arguments!")
  if (rtfName != "") {
    rtf = RTF(rtfName)
    addHeader(rtf, title="Bioequivalence Test Result")
    addNewLine(rtf)
    addHeader(rtf, "Table of Contents")
    addTOC(rtf)
  }
  Result = vector()
  for (i in 1:nCol) {
    plot2x2(bedata, Columns[i])
    if (toupper(Columns[i]) != "TMAX"){
      cResult = test2x2(bedata, Columns[i])
    } else {
      cResult = hodges(bedata, Columns[i])
    }  
    if (rtfName != "") {
      addPageBreak(rtf)
      addHeader(rtf, title=Columns[i], TOC.level=1)
      LineResult = capture.output(print(cResult))
      for (j in 1:length(LineResult)) addParagraph(rtf, LineResult[j])
      addPageBreak(rtf)
      addPlot(rtf, plot.fun=plot2x2a, width=6.5, height=6.5, res=300, bedata=bedata, Var=Columns[i])
      addPageBreak(rtf)
      addPlot(rtf, plot.fun=plot2x2b, width=6.5, height=6.5, res=300, bedata=bedata, Var=Columns[i])
    }
    Result = c(Result, list(cResult))
  }
  if (rtfName != "") {
    addPageBreak(rtf)
    addSessionInfo(rtf)
    done(rtf)
  }
  names(Result) = Columns
  return(Result)
}

You should now be able to save the .rtf file:

be2x2(dta, Columns = c("AUClast", "Cmax", "Tmax"), rtfName = "Test.rtf")

Upvotes: 1

Related Questions