如何link ui 下拉菜单选择到闪亮的服务器输出

How to link ui dropdown menu choice to server output in shiny

Shiny 的超级新手。我正在尝试开发一个应用程序来计算不同的 correlation/agreement 系数以及显示图表。

我不知道如何从下拉菜单中 link 我的测试选择。此时我只能使用 cor 和三个选项进行相关性测试 - Spearman、Pearson 和 Kendall。我想合并 Cohen 的 kappa 测试(使用包 fmsb 和命令 Kappa.test(selectedData()))。我尝试使用 if else 语句将 Kappa 合并到 renderPrint 命令中,但我不断收到错误消息。

在这个阶段,我不太关心为每个测试使用什么合适的变量。我 post 下面是我的代码。

谢谢

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)

您可以在 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)
    }
})