首页 > 解决方案 > 在 Shiny 中将输入下拉菜单作为侧边栏

问题描述

我想将我的输入面板作为左侧的侧边栏,而不是在下方。我试过了,sidebarLayout但确实让它工作了。有什么建议么?

输入

dput(df1.t)
structure(list(`hsa-let-7a-3p` = c(5.58182112427671, 5.21705272399953, 
5.42864356356758, -1.1383057411356, 5.06203248358181), `hsa-let-7a-5p` = c(17.0260439263402, 
15.2857710138151, 17.1420214989373, 15.1034766165351, 14.5449390552056
), `hsa-let-7b-3p` = c(4.28580929310353, 2.46805733598209, 5.15298557165018, 
4.63298501632773, -0.398732335974934), `hsa-let-7b-5p` = c(13.0477955183433, 
10.880357260433, 12.2652935281359, 11.1312184397251, 7.45844929748327
), `hsa-let-7c-3p` = c(-1.25421892418566, -1.27175388669896, 
-1.33049811927568, -1.1383057411356, 2.24661989371122)), class = "data.frame", row.names = c("86", 
"175", "217", "394", "444"))

dput(df2.t)
structure(list(A1BG = c(-1.19916952580483, -3.10305950914023, 
-3.10305950914023, -3.10305950914023, -0.982321345582416), `A1BG-AS1` = c(0.102743641800478, 
-2.24464338564074, -3.10305950914023, -1.92943473454654, -0.652692243977369
), A1CF = c(-3.10305950914023, -2.24464338564074, -2.18375470186949, 
-1.92943473454654, -2.30044547836697), A2M = c(3.42115035042484, 
-0.0469232989985426, 6.84141032645296, 5.78124672930663, 2.53353451576527
), `A2M-AS1` = c(-2.03481283871687, -3.10305950914023, -2.18375470186949, 
-3.10305950914023, -2.64664301822906)), class = "data.frame", row.names = c("86", 
"175", "217", "394", "444"))

脚本

ui <- fluidPage(
  titlePanel("MicroRNA-mRNA correlation plot"),
  mainPanel(
    plotOutput("plot")
  ),
  selectInput(inputId ="data1",
              label = "Choose Gene",
              choices = names(df2.t),
              selected = NULL
  ),
  selectInput(inputId ="data2",
              label = "Choose MicroRNA",
              choices = names(df1.t),
              selected = NULL
  ),
  textOutput("result"))

server <- function(input,output){
  data <- eventReactive(c(input$data1,input$data2),{
    data <- data.frame(df1.t[[input$data2]], df2.t[[input$data1]])
    colnames(data) <- c("col1", "col2")
    data
  })
  
  output$plot <- renderPlot({
    ggplot(data(),aes(x=col2,y=col1)) +
      geom_point(colour='black') +
      labs(x = paste(input$data1,"(cpm, log2)"), y = paste(input$data2,"(cpm, log2)")) +
      theme_classic(base_size = 12) +
      geom_smooth(method="lm",se = F) +
      stat_cor()
    
  }, height = 400, width = 600)
  
}


shinyApp(ui=ui,server=server)

标签: shiny

解决方案


尝试这个:

ui <- fluidPage(
  titlePanel("MicroRNA-mRNA correlation plot"),
  
  sidebarLayout(
    sidebarPanel(
      selectInput(inputId ="data1",
                  label = "Choose Gene",
                  choices = names(df2.t),
                  selected = NULL
      ),
      selectInput(inputId ="data2",
                  label = "Choose MicroRNA",
                  choices = names(df1.t),
                  selected = NULL
      )  
    ),
    mainPanel(
      plotOutput("plot"),
      textOutput("result")
    )
  )
)

推荐阅读