简体   繁体   中英

How to RenderTable output to tabPanel in NavBar page + Rshiny app

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM