-1

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.

chas
  • 1,565
  • 5
  • 26
  • 54

1 Answers1

0

There's a typo: output$Table should be output$table to refer to the table, not the tab holding it. Also, to load a file from fileInput, you need to access input$PE$datapath

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$datapath)
        a <- read.delim(input$PE$datapath,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.


If you want the input$Start button to change to the results tab when clicked, you need to do 2 things:

First, add an id to your sidebarMenu:

sidebar <- dashboardSidebar(
    sidebarMenu(id = 'tabs',
    ...

Second, set up updateTabItems to change the selected tab to results. Since you're using shinydashboard, you want to use shinydashboard::updateTabItems, not shiny:: updateTabsetPanel as in this question. Since you want to change tabs when the table content is loaded, I'd make the table_content() reactive the trigger by adding this:

observeEvent(table_content(),{
    updateTabItems(session, "tabs", 'results')
})

Now, when table_content() is changed, the tab will be switched to results. If something goes wrong in your eventReactive and the file cannot be read or processed properly, then the tab won't switch.

divibisan
  • 11,659
  • 11
  • 40
  • 58
  • it works. However, as you see the "Result" menu has to be clicked from the dashboard sidemenu inroder to see the output after the actionButton. Is there a way to get the "Result" page displayed on the click of actionButton? – chas Sep 28 '18 at 15:53
  • @chas This question shows you how to do that: https://stackoverflow.com/questions/38706965/is-there-any-way-for-an-actionbutton-to-navigate-to-another-tab-within-a-r-shi – divibisan Sep 28 '18 at 15:54
  • thanks!! It seems to answer my question but not yet. Should i remove the "Result" menu from the sidebarmenu and include navbar page only in dahsboard body? i am confused what to do!!! could you give a pseudo code if not complete solution. – chas Sep 28 '18 at 16:14
  • since we have used evenReactive on the same actionButton i wonder how to use the observeEvent again on the same button and the order where the code need to be placed – chas Sep 28 '18 at 16:23
  • one final query if you have time. We now see "Results" in the sidebarmenu by default. Would it be possible to remove in the sidebarmenu and make the "Results" navbarpage appear in the body when `observeEvent` function is executed? – chas Sep 28 '18 at 17:33
  • I'm, not sure what you mean by that. If you have followup questions, it might make sense to ask a new question focused on that. – divibisan Sep 28 '18 at 17:35
  • this may be too naive query to make a new question. I mean to say that when the app is run, the Results menu is visible in the sidebarmenu. And when you click on the Results menu it will show the "Table" tab which will be empty because we haven't uploaded the file and haven't hit actionButton. Would it be possible to make the Results Menu invisible in the sidebar and make it visible in the body when the actionButton is executed? – chas Sep 28 '18 at 17:48
  • On the contrary: simple, clear questions like that are ideal! I'm not sure how best to do this, but [this question might help you](https://stackoverflow.com/questions/41738927/show-hide-button-on-tab-select-r-shiny). If not, I'd ask a new question just focused on that – divibisan Sep 28 '18 at 17:56