R shiny:renderUI中的函数中的可选参数不起作用

时间:2017-03-02 08:57:08

标签: r function shiny

在我的应用程序中,我使用renderUI仅在选择了数据框后才显示过滤器。我编写了自己的函数来将selectInput元素分配给函数参数给出的多个列。

我添加了指定rosterWidth(每行应显示的列数)和columnWidth的可能性,shiny::column作为宽度参数传递给otherParam = 12 %/% firstParam

理想情况下,我只想指定其中一个参数,我的函数应根据arrangeHtmlItems <- function(rosterWidth, columnWidth, htmlItemList, actionButton) { if (missing(rosterWidth) && missing(columnWidth)) { stop("Specify either rosterWidth or columnWidth") return(NULL) } if (missing(rosterWidth)) { rosterWidth <- 12 %/% columnWidth } if (missing(columnWidth)) { columnWidth <- 12 %/% rosterWidth } # number of columns needed numCols <- min(length(htmlItemList), rosterWidth) # item indices to use in for-loop itemIndices <- 1:length(htmlItemList) # column indices: column 1 should be first, column 0 should be the last column, # since i %% numCols for i == numCols is 0 # (in a four-column roster, the first item is attributed to column 0 since 4 %% 4 is 0) columnIndices <- c(1:(numCols - 1), 0) cols <- list() for (i in 1:length(columnIndices)) { curIndex <- columnIndices[i] curCol <- htmlItemList[which(itemIndices %% numCols == curIndex)] cols[[i]] <- column(columnWidth, curCol) } # return action button in fluid row if given as parameter if(missing(actionButton)) { return(fluidRow(cols)) } else { return(fluidRow(cols, actionButton)) } } 计算另一个(因为12是闪亮允许的最大宽度)。

我的功能:

library(shiny)
library(dplyr)
library(stringr)
library(magrittr)
library(ggplot2)

source("functions.R")

server <- function(input, output) {
  CtryData <- eventReactive(input$goButton, {
    data <- data.frame(mpg)
  })

  output$colFilter <- renderUI({
    # make objects out of reactive objects
    data <- CtryData()
    cols <- colnames(data)

    # function returns fluidRows with number of columns specified by rosterWidth
    # dynamically adjusts the number of items per column based on number of filters produced in the lapply-function
    arrangeHtmlItems(
      # !!! 
      # rosterWidth * columnWidth must be <= 12
      # !!!
      rosterWidth = 2
      , columnWidth = 6
      , htmlItemList = lapply(cols, function(i) {
        vec_choices <- c("(All)", paste0(sort(unique(data[ ,i])))) # paste0 is needed since otherwise level numbers, not levels are listed for factors 
        selectInput(
          inputId = paste0("colFilter_", i),
          paste0("Select ", i),
          choices = vec_choices,
          multiple = TRUE,
          selectize = FALSE,
          size = 6,
          selected = "(All)"
        )
      })
      , actionButton("colFilterButton", "Go")
    )
  })
}

ui <- fluidPage(
  fluidRow(
    actionButton("goButton", "Go")
    , uiOutput("colFilter")
  )
)

shinyApp(ui = ui, server = server)

我的应用:

columnWidth

这很好用,我希望它的方式。 但是,当我在arrangeHtmlItems()函数中写下column width must be between 1 and 12时,应用会发出警告columnWidth <- 12 %/% rosterWidth,而不是进入arrangeHtmlItems <- function(rosterWidth = NULL, columnWidth = NULL, htmlItemList, actionButton) { ... if (is.null(columnWidth)) { ... } ... } 的代码。 当我将函数定义为

时,会发生同样的情况
renderUI()

这是为什么?如何在public class Father { // skip ... }

中的函数中正确使用可选函数参数

0 个答案:

没有答案