shiny - 如何在 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))
#解决方案
试试这个
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))
推荐阅读
- docker - Filecoin Textileio Powergate 没有足够的矿工来自声誉模块来满足约束错误
- github - GitHub 归档过时的文件
- java - 在单例中使用同步块
- arrays - 特定索引 mongodb 上的数组到数组连接
- azure - Azure Datalake Storage 列出容器中的第一级目录
- javascript - 未在 ui 中呈现的数据在使用 useEffect 做出反应
- javascript - Fetch 数据已加载到 DOM 上,但无法选择
- python - 如何在 Ubuntu 中更新 Docx 文件中的目录
- kubernetes - 如何恢复牧场主集群
- elasticsearch - 如何在Django Elastic query dsl中匹配搜索词中的冒号