2

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

1 回答 1

2

通过在 R 中输入随机的东西来解决这个问题:

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

我确实想知道为什么我不能像我的问题那样直接在一行中执行此操作?

于 2013-07-29T12:56:04.960 回答