如何在Rshiny中提供数据?

时间:2018-10-01 17:10:29

标签: r shiny rstudio

我正在尝试使R闪亮的数据可用于生存分析。换句话说,某些人可能无权使用R Shiny上传数据。对于那些人,我想提供一些数据集,以便他们可以使用数据。有谁知道如何在R Shiny中提供一些数据?这是代码:

library(shiny)
library(datasets)
library(survival)

options(shiny.maxRequestSize = 70 * 1024 ^ 2)
shinyUI(fluidPage(
titlePanel("Data Visualization"),
tabsetPanel(
tabPanel(
  "Upload File",
  titlePanel("Uploading Files"),
  sidebarLayout(
    sidebarPanel(
      fileInput(
        'file1',
        'Choose RData File',
        accept = c(".Rdata")
      ), 
       selectInput(
         "dataset", "Select the dataset", choices = c("LUAD_RNASeq", 
   "LUSC_RNASeq")
      )
    ),
    mainPanel(
      p("Head of the data"),
      tableOutput('contents'))
  )
),
tabPanel(
  "Summary",
  pageWithSidebar(
    headerPanel('Summary of Variable'),
    sidebarPanel(
      # "Empty inputs" - they will be updated after the data is 
   uploaded
      #selectInput('row', 'Row', ""),
      selectInput('col', 'Column', "", selected = "")

    ),
    mainPanel(
      #p("Summary for the row"),
      #verbatimTextOutput("row_summary"),
      p("Summary for the column"),
      verbatimTextOutput("col_summary")
    )
    )
  ),
  tabPanel(
    "Survival",
  plotOutput("surPlot")
),

 tabPanel(
  "Scatter Plot",
  pageWithSidebar(
    headerPanel('Scatter Plot'),
    sidebarPanel(
      selectInput('col1', 'Column1', "", selected = ""),
      selectInput('col2', 'Column2', "", selected = "")
    ),

    mainPanel(
      plotOutput("scaPlot")
    )
  )
),
tabPanel(
  "Univariate Cox Regression",
  pageWithSidebar(
    headerPanel('Univariate Cox Regression'),
    sidebarPanel(
      selectInput('col3', 'Cox Variable', "", selected = "")
    ),

    mainPanel(
      verbatimTextOutput("reg_summary"),
      plotOutput("reg_Plot")
    )
  )
),
tabPanel(
  "AFT Regression",
  pageWithSidebar(
    headerPanel('Log normal AFT regression'),
    sidebarPanel(
      selectInput('col4', 'AFT Variable', "", selected = "")
    ),

    mainPanel(
      p("To do the AFT regression, the survival time can not be 
    zero"),
      verbatimTextOutput("aft_summary")
      #plotOutput("aft_Plot")
    )
  )
),
tabPanel(
  "Glmnet Analysis",
  p("To do Glmnetplot, the survival time can not be zero or NA 
value"),
  plotOutput("GlmnetPlot")
)

)
))



library(shiny)
library(datasets)
library(survival)
library(ggplot2)
library(survminer)
library(GGally)
library(glmnet)
library(rsconnect)


options(shiny.maxRequestSize = 70 * 1024 ^ 2)
load_obj <- function(f)
{
    env <- new.env()
nm <- load(f, env)[1]
 env[[nm]]
}
shinyServer(function(input, output, session) {


data <- reactive({
  req(input$file1)
  df<-load_obj(input$file1$datapath)$merged.dat

# Update inputs (you could create an observer with both updateSel...)
# You can also constraint your choices. If you wanted select only 
numeric
# variables you could set "choices = sapply(df, is.numeric)"
# It depends on what do you want to do later on.

updateSelectInput(
  session,
  inputId = 'row',
  label = 'Row',
  choices = 1:nrow(df),
  selected = 1
)
updateSelectInput(
  session,
  inputId = 'col',
  label = 'Column',
  choices = names(sapply(df, is.numeric)),
  selected = names(df)[2]
)
updateSelectInput(
  session,
  inputId = 'col1',
  label = 'Column1',
  choices = names(sapply(df, is.numeric)),
  selected = names(df)[4]
)
updateSelectInput(
  session,
  inputId = 'col2',
  label = 'Column2',
  choices = names(sapply(df, is.numeric)),
  selected = names(df)[5]
)
updateSelectInput(
  session,
  inputId = 'col3',
  label = 'Cox Variable',
  choices = names(sapply(df, is.numeric)),
  selected = names(df)[5]
 )
updateSelectInput(
  session,
  inputId = 'col4',
  label = 'AFT Variable',
  choices = names(sapply(df, is.numeric)),
  selected = names(df)[5]
)
return(df)

   })

   output$contents <- renderTable({
df<-data()
return (head(df[,1:10]))
  })

 output$row_summary <- renderPrint({
  df<-data()
  row<-as.numeric(df[input$row, 2:ncol(df)])
  summary(row)
   })

  output$col_summary <- renderPrint({
   df<-data()
   column<-df[,input$col]
   summary(column)
 })


 output$surPlot <- renderPlot({

  plot.survival <- function(data)
  {
    ggsurv(survfit(
      Surv(data$OS, data$status) ~ 1,
      type = "kaplan-meier",
      conf.type = "log-log"
    ),
    main = "Survival Plot(K-M estimate)")

}

print(plot.survival(data()))

})

 output$scaPlot <- renderPlot({
df<-data()
column1<-df[,input$col1]
column2<-df[,input$col2]
plot(column1, column2)
  })

 output$reg_summary <-renderPrint({
df=data()
column3=df[,input$col3]

res.cox=coxph(Surv(df$OS,df$status) ~ column3, data=df)
summary(res.cox)
    })

 output$reg_Plot <- renderPlot({
df=data()
column3=df[,input$col3]
res.cox=coxph(Surv(df$OS,df$status) ~ column3, data=df)
ggsurvplot(survfit(res.cox), palette = "#2E9FDF",ggtheme = 
theme_minimal(),data = df)
})

 output$aft_summary <-renderPrint({
  df=data()
  column4=df[,input$col4]
res.aft=survreg(Surv(df$OS,df$status) ~ column4, data=df, 
 dist="lognormal")
summary(res.aft)
})

  output$GlmnetPlot <- renderPlot({
  df=data()
  y1=cbind(time=df$OS,status=df$statu)
  x1 <- subset(df, select = -c(bcr,OS, status))
    x1=data.matrix(x1, rownames.force = NA)
  fit1=glmnet(x1,y1,family="cox")
  plot(fit1)
   })

  # datasetInput <- eventReactive(input$update, {
  #   switch(input$dataset,
  #          "rock" = rock,
  #          "pressure" = pressure,
  #          "cars" = cars)
  # })


  #  output$aft_Plot <- renderPlot({
  #    df=data()
  #    df=df[-c(which(df$OS==0)),]
  #    column4=df[,input$col4]
  #    res.aft=survreg(Surv(df$OS,df$status) ~ column4, data=df, 
 dist="lognormal")
  #    ggsurvplot(survfit(res.aft), color = "#2E9FDF",ggtheme = 
theme_minimal(),data = df)
  #  })

   })

1 个答案:

答案 0 :(得分:0)

如果您的数据包含CSV文件,请将其包含在您的Shiny应用程序所在的文件夹中,然后致电:

mydata <- read.csv("your_file.csv")

如果将数据另存为R变量和Rds:

mydata <- readRDS("your_file.Rds")