首页 > 解决方案 > 选择输入启用或禁用操作按钮 - 有问题

问题描述

标签: rshinyshinyjs

解决方案


试试这个

ui <- fluidPage(
  shinyjs::useShinyjs(),
  
  fluidRow(
    column(8,offset = 3,
           h2("Gene_FPKM Value Barplot")
    )
  ),
  fluidRow(
    column(8,offset = 3,
           selectInput(
             "selectGeneSymbol", 
             "Select Gene Symbol:", 
             choices = unique(data_mean_sd$Gene),
             multiple =F,
             width = 800,
             selected = "Igfbp7"
           ))
  ),
  fluidRow(
    column(8,offset = 3,
           actionButton(inputId = "plot1", label = "FPKM",width=80),
           actionButton(inputId = "plot2", label = "LogFc",width=80),
           actionButton(inputId = "all",label = "FPKM&LogFc",width=120)
    )
  ),
  fluidRow(
    column(3)
  ),
  fluidRow(
    column(3)
  ),
  fluidRow(
    column(12,align="center",
           uiOutput("plots")
    )
  )
)

server <- function(input, output,session) {
  
  observeEvent(input$selectGeneSymbol, {
    if(sum(unique(data_mean_sd2$Gene) %in% input$selectGeneSymbol)>0) {
      shinyjs::enable("plot2")
      shinyjs::enable("all")
    }else{
      shinyjs::disable("plot2")
      shinyjs::disable("all")
    }
  })
  
  plot_data1 <- eventReactive(list(input$plot1,input$all), { 
    subset(data_mean_sd, Gene %in% input$selectGeneSymbol)
  })
  
  plot_data2 <- eventReactive(list(input$plot2,input$all), { 
    subset(data_mean_sd2, Gene %in% input$selectGeneSymbol)
  })
  
  global <- reactiveValues()
  
  observeEvent(list(input$plot1,input$all), {
               req(plot_data1())
  #p1 <- eventReactive(list(input$plot1,
  #                         input$all), {
                global$p1 <- ggplot(data = plot_data1(), aes(x = Name, y = mean,fill=Name)) +
                               geom_bar(stat = "identity", position = position_dodge(0.9), width = 0.9) +
                               geom_errorbar(aes(ymin = mean - sd, ymax = mean + sd), width = .2, position = position_dodge(0.9)) +
                               theme_classic2() +
                               rotate_x_text(angle = 45) +
                               theme(legend.position = "none") +
                               labs(title = input$selectGeneSymbol, x = NULL, y = "FPKM_value") +
                               theme(plot.title = element_text(hjust = 0.5)) +
                               theme(plot.margin = unit(c(20, 5, 1, 5), "mm"))+
                               theme(axis.text.x=element_text(vjust=1,size=12)) 
                           })  
  
  observeEvent(list(input$plot2,input$all), {
               req(plot_data2())
  #p2 <- eventReactive(list(input$plot2,
  #                         input$all), {
                global$p2 <- ggplot(data = plot_data2(), aes(x = Name, y = mean,fill=Name)) +
                               geom_bar(stat = "identity", position = position_dodge(0.9), width = 0.9) +
                               geom_errorbar(aes(ymin = mean - sd, ymax = mean + sd), width = .2, position = position_dodge(0.9)) +
                               theme_classic2() +
                               rotate_x_text(angle = 45) +
                               theme(legend.position = "none") +
                               labs(title = input$selectGeneSymbol, x = NULL, y = "FPKM_value") +
                               theme(plot.title = element_text(hjust = 0.5)) +
                               theme(plot.margin = unit(c(20, 5, 1, 5), "mm"))+
                               theme(axis.text.x=element_text(vjust=1,size=12)) 
                           })  
  
  output$plt1 <- renderPlot({ global$p1 })
  output$plt2 <- renderPlot({ global$p2 })
  output$plt3 <- renderPlot({ grid.arrange(global$p1,global$p2, ncol=1) })
  
  observeEvent(input$plot1, {
    global$out <- plotOutput("plt1", height=600)
  })
  
  observeEvent(input$plot2, {
    global$out <- plotOutput("plt2", height=600)
  })
  
  observeEvent(input$all, {
    global$out <- plotOutput("plt3", height=800)
  })
  
  
  output$plots <- renderUI({
    global$out
  })
  
}

# Create Shiny app ----
shinyApp(ui = ui, server = server)

推荐阅读