Search code examples
rquantmod

Obtaining only numeric output from viewFinancials without additional text


I calculated dividend yield of Microsoft the following way:

# load financial data for MSFT
library(quantmod)
getFinancials('MSFT')

# calculate dividend yield for MSFT
as.numeric(first(-viewFinancials(MSFT.f, type='CF', period='A',subset = NULL)['Total Cash Dividends Paid',]/viewFinancials(MSFT.f, type='BS', period='A',subset = NULL)['Total Common Shares Outstanding',]))

Here is the output

Annual Cash Flow Statement for MSFT
Annual Balance Sheet for MSFT
[1] 1.40958

How is it possible to have only the numeric output 1.40958 without the additional text Annual Cash Flow Statement for MSFT and Annual Balance Sheet for MSFT? Is there a way to suppress those?


Solution

  • The two strings, "Annual Cash Flow Statement for MSFT" and "Annual Balance Sheet for MSFT" are messages from viewFinancials. They are not attached to the result in any way.

    R> dy <- as.numeric(first(-viewFinancials(MSFT.f, type='CF', period='A',subset = NULL)['Total Cash Dividends Paid',]/viewFinancials(MSFT.f, type='BS', period='A',subset = NULL)['Total Common Shares Outstanding',]))
    Annual Cash Flow Statement for MSFT
    Annual Balance Sheet for MSFT
    R> dy
    [1] 1.40958
    

    If you want to squelch the messages, use suppressMessages().

    R> suppressMessages(dy <- as.numeric(first(-viewFinancials(MSFT.f, type='CF', period='A',subset = NULL)['Total Cash Dividends Paid',]/viewFinancials(MSFT.f, type='BS', period='A',subset = NULL)['Total Common Shares Outstanding',])))
    R> dy
    [1] 1.40958
    R>