2

How do I change the title of my chart in Excel via the RDCOMClient package?

I can create a chart and obtain its title as follows:

# Load package and helper functions - see http://www.omegahat.org/RDCOMClient
require(RDCOMClient)
source("http://www.omegahat.org/RDCOMClient/examples/excelUtils.R")

# Create Excel application
xls <- COMCreate("Excel.Application")

# Make Excel workbook visible to user
xls[["Visible"]] <- TRUE

# Add a worksheet to the workbook
wb = xls[["Workbooks"]]$Add(1)

# Add data.frame to worksheet
df <- data.frame(x=c("a", "b", "c"), Income = 4:6)
exportDataFrame(df, at = wb$ActiveSheet()$Range("A1"))

# Add Chart
chart.display.range <- wb$ActiveSheet()$Range("D2:H12")
wb$ActiveSheet()$Range("A1:B4")$Select()
wb$ActiveSheet()$Shapes()$AddChart(Top = chart.display.range$Top(), 
                                  Left = chart.display.range$Left(), 
                                  Height = chart.display.range$Height(), 
                                  Width = chart.display.range$Width())$Select()

# chart title
wb$ActiveChart()$ChartTitle()[["Text"]]
#[1] "Income"

But when I try to change the name

# Change chart title??
wb$ActiveChart()$ChartTitle()[["Text"]] <- "Tony's Chart"

I get an error:

Error in wb$ActiveChart()$ChartTitle()[["Text"]] <- "Tony's Chart" : 
    invalid (NULL) left side of assignment

I seem to often come across this type of problem whereby I can not change a property value and would like to figure out how to solve this (I know that I can change the name of the data.frame column but I'd like a better solution as I'm probably missing something very obvious).

Thanks in advance.

> sessionInfo()
R version 3.0.0 (2013-04-03)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United Kingdom.1252  LC_CTYPE=English_United Kingdom.1252    LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C                           
[5] LC_TIME=English_United Kingdom.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] excel.link_0.5.4     zoo_1.7-10           RDCOMClient_0.93-0.1 data.table_1.8.8     ggplot2_0.9.3.1      plyr_1.8             reshape2_1.2.2       countrycode_0.14    

loaded via a namespace (and not attached):
 [1] colorspace_1.2-2   dichromat_2.0-0    digest_0.6.3       grid_3.0.0         gtable_0.1.2       labeling_0.1       lattice_0.20-15    MASS_7.3-26        munsell_0.4       
[10] proto_0.3-10       RColorBrewer_1.0-5 scales_0.2.3       stringr_0.6.2      tools_3.0.0       
Tony Breyal
  • 5,068
  • 3
  • 25
  • 48

1 Answers1

2

Figured it out by typing random things into R:

x = wb$ActiveChart()$ChartTitle()
x[["Text"]] = "Tony's Chart"

I do wonder why I can't do this directly in one line as in my question though?

Tony Breyal
  • 5,068
  • 3
  • 25
  • 48
  • 1
    See [here](http://www.omegahat.org/RDCOMClient/Todo.html). When written in one line, [["Value"]] gets evaluated, and thus cannot be asigned a value. –  Aug 23 '13 at 10:43
  • @arbautjc Thank you, I had not seen that before, very good to know. – Tony Breyal Aug 23 '13 at 12:00