如何在NavBar页面+ Rshiny应用程序中将TableTable输出输出到tabPanel

时间:2018-09-28 13:37:46

标签: r shiny shinydashboard

我正在构建一个仪表板页面,用户在该页面上载文件,然后单击操作按钮,它将运行服务器代码并显示输出,还允许将输出下载为文件。下面是显示基本UI的代码。

我需要服务器功能方面的帮助,以将服务器功能中的命令的输出呈现到NavBar页面中的“ Table”输出,其中UI中可以显示前5行,然后单击,下载完整的输出文件。 “下载列表”按钮。我是rshiny的新手。任何帮助都会有所帮助。

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) 

或者在actionButton完成时将当前在“结果”菜单(navbarPage)中显示在仪表盘上的sidebarMenu中将是理想的。

1 个答案:

答案 0 :(得分:0)

有一个错字:output$Table应该是output$table来引用表格,而不是包含表格的标签。另外,要从fileInput加载文件,您需要访问input$PE$datapath

我构造此方法的方式是使用eventReactive触发的actionButton来加载数据并使它可用作{{1 }}

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() }) } 表达式来设置downloadHandler函数。 table_content()还有很多其他问题,因此我将不做详细介绍。


如果要在单击时将downloadHandler按钮更改为结果标签,则需要做两件事:

首先,向您的input$Start添加一个ID:

sidebarMenu

第二,设置sidebar <- dashboardSidebar( sidebarMenu(id = 'tabs', ... ,将选定的标签更改为updateTabItems。由于您使用的是results,因此您想使用shinydashboard,而不是this question中的shinydashboard::updateTabItems。由于您想在加载表格内容时更改标签,因此我将通过添加以下内容来使shiny:: updateTabsetPanel对触发器进行响应:

table_content()

现在,当更改observeEvent(table_content(),{ updateTabItems(session, "tabs", 'results') }) 时,选项卡将切换到table_content()。如果您的results中出现问题,并且无法正确读取或处理文件,则该标签将不会切换。