发送tabe作为Shiny的输出不会在Shiny UI中呈现

时间:2016-07-30 14:51:08

标签: r shiny

我已经从我的表中计算了来自闪亮UI的给定输入的min,max,Q1,Q3值。但是无法从server.R发送循环变量。

# ui.R
library(shiny)
library(RODBC)
library(DBI)
dbconnect <- odbcConnect("orecloud", uid="XXX", pwd="XX", believeNRows=FALSE)
df <- data.frame()
df <- sqlQuery(dbconnect,"SELECT distinct cpan FROM CYTOKINE ")
shinyUI(fluidPage(
  headerPanel("ORE Cytokine Summary"),
  sidebarLayout(
    sidebarPanel(
      helpText("Please select Patient Details.."),
      selectInput("CPAN", 
                  label = "Choose patient",
                  choices = df,
                  selected = NULL),
   submitButton(text = "Submit", icon = NULL)),
    mainPanel(
        dataTableOutput("tableoutput")
    )
  )
))

这里在Server.R文件中我需要将输出变量UI.R发送到renderDataTable

server.R

library(RODBC)
library(RODBC)
library(lattice)
    shinyServer(
      function(input, output) {

        dbconnect <- odbcConnect("orecloud", uid="XXXX", pwd="XXXX", believeNRows=FALSE)

        df2 = reactive({
          # Created DB Connection to get details 
          input_var <- input$CPAN
          my_query <- paste("select CPAN,SCTP,MARKER,RESULT from CYTOKINE where CPAN= ", input_var)
          sqlQuery(dbconnect,my_query)
          cleandata <- na.omit(data)
          colnames(cleandata)
          Markers <- unique(cleandata[,"MARKER"])
           nMarkers <- length(Markers)
          nMarkers
          Patients <- unique(cleandata[,"CPAN"])
          nPatients <- length(Patients)
          nPatients
         output <- data.frame("Marker","Patient", "Min","Q1","Median","Mean","Q3","Max",stringsAsFactors=FALSE,check.names=FALSE)
          output
          for (i in 1:nMarkers) {
            Mrk <- Markers[i]    
            for (j in 1:nPatients){
              Pnt <- Patients[j]
              Mrkdata  <- cleandata[which(cleandata[,'MARKER']== Mrk),]
              Ptntdata <- Mrkdata[which(Mrkdata[,'CPAN']==Pnt), ]
              output[{i-1}*nPatients+j,1] <- Mrk
              output[{i-1}*nPatients+j,2] <- Pnt
              for (k in 1:6){
                output[{i-1}*nPatients+j,k+2] <- summary(Ptntdata[,'RESULT'])[k]
                #histogram(~Mrkdata [,'RESULT'])
              }
              #output[j,9]<- quantile(Mrkdata[,'RESULT'],c(.25,.75))[1]
              #output[j,10]<- quantile(Mrkdata[,'RESULT'],c(.25,.75))[2]
            }        
          } 
        })
        output$tableoutput <- renderDataTable({df2()}) 
           } )

1 个答案:

答案 0 :(得分:0)

看看是否有效:

df2 <- reactive({
  # Created DB Connection to get details 
  input_var <- input$CPAN
  my_query <- paste("select CPAN,SCTP,MARKER,RESULT from CYTOKINE where CPAN= ", input_var)
  sqlQuery(dbconnect,my_query)
  cleandata <- na.omit(data)
  colnames(cleandata)
  Markers <- unique(cleandata[,"MARKER"])
  nMarkers <- length(Markers)
  Patients <- unique(cleandata[,"CPAN"])
  nPatients <- length(Patients)
  output <- data.frame("Marker","Patient", "Min","Q1","Median","Mean","Q3","Max",stringsAsFactors=FALSE,check.names=FALSE)

  for (i in 1:nMarkers) {
    Mrk <- Markers[i]    
    for (j in 1:nPatients){
      Pnt <- Patients[j]
      Mrkdata  <- cleandata[which(cleandata[,'MARKER']== Mrk),]
      Ptntdata <- Mrkdata[which(Mrkdata[,'CPAN']==Pnt), ]
      output[{i-1}*nPatients+j,1] <- Mrk
      output[{i-1}*nPatients+j,2] <- Pnt
      for (k in 1:6){
        output[{i-1}*nPatients+j,k+2] <- summary(Ptntdata[,'RESULT'])[k]
      }
    }        
  }
  output
})


output$tableoutput <- renderTable({
    data.table(df2())
})

此外,在UI中,进行此更改:

mainPanel(
        tableOutput("tableoutput")
    )