Setting cowsay variable?

Hey there!
Very new to rstudio and just trying to test stuff out. Currently having problems setting the cowsay command say() as a variable and was just if anyone might be able to help? :slight_smile:
Thanks! :slight_smile:

EB

Hey,
Could you post the code you currently have? That way we don't have to transcribe it to help you out (this is typically called a reprex, short for reproducible example).

Give how short this is, you can probably just add backticks around the code and output manually, but in the long run (or even short run) reprex is the way to go! (By adding the r next to the backticks, you'll get syntax highlighting).

```r
library(foo)
x <- "bar"
x
#> bar
```

If you've never heard of a reprex before, you might want to start by reading the tidyverse.org help page. The reprex dos and don'ts are also useful.

Hi @eb1058, cute package! :blush:

I didn't manage to get something to work in the x <- say() format, but capture.output() gets pretty close.

> capture.output(say(), type = "message")
 [1] ""                        " -------------- "       
 [3] "Hello world! "           " --------------"        
 [5] "    \\"                  "      \\"               
 [7] "        \\"              "            |\\___/|"   
 [9] "          ==) ^Y^ (=="   "            \\  ^  /"   
[11] "             )=*=("      "            /     \\"   
[13] "            |     |"     "           /| | | |\\"  
[15] "           \\| | |_|/\\" "      jgs  //_// ___/"  
[17] "               \\_)"     "  "

I don't know if this is a good enough work around?

> x <- capture.output(say(), type = "message")
> cat(x, sep = "\n")

 -------------- 
Hello world! 
 --------------
    \
      \
        \
            |\___/|
          ==) ^Y^ (==
            \  ^  /
             )=*=(
            /     \
            |     |
           /| | | |\
           \| | |_|/\
      jgs  //_// ___/
               \_)

I have to admit, the capture.output help page says "Messages sent to stderr() (including those from message, warning and stop) are captured by type = "message". Note that this can be “unsafe” and should only be used with care.", so I'm not sure when you should or shouldn't use it..

Good luck! :blush:


Edit:

I think I've found a better solution :smile:

> say('time', type = "string")
[1] "\n -------------- \n2018-03-13 13:25:12 \n --------------\n    \\\n      \\\n        \\\n            |\\___/|\n          ==) ^Y^ (==\n            \\  ^  /\n             )=*=(\n            /     \\\n            |     |\n           /| | | |\\\n           \\| | |_|/\\\n      jgs  //_// ___/\n               \\_)\n  "
> x <- say('time', type = "string")
> writeLines(x)

 -------------- 
2018-03-13 13:25:19 
 --------------
    \
      \
        \
            |\___/|
          ==) ^Y^ (==
            \  ^  /
             )=*=(
            /     \
            |     |
           /| | | |\
           \| | |_|/\
      jgs  //_// ___/
               \_)

If you look at the help page for the say() function, you'll see that by default type = "message", which is why we couldn't store it as a variable using x <- say(). Changing to type = "string" means we can :slight_smile:

The top answer to this stack overflow question explains why we have to use writeLines() rather than print(). Note:

> print(x)
[1] "\n -------------- \n2018-03-13 13:25:19 \n --------------\n    \\\n      \\\n        \\\n            |\\___/|\n          ==) ^Y^ (==\n            \\  ^  /\n             )=*=(\n            /     \\\n            |     |\n           /| | | |\\\n           \\| | |_|/\\\n      jgs  //_// ___/\n               \\_)\n  "
2 Likes