R:renderUI内函数中的可选参数不起作用
在我的应用程序中,我使用renderUI仅在选择数据帧后显示过滤器。我编写了自己的函数,将selectInput元素分配到函数参数给定的许多列中 我添加了指定R:renderUI内函数中的可选参数不起作用,r,function,shiny,R,Function,Shiny,在我的应用程序中,我使用renderUI仅在选择数据帧后显示过滤器。我编写了自己的函数,将selectInput元素分配到函数参数给定的许多列中 我添加了指定rosterWidth、每行应显示的列数和columnWidth、传递给shinny::column的参数作为宽度参数的可能性 理想情况下,我只需要指定其中一个参数,我的函数应该根据otherParam=12%/%firstParam(因为12是允许的最大宽度)计算另一个参数 我的职能: arrangeHtmlItems <- fun
rosterWidth
、每行应显示的列数和columnWidth
、传递给shinny::column
的参数作为宽度参数的可能性
理想情况下,我只需要指定其中一个参数,我的函数应该根据otherParam=12%/%firstParam
(因为12是允许的最大宽度)计算另一个参数
我的职能:
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))
}
}
为什么会这样?如何在
renderUI()中的函数中正确使用可选函数参数?您的代码是正确的。。。除了actionButton的最后一个参数未命名外。您需要执行排列Htmlitems(罗斯特宽度=…,htmlItemList=…,actionButton=…)
,否则actionButton将作为列宽度传入。我通过在函数中设置断点发现了这一点,你也可以这样做。你说得对!非常感谢你!从现在起,我将使用断点。
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)
arrangeHtmlItems <- function(rosterWidth = NULL, columnWidth = NULL, htmlItemList, actionButton) {
...
if (is.null(columnWidth)) {
...
}
...
}