rStudio / Shiny:从渲染表中计算中位数

时间:2014-08-26 18:12:21

标签: r shiny rstudio

我能够根据以下代码中的用户选择对数据表进行子集化,但我还想根据用户输入计算并显示ohsi列的中值。

ui.R

library(shiny)
shinyUI(fluidPage(
  titlePanel("HSI Explorer"),

  sidebarLayout(
    sidebarPanel(
    radioButtons("type",label="choose CAH/NON-CAHs",choices=list("CAH","NON-CAH")),
    radioButtons("system",label="choose SYSTEM/NON-SYSTEM",choices=list("SYSTEM","NON-SYSTEM")),
    radioButtons("teaching",label="choose TEACHING/NON-TEACHING",choices=list("TEACHING","NON-TEACHING"))
    ),
    mainPanel(
      textOutput("mOut"),
      tableOutput("table1")
  )
)))

server.R

library(shiny)
datan <- read.csv("data/hsi.csv")
tcols <- c("provid","st","ohsi")
ocols <- c("ohsi")
datax <- datan[tcols]
datao <- datan[ocols]
shinyServer(function(input, output){
rTable <- renderTable({
datax[which(datan$tnt==input$teaching & datan$sysnsys==input$system & datan$cahncah==input$type),]
})
median <- median(rTable$ohsi)
output$table1 <- rTable
output$mOut <- renderText({
paste("the median for your selection is: "m,
}) 
}  
)

1 个答案:

答案 0 :(得分:1)

如果没有数据,很难给出确切的答案,但您可以将数据定义为反应函数并使用它来提供renderTablerenderText

shinyServer(function(input, output){
  myData <- reactive({
    datax[which(datan$tnt==input$teaching & datan$sysnsys==input$system & datan$cahncah==input$type),]
  })
  output$table1 <- renderTable({
    myData()
  })
  output$mOut <- renderText({
    m <- median(myData()$ohsi)
    paste("the median for your selection is: ", m)
  }) 
}  
)