如何在shinyR应用程序中使用renderUI创建的选项卡下添加动态内容
我正在Shinny R中构建一个应用程序,用户可以在其中选择所需的选项卡,并在其下显示与这些选项卡相关的数据 例如,在下面的示例应用程序中,.csv中的mtcars数据将被接受为输入参数。用户可以在“选项卡”字段中选择所需的列名。这些列将被创建为选项卡 现在,我想在适当的选项卡中显示.csv中与该列相关的数据。例如,“mpg”列中的数据将显示在“mpg”选项卡下 但我被困在这里了。希望有人能告诉我一种方法,在适当的选项卡下动态显示相关列中的数据 使用的示例代码如下所示: 写入.csvmtcars,'mtcars.csv' } runApplistui=ui,server=server 试试这个如何在shinyR应用程序中使用renderUI创建的选项卡下添加动态内容,shiny,Shiny,我正在Shinny R中构建一个应用程序,用户可以在其中选择所需的选项卡,并在其下显示与这些选项卡相关的数据 例如,在下面的示例应用程序中,.csv中的mtcars数据将被接受为输入参数。用户可以在“选项卡”字段中选择所需的列名。这些列将被创建为选项卡 现在,我想在适当的选项卡中显示.csv中与该列相关的数据。例如,“mpg”列中的数据将显示在“mpg”选项卡下 但我被困在这里了。希望有人能告诉我一种方法,在适当的选项卡下动态显示相关列中的数据 使用的示例代码如下所示: 写入.csvmtcars
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))
,如果我可以再问一个问题的话,我根据您的反馈开发了包含两个图的代码,它正在工作。但是每次我都必须对数据进行子集,我希望避免这种情况。假设使用“mpg”子集数据,我在“mpg”选项卡中绘制了两种不同类型的图形,我不想每次都对数据进行子集。当前,我每次绘制该绘图时都会对数据进行子集。对于一个选项卡中的所有计算,我只想对数据进行一次子集。很抱歉打扰你。请帮忙。请作为一个单独的问题提出来。
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))