【问题标题】:How to print out a rpart tree result in text in Rshiny如何在 Rshiny 中打印出 rpart 树的结果为文本
【发布时间】:2015-04-05 17:22:49
【问题描述】:

您可以使用以下命令轻松地在 R 控制台中以文本形式打印出 rpart 树结果。

fit <- rpart(Kyphosis ~ Age + Number + Start, data = kyphosis)
print(fit)

然后打印出来:

n=81

node), split, n, loss, yval, (yprob) * 表示终端节点

1) 根 81 17 缺席 (0.79012346 0.20987654) 2) 开始>=8.5 62 6 缺席 (0.90322581 0.09677419)
4) 开始>=14.5 29 0 缺席 (1.00000000 0.00000000) * 5) 开始 10) 年龄=55 21 6 缺席 (0.71428571 0.28571429)
22) 年龄>=111 14 2 缺席 (0.85714286 0.14285714) * 23) 年龄

但是,这在 Rshiny textOutput 中不起作用。
请参阅以下 Rshiny 代码:

ui.r

library(shiny)

# Define UI for application that draws a histogram
shinyUI(fluidPage(

  # Application title
    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("distPlot"),
      textOutput("distText")
    )
  )
)

服务器.r

library(shiny)
library(rpart)
# Define server logic required to draw a histogram
shinyServer(function(input, output) {
  output$distText <- renderText({
    fit <- rpart(Kyphosis ~ Age + Number + Start, data = kyphosis)
    print(fit)
  })
})

如果我运行上述闪亮的应用程序,它会给出以下错误:

cat(list(...)、file、sep、fill、labels、append) 中的错误:
'cat' 无法处理参数 1(类型 'list')

【问题讨论】:

    标签: r shiny rpart


    【解决方案1】:

    您可以使用capture.output(fit) 来获取打印函数输出的字符串。 您可能还想将ui.R 中的textOutput 更改为htmlOutput。这使您可以进行多行文本输出。

    代码如下: 服务器.R

    library(shiny)
    library(rpart)
    # Define server logic required to draw a histogram
    shinyServer(function(input, output) {
    
            output$distText <- renderText({
                    fit <- rpart(Kyphosis ~ Age + Number + Start, data = kyphosis)
                    paste(capture.output(fit),collapse="<br>")
    
    
            })
    })
    

    ui.R

    library(shiny)
    
    # Define UI for application that draws a histogram
    shinyUI(fluidPage(
    
            # Application title
            # Show a plot of the generated distribution
            mainPanel(
                    plotOutput("distPlot"),
                    htmlOutput("distText")
            )
    )
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-05
      • 2013-07-07
      • 2012-01-27
      • 1970-01-01
      • 2012-06-07
      • 1970-01-01
      相关资源
      最近更新 更多