【问题标题】:How to RenderTable output to tabPanel in NavBar page + Rshiny app如何在 NavBarpage + R Shiny 应用程序中将表格输出渲染到 tabPanel
【发布时间】:2019-03-04 12:42:39
【问题描述】:

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

我需要有关服务器功能的帮助,以将服务器功能中命令的输出呈现到导航栏页面中的“表格”输出,其中前 5 行可以显示在 UI 中,并在单击时下载完整的输出文件“下载列表”按钮。我是 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完成时将当前在sidebarMenu中的“结果”菜单(navbarPage)显示到dashboardBody会比较理想。

【问题讨论】:

    标签: r shiny shinydashboard


    【解决方案1】:

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

    我的结构方式是使用由actionButton 触发的eventReactive 来加载数据并使其可用作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 按钮在单击时变为结果选项卡,您需要做两件事:

    首先,给你的sidebarMenu添加一个id:

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

    其次,设置updateTabItems,将选中的标签改为results。由于您使用的是shinydashboard,因此您想使用shinydashboard::updateTabItems,而不是shiny:: updateTabsetPanel,如this question。由于您想在加载表格内容时更改选项卡,我将通过添加以下内容使 table_content() 反应触发器:

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

    现在,当table_content() 更改时,选项卡将切换到results。如果您的eventReactive 出现问题并且无法正确读取或处理文件,则选项卡将不会切换。

    【讨论】:

    • 它有效。但是,正如您所见,必须从仪表板侧边菜单中单击“结果”菜单才能看到 actionButton 之后的输出。有没有办法让点击actionButton时显示的“结果”页面?
    • @chas 这个问题告诉你如何做到这一点:stackoverflow.com/questions/38706965/…
    • 谢谢!!它似乎回答了我的问题,但还没有。我应该从侧边栏菜单中删除“结果”菜单并仅在仪表板正文中包含导航栏页面吗?我很困惑该怎么办!!!如果没有完整的解决方案,你能给出一个伪代码吗?
    • 因为我们在同一个 actionButton 上使用了 evenReactive,我想知道如何在同一个按钮上再次使用 observeEvent 以及代码需要放置的顺序
    • 如果您有时间,请进行最后一次查询。我们现在默认在侧边栏菜单中看到“结果”。执行observeEvent函数时,是否可以在侧边栏菜单中删除并使“结果”导航栏页面出现在正文中?
    猜你喜欢
    • 2019-02-11
    • 2021-07-29
    • 2018-08-03
    • 2014-06-08
    • 2022-01-15
    • 2021-03-30
    • 2022-12-21
    • 1970-01-01
    • 2018-06-12
    相关资源
    最近更新 更多