2

次のコマンドを使用して、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)Start> = 8.5 62 6 Absant(0.90322581 0.0967419)
4)Start> = 14.5 29 0 ABSENT(1.000000000.000000) * 5)
開始)年齢<55 12 0欠席(1.000000000.00000000) * 11)年齢> = 55 21 6存在(0.71428571 0.28571429) 22)
22)年齢> = 111 14 2欠席(0.85714286 0.14285714) * 23)年齢<111 7 ) * 3) 開始 < 8.5 19 8 現在 (0.42105263 0.57894737) *

ただし、これは 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")
    )
  )
)

server.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) のエラー:
引数 1 (タイプ 'list') は 'cat' で処理できません

4

1 に答える 1

2

capture.output(fit)print 関数によって出力された文字列を取得するために使用できます。の を に変更することもtextOutputできui.RますhtmlOutput。これにより、複数行のテキスト出力が可能になります。

コードは次のようになります: server.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")
        )
)
)
于 2015-02-05T18:29:26.733 に答える