首页 > 解决方案 > 如何在 shinyR 应用程序中使用 renderUI 创建的那些选项卡下添加动态内容

问题描述

我正在用闪亮的 R 构建一个应用程序,其中用户可以选择所需的选项卡,并且与这些选项卡相关的数据将显示在它下面。

例如,在下面的示例应用程序中,.csv 中的 mtcars 数据将被接受为输入参数。用户可以在选项卡字段中选择所需的列名。这些列将被创建为选项卡。

现在,我想在适当的选项卡中显示与 .csv 中的该列有关的数据。比如说,“mpg”列中的数据将显示在“mpg”选项卡下。

But i am stuck here.Appreciate someone could tell me a way to display data from relevant column under appropriate tab ,dynamically.

使用的示例代码如下所示:

write.csv(mtcars,'mtcars.csv')

#
library(shiny)
library(plyr)
library(dplyr)

ui <- pageWithSidebar(
    headerPanel = headerPanel('data'),
    sidebarPanel = sidebarPanel(fileInput(
            'mtcars', h4('Uplaodmtcardata in csv format')
    ),
    uiOutput('tabnamesui')),
    mainPanel(uiOutput("tabsets"))
   )

  server <- function(input, output, session) {
     mtcarsFile <- reactive({input$mtcars})




    xxmtcars <-
            reactive({
                    read.table(
                            file = mtcarsFile()$datapath,
                            sep = ',',
                            header = T,
                            stringsAsFactors = T
                    )
            })

    tabsnames <- reactive({
            names(xxmtcars())
    })

    output$tabnamesui <- renderUI({
            req(mtcarsFile())
            selectInput(
                    'tabnamesui',
                    h5('Tab names'),
                    choices = as.list(tabsnames()),
                    multiple = T
            )


    })
    tabnamesinput <- reactive({
            input$tabnamesui
    })

    output$tabsets <- renderUI({
            req(mtcarsFile())
            tabs <-
                    reactive({
                            lapply(tabnamesinput(), function(x)
                                    tabPanel(title = basename(x), dataTableOutput(x)))
                    })
            do.call(tabsetPanel, c(tabs()))
    })

    output[['mpg']] <-
            renderDataTable(as.data.frame(select(xxmtcars(), mpg)))#HOW TO AVOID THIS HARD CODING..?BASED ON THE TAB NAME DATA FROM RELEVANT COLUMN IN THE CSV TO BE RETURNED.

} runApp(list(ui = ui, server = server))

#

标签: shiny

解决方案


试试这个

library(shiny)
library(plyr)
library(dplyr)
library(rlang)
library(DT)
ui <- pageWithSidebar(
  headerPanel = headerPanel('data'),
  sidebarPanel = sidebarPanel(fileInput(
    'mtcars', h4('Uplaodmtcardata in csv format')
  ),
  uiOutput('tabnamesui')),
  mainPanel(uiOutput("tabsets"))
)

server <- function(input, output, session) {
  mtcarsFile <- reactive({input$mtcars})




  xxmtcars <-
    reactive({
      read.table(
        file = mtcarsFile()$datapath,
        sep = ',',
        header = T,
        stringsAsFactors = FALSE
      )
    })

  tabsnames <- reactive({
    names(xxmtcars())
  })

  output$tabnamesui <- renderUI({
    req(mtcarsFile())
    selectInput(
      'tabnamesui',
      h5('Tab names'),
      choices = as.list(tabsnames()),
      multiple = T
    )


  })
  tabnamesinput <- reactive({
    input$tabnamesui
  })

  output$tabsets <- renderUI({
    req(mtcarsFile())

    tabs <-
      reactive({
        lapply(tabnamesinput(), function(x)
          tabPanel(title = basename(x), dataTableOutput(x)))
      })
    do.call(tabsetPanel, c(tabs()))
  })


observe(
  lapply(tabnamesinput(), function(x) {
    output[[x]] <- DT::renderDataTable({
t<-as.data.frame(dplyr::select(xxmtcars(), !! sym(x)) )
   print(t)
   datatable(t)

    })
  })
)
}


runApp(list(ui = ui, server = server))

推荐阅读