首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Shiny中绘制动态C5.0决策树

在Shiny中绘制动态C5.0决策树
EN

Stack Overflow用户
提问于 2016-09-22 01:29:03
回答 1查看 1.8K关注 0票数 2

我正在开发一个闪亮的应用程序,让用户按需选择因变量/自变量,然后执行C5.0来生成摘要和树图。但是,在生成情节时会出现错误消息。有人知道解决办法吗?请查找代码:

代码语言:javascript
复制
library(shiny)

ui <- fluidPage(
  titlePanel('Plotting Decision Tree'),
  sidebarLayout(
    sidebarPanel(
      h3('iris data'),
      uiOutput('choose_y'),
      uiOutput('choose_x'),
      actionButton('c50', label = 'Generate C5.0 summary and plot')
    ),
    mainPanel(
      verbatimTextOutput('tree_summary'),
      plotOutput('tree_plot_c50')
    )
  )
)

# server.R
library(shiny)
library(C50)

server <- function(input, output) {
  output$choose_y <- renderUI({
    is_factor <- sapply(iris, FUN = is.factor)
    y_choices <- names(iris)[is_factor]
    selectInput('choose_y', label = 'Choose Target Variable', choices = y_choices)
  })

  output$choose_x <- renderUI({
    x_choices <- names(iris)[!names(iris) %in% input$choose_y]
    checkboxGroupInput('choose_x', label = 'Choose Predictors', choices = x_choices)
  })

  observeEvent(input$c50, {
    c50_fit <- C5.0(as.formula(paste(isolate(input$choose_y), '~', paste(isolate(input$choose_x), collapse = '+'))), data = iris)
    output$tree_summary <- renderPrint(summary(c50_fit))
    output$tree_plot_c50 <- renderPlot({
      plot(c50_fit)
    })
  })
}

shinyApp(ui, server)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-22 02:26:00

在你的server.R中尝试

代码语言:javascript
复制
function(input, output) {
  output$choose_y <- renderUI({
    is_factor <- sapply(iris, FUN = is.factor)
    y_choices <- names(iris)[is_factor]
    selectInput('choose_y', label = 'Choose Target Variable', choices = y_choices)
  })

  output$choose_x <- renderUI({
    x_choices <- names(iris)[!names(iris) %in% input$choose_y]
    checkboxGroupInput('choose_x', label = 'Choose Predictors', choices = x_choices)
  })

  observeEvent(input$c50, {
    form <- paste(isolate(input$choose_y), '~', paste(isolate(input$choose_x), collapse = '+'))
    c50_fit <- eval(parse(text = sprintf("C5.0(%s, data = iris)", form)))
    output$tree_summary <- renderPrint(summary(c50_fit))
    output$tree_plot_c50 <- renderPlot({
      plot(c50_fit)
    })
  })
}

解释一下。plot方法似乎在查找从C5.0()返回的值的call元素中指定的术语,并在找不到它们时抛出一个错误。在您的例子中,这是指input对象。解决方法是通过C5.0()结构使用完全指定的公式(例如Species ~ Sepal.Length + Petal.Width)调用eval(parse(text = ...))

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39628872

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档