R 使用两个显示不同绘图的不同数据集创建一个闪亮的应用程序

R 使用两个显示不同绘图的不同数据集创建一个闪亮的应用程序,r,shiny,shiny-server,shinyapps,R,Shiny,Shiny Server,Shinyapps,我有两种类型的数据集df.m和df2。对于数据集df.m和df2我分别为每个数据集制作了一个应用程序,输出给出了不同的图。下面我展示了这些应用程序的数据集和服务器端。这两个应用的用户界面ui是相同的,为了复制,我只是在这里展示一下 这两款应用都运行良好,没有任何问题 library(reshape2) library(ggplot2) library(ggpubr) library(EnvStats) library(shinyjs) library(shiny) library(shinyj

我有两种类型的数据集
df.m
df2
。对于数据集
df.m
df2
我分别为每个数据集制作了一个应用程序,输出给出了不同的图。下面我展示了这些应用程序的数据集和服务器端。这两个应用的用户界面
ui
是相同的,为了复制,我只是在这里展示一下

这两款应用都运行良好,没有任何问题

library(reshape2)
library(ggplot2)
library(ggpubr)
library(EnvStats)
library(shinyjs)

library(shiny)
library(shinyjs)
library(tidyverse)
library(survminer)
library(survival)

df <- structure(list(Samples = structure(1:10, .Label = c("Sample1", 
                                                          "Sample10", "Sample2", "Sample3", "Sample4", "Sample5", "Sample6", 
                                                          "Sample7", "Sample8", "Sample9"), class = "factor"), Type = structure(c(2L, 
                                                                                                                                  1L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L), .Label = c("Normal", "Tumor"
                                                                                                                                  ), class = "factor"), A1BG = c(0, 0.01869105, 0.026705782, 0.016576987, 
                                                                                                                                                                 0, 0.007636787, 0.015756547, 0.00609601, 0.115575528, 0.04717536
                                                                                                                                  ), A1BG.AS1 = c(0, 0.096652515, 0.086710002, 0.04683499, 0.188283185, 
                                                                                                                                                  0.104318353, 0.102735593, 0.100064808, 0.04717536, 0.159745808
                                                                                                                                  ), A1CF = c(1.616942802, 1.367084444, 1.101855892, 1.3823884, 
                                                                                                                                              0.631627098, 2.407159505, 1.687449785, 1.229844138, 0.87989414, 
                                                                                                                                              0.642785868), A2M = c(3.357654845, 3.149165846, 3.654774122, 
                                                                                                                                                                    2.851143092, 2.952601867, 4.002335454, 4.123949457, 3.691343955, 
                                                                                                                                                                    3.553064673, 3.425443559), A2M.AS1 = c(0.217308191, 0.08268571, 
                                                                                                                                                                                                           0.297320544, 0.101579093, 0.020102613, 0.35578965, 0.288014115, 
                                                                                                                                                                                                           0.145352771, 0.043808388, 0.104677012), A2ML1 = c(0, 0.017949113, 
                                                                                                                                                                                                                                                             0.00984907, 0.002289616, 0, 0.002100359, 0.032146138, 0.052275569, 
                                                                                                                                                                                                                                                             0.537892142, 0), A2ML1.AS1 = c(0.631627098, 0.04717536, 1.229844138, 
                                                                                                                                                                                                                                                                                            0, 4.002335454, 0, 1.229844138, 1.229844138, 0.04717536, 0)), row.names = c(NA, 
                                                                                                                                                                                                                                                                                                                                                                        -10L), class = "data.frame")

df.m <- reshape2::melt(df, c("Samples", "Type"))
colnames(df.m)[3] <- "GENE"

df2 <- structure(list(Samples = structure(c(1L, 12L, 23L, 34L, 45L, 
                                            46L, 47L, 48L, 49L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
                                            13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 24L, 25L, 26L, 
                                            27L, 28L, 29L, 30L, 31L, 32L, 33L, 35L, 36L, 37L, 38L, 39L, 40L, 
                                            41L, 42L, 43L, 44L), .Label = c("Sample1", "Sample10", "Sample11", 
                                                                            "Sample12", "Sample13", "Sample14", "Sample15", "Sample16", "Sample17", 
                                                                            "Sample18", "Sample19", "Sample2", "Sample20", "Sample21", "Sample22", 
                                                                            "Sample23", "Sample24", "Sample25", "Sample26", "Sample27", "Sample28", 
                                                                            "Sample29", "Sample3", "Sample30", "Sample31", "Sample32", "Sample33", 
                                                                            "Sample34", "Sample35", "Sample36", "Sample37", "Sample38", "Sample39", 
                                                                            "Sample4", "Sample40", "Sample41", "Sample42", "Sample43", "Sample44", 
                                                                            "Sample45", "Sample46", "Sample47", "Sample48", "Sample49", "Sample5", 
                                                                            "Sample6", "Sample7", "Sample8", "Sample9"), class = "factor"), 
                      years = c(1.301369863, 0.4, 1.054794521, 0.134246575, 0.794520548, 
                                3.287671233, 3.646575342, 3.887671233, 3.646575342, 3.619178082, 
                                3.575342466, 2.02739726, 3.523287671, 3.742465753, 2.926027397, 
                                3.8, 1.161643836, 1.380821918, 3.087671233, 3.104109589, 
                                3.084931507, 2.887671233, 2.778082192, 2.728767123, 3.043835616, 
                                1.210958904, 2.704109589, 2.742465753, 2.635616438, 2.536986301, 
                                2.432876712, 2.794520548, 1.967123288, 1.84109589, 1.838356164, 
                                2.726027397, 2.430136986, 2.257534247, 1.876712329, 2.010958904, 
                                0.698630137, 2.090410959, 2.098630137, 2.01369863, 1.717808219, 
                                1.81369863, 2.057534247, 2.032876712, 1.989041096), patient.vital_status = c(0L, 
                                                                                                             0L, 0L, 1L, 1L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 
                                                                                                             1L, 1L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 
                                                                                                             0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
                                                                                                             0L, 0L, 0L), A1BG = structure(c(1L, 1L, 2L, 2L, 1L, 2L, 2L, 
                                                                                                                                             1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 2L, 
                                                                                                                                             2L, 1L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 2L, 2L, 1L, 
                                                                                                                                             1L, 1L, 2L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L), .Label = c("high", 
                                                                                                                                                                                                         "low"), class = "factor"), A1CF = structure(c(1L, 1L, 1L, 
                                                                                                                                                                                                                                                       1L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 
                                                                                                                                                                                                                                                       2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                                                                                                                                                                                                                       1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                                                                                                                                                                                                                                       2L), .Label = c("high", "low"), class = "factor")), class = "data.frame", row.names = c(NA, 
                                                                                                                                                                                                                                                                                                                                               -49L))


ui <- fluidPage(
  titlePanel("plots"),
  sidebarLayout(
    sidebarPanel(
      selectInput(inputId = "thegene", label = "Gene", choices = c("FAP", "ADSL", "SIX2"), selected = "SIX2"),
      radioButtons(inputId = "FileType", label = "Select the file type", choices = list("png", "pdf"), selected = "pdf"),
      width = 3
    ),
    mainPanel(
      plotOutput("plot"),
      downloadButton(outputId = "downloadPlot", label = "Download the plot"),
      width = 9
    )
  )
)


server <- function(input, output, session) {
  vals <- reactiveValues()
  alldat <- reactive({
    choices <- unique(df.m$GENE)
    selected <- isolate(input$thegene)
    if (!selected %in% choices) selected <- choices[1]
    updateSelectInput(session, "thegene", choices = choices, selected = selected)
    df.m
  })

  dat <- reactive({
    x <- alldat()
    x[ x$GENE == input$thegene,,drop=FALSE]
  })

  output$plot <- renderPlot({
    gg <- ggplot(data = dat(), aes(x = Type, y = value, fill=GENE)) +
      geom_boxplot() +
      theme_bw(base_size = 14) + xlab("") + ylab("Expression logFPKM") +
      theme(axis.text=element_text(size=15, face = "bold", color = "black"),
            axis.title=element_text(size=15, face = "bold", color = "black"),
            strip.text = element_text(size=15, face = "bold", color = "black")) +
      scale_fill_manual(values = input$colour) + 
      stat_compare_means(method = "t.test", size=5) +
      stat_n_text()

    vals$gg <- gg

    print(gg)
  })

  output$downloadPlot <- downloadHandler(
    filename =  function() {
      paste(input$thegene, input$FileType,sep=".")
    },
    # content is a function with argument file. content writes the plot to the device
    content = function(file){
      if(input$FileType=="png")
        png(file, units="in", width=5, height=5, res=300)
      else
        pdf(file, width = 5, height = 5)
      print(vals$gg)
      dev.off()
    } 
  )
}

# Run the application 
shinyApp(ui = ui, server = server)

#############################################################################################


server <- function(input, output, session) {
  genes <- as.factor(names(df2[c(4:5)]))
  env <- parent.frame()

  vals <- reactiveValues()

  alldat <- reactive({
    choices <- genes
    selected <- isolate(input$thegene)
    if (!selected %in% choices) selected <- choices[1]
    updateSelectInput(session, "thegene", choices = choices, selected = selected)
    df2
  })

  dat <- reactive({
    x <- alldat()
    x[ x$variable == input$thegene,,drop=FALSE]
  })

  output$plot <- renderPlot({

    fit <- eval(parse(text = paste0("survfit(Surv(years, patient.vital_status) ~ ", input$thegene, ", data = final)")))

    gg <-ggsurvplot(fit,
                    data = final,
                    pval = TRUE, conf.int = FALSE,
                    risk.table = TRUE, # Add risk table
                    risk.table.col = "strata", # Change risk table color by groups
                    linetype = "strata", # Change line type by groups
                    surv.median.line = "hv", # Specify median survival
                    ggtheme = theme_bw(), # Change ggplot2 theme
                    palette = c("#FF0027", "#060606"),
                    xlim = c(0,10),
                    break.x.by = 3,
                    xlab="Time in years",
                    risk.table.y.text.col = T, # colour risk table text annotations.
                    risk.table.y.text = FALSE)

    vals$gg <- gg
    print(gg)
  })

  output$downloadPlot <- downloadHandler(
    filename =  function() {
      paste(input$thegene, input$FileType,sep=".")
    },
    # content is a function with argument file. content writes the plot to the device
    content = function(file){
      if(input$FileType=="png")
        png(file, units="in", width=5, height=5, res=300)
      else
        pdf(file, width = 5, height = 5, onefile = FALSE)
      print(vals$gg)
      dev.off()
    } 
  )
}

# Run the application 
shinyApp(ui = ui, server = server)
library(重塑2)
图书馆(GG2)
图书馆(ggpubr)
图书馆(环境统计)
图书馆(shinyjs)
图书馆(闪亮)
图书馆(shinyjs)
图书馆(tidyverse)
图书馆(survminer)
图书馆(生存)

df请提交一个最小可复制示例。您需要包括ui以便我们复制它。是的,我现在也添加了。请检查一下。你的代码完全令人困惑。你可以看看这里。