Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to suppress R output using markstat (SSC)?

    German Rodriguez I am a big fan of Germán Rodríguez's command markstat (SSC) for Stata Markdown because it can combine Stata, Mata and R code blocks and inline code. But can markstat suppress R output? I am using Stata/MP 15.1 for Windows (64-bit x86-64).

    To suppress Stata output use quietly as mentioned, for example, on Germán's webpage at http://data.princeton.edu/stata/markdown/documentation. Here is an example of using markstat with R from his website: http://data.princeton.edu/stata/mark...antiles.cshtml. To suppress R output using R Markdown, a stackoverflow message suggested the parameter results='hide'. I am hoping that markstat could get a similar feature for suppressing R output or to be told a possible workaround.

  • #2
    disclaimer: I haven't tried out any of these ideas, but here's what I might try.

    I'd bet German built this so that you can suppress the R commands with a forward slash at the end of the language code fence

    ```R/

    Maybe?
    Then to suppress the output you need some way to suppress the R output in batch mode. Maybe use the R function sink()? (Sinking output and sinking messages is two separate steps, if I recall correctly).

    Kind of cludgy, but it might be a place to start.
    Doug Hemken
    SSCC, Univ. of Wisc.-Madison

    Comment


    • #3
      Hi Doug,

      Thanks for the tip. Yes, the code fence (the forward slash, "/") will suppress the R commands. The issue is how to also suppress the output.

      Unfortunately, I am not as good in R as in Stata. I mostly use R at work because of one single R command: fastLink for probabilistic record linkage. Often I have to do muItiple linkages on subsets to build up to an overall linkage result. I want to suppress most of the intermediate linkage commands and output since it is too technical for the casual reader. I will report back to you and Statalist unless German or someone else responds on Statalist first.

      I will try sink(). I also read about the similar "capture.output()" here but I do not yet understand the difference:
      http://stat.ethz.ch/R-manual/R-devel...re.output.html

      Comment


      • #4
        capture.output() is meant to work with a single "command" (call or expression in R jargon, usually a function in this context), while sink() toggles on and off redirecting output to a file. capture.output() could be perfect for your situation.

        I feel like I'm writing on the wrong forum! (smile)

        Depending what other features of -markstat- you are using, this might be easier to specify over in R (which can call Stata to do markdown).

        You should just email German, he is very responsive to this sort of question.
        Doug Hemken
        SSCC, Univ. of Wisc.-Madison

        Comment


        • #5
          As Doug Hemken and Anders Alexandersson have noted, markstat can suppress echoing the input command with a forward slash at the end of the code fence. There is no equivalent way to suppress echoing the output, because in Stata you can use quietly and R will not print anything unless you ask for it explicitly. So the short answer is simply to assign the result of your function call. For example if you fit a linear model, lm(y ~ x,...), will print the result but fit <- lm(y ~ x, ...- will not. Sometimes I use something like fit <- lm(y ~ x, ...); fit to both store the result and print it. If there are some use cases where this would not work I would love to hear about them. Hope this helps.

          Comment


          • #6
            German Rodriguez The example R package I mentioned for probabilistic record linkage, fastLink, is an example where assigning the result of your function call is not enough to suppress the R output. The package is available from GitHub at https://github.com/kosukeimai/fastLink . You still get the standard fastLink output (only the number of threads vary):

            ====================
            fastLink(): Fast Probabilistic Record Linkage
            ====================

            Calculating matches for each variable.
            Getting counts for zeta parameters.
            Parallelizing calculation using OpenMP. 1 threads out of 8 are used.
            Running the EM algorithm.
            Getting the indices of estimated matches.
            Parallelizing calculation using OpenMP. 1 threads out of 8 are used.
            Deduping the estimated matches.
            Getting the match patterns for each estimated match.


            However, by adding capture.out(..., file = 'NUL') I am now able to suppress the R output.

            I just learned the trick at stackoverflow: https://stackoverflow.com/questions/...ds-output-in-r
            As noted in the posting, how to specify NULL is specific to the operating system. I specified 'NUL' since I used a Windows OS.

            In my example, I assigned to out2 whereas Germán assigned to fit. This is what I typed to suppress the R command (it is indented though preview does not show) AND the output:

            ```r/
            capture.output(out2 <- fastLink(
            dfA = patient_other, dfB = requestor_other,
            varnames = c("ssn", "zip", "stnum", "dob", "dblmf", "dblml"),
            return.all = TRUE), file = 'NUL')
            ```


            This the more generalized pseudo-code solution for suppressing R output with markstat that I found:

            ```r/
            capture.output(..., file = NULL)
            ```

            Comment


            • #7
              Anders Alexandersson thanks for the example. Looks like fastLink and perhaps other R packages don't follow the usual convention of limiting output to an object that can be assigned or printed.

              I think you are on the right track using capture.output(), but as you note it requires care depending on the platform. Basically what we want to do is send the output to a null device, which is "NUL" in Windows and "/dev/null" otherwise. This is not the same as specifying NULL (no quotes), which according to the help file returns the output as a character vector, so it will not be hidden. However, it can be assigned to suppress printing.

              So a revised general solution to suppress input and output in markstat might look like this:

              Code:
              ```r/
                  hide <- capture.output(...)
              ```
              This gets around the need to specify a null device and should work cross-platform. In your application you could try something like hide <- capture.output(out2 <- fastLink(...)).

              If there is demand we always have the alternative of adding a chunk option to hide all output.

              Comment


              • #8
                German Rodriguez Thanks! Your revised general solution worked for my example.

                Comment

                Working...
                X