102102# ' @importFrom data.table := fcoalesce fifelse setnames
103103# ' @importFrom DiagrammeR add_global_graph_attrs create_edge_df create_graph create_node_df render_graph
104104# ' @export
105- lgb.plot.tree <- function (model = NULL ,
106- tree = NULL ,
107- rules = NULL ,
108- render = TRUE ,
109- plot_width = NULL ,
110- plot_height = NULL
105+ lgb.plot.tree <- function (model
106+ , tree
107+ , rules = NULL
108+ , render = TRUE
109+ , plot_width = NULL
110+ , plot_height = NULL
111111 ) {
112112 # check model is lgb.Booster
113113 if (! .is_Booster(x = model )) {
@@ -119,12 +119,8 @@ lgb.plot.tree <- function(model = NULL,
119119 call. = FALSE
120120 )
121121 }
122- # tree must be integer or numeric
123- if (! inherits(tree , c(' integer' ,' numeric' ))) {
124- stop(sprintf(" lgb.plot.tree: 'tree' must only contain integers." ))
125- }
126122 # all elements of tree must be integers
127- if (! all(tree %% 1L == 0L )) {
123+ if (! inherits( tree , c( " integer " , " numeric " )) || ! all(tree %% 1L == 0L )) {
128124 stop(sprintf(" lgb.plot.tree: 'tree' must only contain integers." ))
129125 }
130126 # extract data.table model structure
@@ -140,9 +136,10 @@ lgb.plot.tree <- function(model = NULL,
140136 # filter modelDT to just the rows for the selected trees
141137 modelDT <- modelDT [tree_index %in% tree ]
142138 # change some column names to shorter and more diagram friendly versions
143- data.table :: setnames(modelDT
144- , old = c(" tree_index" , " split_feature" , " threshold" , " split_gain" )
145- , new = c(" Tree" , " Feature" , " Split" , " Gain" )
139+ data.table :: setnames(
140+ modelDT
141+ , old = c(" tree_index" , " split_feature" , " threshold" , " split_gain" )
142+ , new = c(" Tree" , " Feature" , " Split" , " Gain" )
146143 )
147144 # the output from "lgb.model.dt.tree" follows these rules
148145 # "leaf_value" and "leaf_count" are only populated for leaves (NA for internal splits)
0 commit comments