2016-05-03 5 views
1

スーパーで新しいシャイニーです。私はさまざまな相関/一致係数と表示グラフを計算するアプリケーションを開発しようとしています。uiドロップダウンメニューの選択肢をサーバーの出力にリンクする方法

テストの選択肢をドロップダウンメニューからリンクする方法がわかりません。現時点では、私は3つの選択肢(Spearman、Pearson、Kendall)でcorを使って相関テストを行うことしかできません。私はCohenのκテスト(パッケージfmsbとコマンドKappa.test(selectedData())を使用しています)を組み込みたいと思います。私はrenderPrintコマンドの中にKappaを組み込むためにelse文を使用しようとしましたが、エラーメッセージが出ます。

この段階では、各テストにどのような適切な変数を使用するかについてはあまり心配していません。私は以下のコードを投稿します。

おかげ

library(shiny) 

# Loading package for Kappa test 
library(fmsb) 

# Generating simulated data 
    dat <- as.data.frame(dat) 

UI.R

ui <- fluidPage(
    pageWithSidebar(
    headerPanel('Correlation coefficient and scatter plots'), 
    sidebarPanel(
     selectInput('xcol', 'X Variable', names(dat)), 
     selectInput('ycol', 'Y Variable', names(dat), 
        selected=names(dat)[[2]]), 
     selectInput(inputId = "measure", label = "Choose the correlation measure that you want to use", 
        choices = c("Spearman correlation" = "spearman", 
           "Pearson correlation" = "pearson", 
           "Kendall's W" = "kendall", 
           "Cohen's kappa" = "kappa")) 
    ), 
    mainPanel(
     plotOutput('plot1'), 
     verbatimTextOutput('stats') 
    ) 
) 
) 

Server.R

server <- function(input, output){ 

    # Combine the selected variables into a new data frame 
    selectedData <- reactive({ 
    dat[, c(input$xcol, input$ycol)] 
    }) 

    output$plot1 <- renderPlot({ 
    plot(selectedData(),pch = 20, cex = 3, col = "red") 
    }) 

    output$stats <- renderPrint({ 

     round(cor(selectedData(), method = input$measure), 3) 
     }) 
} 

shinyApp(ui = ui, server = server) 
+0

'cor'関数は依然としてそのメソッドだけをとります。新しい選択肢を別々に処理する必要があります。 – Gopala

答えて

2

あなただけrenderPrint関数内自分の条件に投げることができます。

output$stats <- renderPrint({ 
    measure <- input$measure 
    mydat <- selectedData() 
    if(measure == "kappa") { 
     Kappa.test(mydat[,1], mydat[,2]) 
    } else { 
     round(cor(mydat, method = measure), 3) 
    } 
}) 
+0

これは動作します、多くのおかげで@モロックス! – AlxRd

関連する問題