I am building a dashboard page where the user uploads a file and on clicking the actionbutton it should run the server code and show the output and also allow to download the output as file. Below is the code that shows the basic UI.

I would need help with the server function to render the output from the command in server function to the "Table" output in the NavBar page where first 5 rows could be shown in the UI and download the complete output file on clicking the "Download List" button. I am novice with rshiny. Any help would be helpful.

library(shiny)
library(shinydashboard)

sidebar <- dashboardSidebar(
sidebarMenu(
menuItem("Documentation", tabName = "documentation",selected=FALSE),
menuItem("Dataset", tabName = "dataset", badgeColor = "green"),
menuItem("Result", tabName = "results", badgeColor = "green")
))




body <- dashboardBody(
tabItems(
tabItem(tabName = "documentation",h3("Tool Documentation")),
tabItem(tabName = "dataset",menuItem(icon = NULL, fileInput("PE", "Upload input file:")),
menuSubItem(icon = icon("refresh"),actionButton("Start","Analyze"))),

tabItem(tabName = "results",navbarPage(tabPanel("summary","Summary",icon = icon("list-alt")),
                   tabPanel("Table",tableOutput("table"),icon = icon("table")),
                            downloadButton("downList", "Download List")))))



# Put them together into a dashboardPage
ui <- dashboardPage(dashboardHeader(title = "FanDB"),
          sidebar,
          body)

# Define server logic 
server <- function(input, output, session) {
##run this command on input$PE file on the click of actionButton

  output$Table <- renderTable({ 
  input$Start
  req(input$PE)
  a<-read.delim(input$PE,sep="\t",header=T)
  b<-a[a[,6]==2,1]
  {
  return(b)
  }

   #Show the results from the actionButton in the Table panel in the navbar page and download the results using downloadButton
 })

}

shinyApp(ui, server) 

Or displaying the "results" menu (navbarPage) which is currently in the sidebarMenu to the dashboardBody on the completion of actionButton would be ideal.

The way I'd structure this is to use an eventReactive, which is triggered by the actionButton, to load the data and make it available as a reactive expression which is used by renderTable

server <- function(input, output, session) {

    # When button is pressed, load data and make available as reactive expression
    table_content <- eventReactive(input$Start, {
        req(input$PE)
        a <- read.delim(input$PE,sep="\t",header=T)
        b <- a[a[,6]==2,1]
        return(b)
    })

    # Render data as table
    #   Since table_content is reactive, the table will update when table_content changes 
    output$Table <- renderTable({
        table_content()
    })
}

To download the table, you can just set up a downloadHandler function with this same table_content() expression as the content. There are a bunch of other questions on downloadHandler, so I won't go into detail on that.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.