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

时间:2013-07-29 12:01:23

标签: r excel vba rdcomclient

如何通过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

我似乎经常遇到这种类型的问题,我无法更改属性值并想知道如何解决这个问题(我知道我可以更改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       

1 个答案:

答案 0 :(得分:2)

通过在R:中输入随机内容来计算出来:

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

我想知道为什么我不能像我的问题那样直接在一行中这样做?