首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >rStudio/闪亮:从呈现表中计算中间值

rStudio/闪亮:从呈现表中计算中间值
EN

Stack Overflow用户
提问于 2014-08-26 18:12:21
回答 1查看 984关注 0票数 1

我可以在下面的代码中根据用户选择来子集数据表,但我也想根据用户输入计算和显示ohsi列的中值。

ui.R

代码语言:javascript
复制
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

代码语言:javascript
复制
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,
}) 
}  
)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-26 18:24:00

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

代码语言:javascript
复制
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)
  }) 
}  
)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25512462

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档