Search code examples
rshinyshinydashboardshinyappsshiny-reactivity

Using fluidpage and column layout for RShiny dashboard


I will like to use the code below to output a dashboard with 2 panels on the left (Top panel: input file; Bottom panel: action plot) and plot (main page) on the right. Currently, the code outputs the two panels at the top part of dashboard and plot at the bottom, and this is not what I want. I'm new to Rshiny and need help.

Code:

library(shiny)
library(pheatmap)

# Define UI for dataset viewer app ----
ui <- fluidPage(

           # App title ----
       titlePanel("plot"),

# Sidebar layout with input and output definitions ----
   sidebarLayout(

# Sidebar panel for inputs ----
   sidebarPanel("Sidebar panel",

  # Input: Selector for choosing dataset ----
  fileInput("file1", "Choose CSV File",
              accept = c(
                "text/csv",
                "text/comma-separated-values,text/plain",
            ".csv")
    ),
    tags$hr(),
    checkboxInput("header", "Header", TRUE)
),

   # tags$hr(),

     sidebarPanel('get heatmap',
              actionButton('getHmap', 'get heatmap'))
),

# Main panel for displaying outputs ----
mainPanel("Plot",
          #column(6, 
             plotOutput("themap"),
             tableOutput("table.output"))
          #)
  )


server = function(input, output, session) {
  a <- reactive({
    inFile <- input$file1
    if (is.null(inFile))
      return(NULL)
tbl <- read.csv(inFile$datapath, header=input$header) #, sep=input$sep,  dec = input$dec)
return(tbl)
  })

  output$table.output <- renderTable({
    a()
  })

  plotdata <- eventReactive(input$getHmap, {
    a <- as.matrix(a()[-1])
    row.names(a) <- a()$ID
    a[is.na(a)] <- 0
    a
   })

 output$themap = renderPlot({ 
    pheatmap(plotdata())
  })
}

shinyApp(ui, server)

Does anyone know how to use Rshiny fluidpage and columns well and can help?


Solution

  • I'm not entirely sure what you want, but you do have two sidebarPanels in your UI, which isn't helping. How about:

    ui <- fluidPage(
      titlePanel("plot"),
      sidebarLayout(
        sidebarPanel("Sidebar panel",
                      # Input: Selector for choosing dataset ----
                      fileInput("file1", "Choose CSV File",
                               accept = c(
                                 "text/csv",
                                 "text/comma-separated-values,text/plain",
                                 ".csv")
                     ),
                     tags$hr(),
                     checkboxInput("header", "Header", TRUE),
                     actionButton('getHmap', 'get heatmap')
        ),
        mainPanel("Plot",
                  #column(6, 
                  plotOutput("themap"),
                  tableOutput("table.output")
        )
      )
    )
    

    as your UI?

    ** Edit **

    If this isn't what you want, as per your comment below, then please provide more information. Here's a screenshot of what this layout gives me:

    enter image description here

    which has your plot to the right at the top of the main panel and two controls in the panel to the left: the input file at the top and an action button below. That seems to match your spec in the question. (Although I wasn't sure what you meant by "action plot", so I assumed you meant "action button" as that's what your sample code had.) Note that the plot doesn't actually appear because I haven't installed the pheatmap package and you haven't provided any test data.