将向量循环到uiOutput()总是将向量的第一个元素打印到应用程序?

时间:2018-02-02 06:07:09

标签: r shiny

我有一个矢量:

uiOutput()

在我的Shiny app UI中,对于每个向量,我想创建一个 # output the filters box(width = 3, title = "Filters", lapply(filter_vars, function(x) uiOutput(x)) ),

lapply(filter_vars, function(x) uiOutput(x[[1]]))

这样可行,我的应用程序会显示所有ui过滤器框。

但是,矢量中的第一个元素始终显示!请参见屏幕截图,其中" source",向量中的第一项显示在过滤器的正上方: enter image description here

我尝试改变循环

library(shiny)

ui <- fluidPage(

   # Application title
   titlePanel("My Shiny App"),

   # row with filters
   fluidRow(style = "border-bottom: 3px solid grey;
                      margin-bottom: 10px",

   filter_vars <- c("Sepal.Length", "Sepal.Width", "Petal.Length"),


   # output the filters  
   box(width = 3,
       title = "Filters",
       lapply(filter_vars, function(x) uiOutput(x))
    )
   )
)

server <- function(input, output) {
    # drop down of uniques to pass to ui
  filter_vars <- c("Sepal.Length", "Sepal.Width", "Petal.Length")

  outputFilters <- function(id, df) {
    output[[id]] <- renderUI({
     selectInput(id, 
                 label = id, 
                 choices = as.list(c(paste0("All ", id, "s"), unique(df[[id]]))),
                 selected = paste0("All ", id, "s"))
      })
  }

  lapply(filter_vars, function(x) outputFilters(x, iris))

}

shinyApp(ui, server)

然而没有变化。为什么R Shiny会显示字符串&#34; source&#34;?我应该怎么做才能防止这种情况发生?

以下是使用iris数据集进行复制的代码,其中&#34; Sepal.Length&#34;会出乎意料地显示出像&#34;来源&#34;上面做了:

idx = pd.IndexSlice
df1.loc[idx[:, '00:00:00':'02:00:00'],:] = 1

1 个答案:

答案 0 :(得分:1)

问题在于filter_vars的位置,当在ui中提到它时会发光,认为它只是另一个ui元素。从UI中创建矢量创建可以解决问题。

library(shiny)

filter_vars <- c("Sepal.Length", "Sepal.Width", "Petal.Length")

ui <- fluidPage(

  # Application title
  titlePanel("My Shiny App"),

  # row with filters
  fluidRow(style = "border-bottom: 3px solid grey;
           margin-bottom: 10px",



           # output the filters  
           box(width = 3,
               title = "Filters",
               lapply(filter_vars, function(x) uiOutput(x))
           )
  )
)

server <- function(input, output) {
  # drop down of uniques to pass to ui
  filter_vars <- c("Sepal.Length", "Sepal.Width", "Petal.Length")

  outputFilters <- function(id, df) {
    output[[id]] <- renderUI({
      selectInput(id, 
                  label = id, 
                  choices = as.list(c(paste0("All ", id, "s"), unique(df[[id]]))),
                  selected = paste0("All ", id, "s"))
    })
  }

  lapply(filter_vars, function(x) outputFilters(x, iris))

}

shinyApp(ui, server)