2013-07-29 27 views
2

如何通过RDCOMClient包在Excel中更改图表的标题?如何通过RDCOMClient在Excel中更改图表标题?

我可以创建一个图表,并获得其标题如下:

# 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" 

但是,当我试图改变名称

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

我得到一个错误:

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

我似乎经常遇到这种类型的问题,我不能改变一个属性值,并想弄清楚如何o解决这个问题(我知道我可以更改data.frame列的名称,但我想要一个更好的解决方案,因为我可能错过了一些非常明显的东西)。

在此先感谢。

> 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  

回答

2

想通了通过输入随机的东西到R:

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

我不知道为什么我不能在同一行这样直接在我的问题有关系吗?

+1

请参阅[这里](http://www.omegahat.org/RDCOMClient/Todo.html)。当写入一行时,[[“Value”]]被评估,因此不能被赋值。 – 2013-08-23 10:43:42

+0

@arbautjc谢谢,我以前没有见过,很高兴知道。 –