【问题标题】:How to link ui dropdown menu choice to server output in shiny如何将ui下拉菜单选项链接到闪亮的服务器输出
【发布时间】:2016-08-27 21:09:05
【问题描述】:

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

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

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

谢谢

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')
    )
  )
)

服务器.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)

【问题讨论】:

  • cor 函数仍然只采用它所做的方法。您需要单独处理这个新选择。

标签: r shiny-server shiny


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-07
    • 2021-05-01
    • 2019-11-26
    • 2015-02-09
    • 1970-01-01
    • 2016-12-09
    • 2014-10-05
    • 2017-05-13
    相关资源
    最近更新 更多