gvisScatterChart define series dynamically

ε祈祈猫儿з 提交于 2019-12-05 18:25:23

This is not a bug, just as said in the help it expects a JSON string so you need to build JSON string.

Using RJSONIO you can build the JSON option using toJSON

library(googleVis)
library(RJSONIO)
myColor <- 'grey'   ## my dynamic color, here I fix but you can read it ,e.g
                    ## from a chart config file or whatever you want
isLegend <- TRUE    ## a boolean value 

myseriesOptions <- toJSON(list(list(color=myColor),list(visibleInLegend=isLegend)))

For example

Scatter2 <- gvisScatterChart(women, 
                             options=list(legend="none",
                                          lineWidth=2, pointSize=0,
                                          title="Women", vAxis="{title:'weight (lbs)'}",
                                          hAxis="{title:'height (in)'}", 
                                          width=300, height=300,
                                          series = myseriesOptions    ))

 plot(Scatter2)

PS : We can use fromJSON to get the R form of the string to construct, e.g

fromJSON("{title:'mytitle'}")        ## the ouptut is a list 
$itl
NULL

cat(toJSON(list(title='mytitle')))   ## I construct my list and I use toJSON
                                     ## I get my origin json form

{
 "title": "mytitle" 
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!