[英]dynamic number of ggplots in shiny app
I need your help, because I don't know how to solve my problem. 我需要您的帮助,因为我不知道如何解决我的问题。 I have my shiny app where I have data frame (imported from file) and checkboxgroupinput where I can mark which columns are for me interesting.
我有闪亮的应用程序,其中有数据框(从文件导入)和checkboxgroupinput,可在其中标记哪些列对我来说很有趣。 After that in other tabpanel I would like to get two plot for each column (in one facet_wrap).
之后,在其他选项卡中,我想为每列获取两个图(在一个facet_wrap中)。 All facet_wrap one under the other.
所有facet_wrap一个在另一个下。 The problem is that number of interesting columns is not constant.
问题在于有趣的列数不是恒定的。 It is easy for my if I could hardcode number of rows with plots, but where it can change dynamically I have no idea how to program it, any tips from your side?
如果可以用图对行的行进行硬编码,这对我来说很容易,但是在可以动态更改的地方,我不知道如何编程,您身边有什么建议吗?
We can't solve your question without a reproducible example but you should be able to figure it out from this quick example of using uiOutput
along with renderUI
. 没有可重现的示例,我们无法解决您的问题,但是您应该能够从使用
uiOutput
和renderUI
快速示例中uiOutput
renderUI
。 This allows the use of dynamic values in UI elements. 这允许在UI元素中使用动态值。
Normally you would define your static input as checkboxGroupInput("columns", "Select the variables to plot", choices = vector_of_known_values)
. 通常,您将静态输入定义为
checkboxGroupInput("columns", "Select the variables to plot", choices = vector_of_known_values)
。
However as per your question, this doesn't work if the dataset is not known beforehand (eg: user file upload). 但是根据您的问题,如果事先不知道数据集(例如:用户文件上传),则此方法将无效。 In this case use
uiOutput
in the UI part: uiOutput("ui")
, so that you delay evaluation to server side. 在这种情况下,请在UI部分使用
uiOutput
: uiOutput("ui")
,以便将评估延迟到服务器端。 In server side you can dynamically set the choices
regardless of the data structure. 在服务器端,无论数据结构如何,都可以动态设置
choices
。
output$ui <- renderUI( {
checkboxGroupInput("columns", "Select the variables to plot", choices = colnames(rv$data))
})
See full example: 查看完整示例:
library(shiny)
library(DT)
library(dplyr)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
selectInput("datasets", "Select a dataset", choices = c("mtcars", "iris"), selected = "mtcars"),
uiOutput("ui")
),
mainPanel(
DT::dataTableOutput("table")
)
)
)
server <- function(input, output, session) {
rv <- reactiveValues(data = NULL)
observe( {
rv$data <- eval(parse(text = input$datasets))
})
filtered <- reactive( {
req(input$columns)
if( all(!input$columns %in% colnames(rv$data))) {
NULL
} else {
rv$data %>% select(input$columns)
}
})
output$ui <- renderUI( {
checkboxGroupInput("columns", "Select the variables to plot", choices = colnames(rv$data))
})
output$table <- DT::renderDataTable( {
req(filtered())
DT::datatable(filtered())
})
}
shinyApp(ui, server)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.