我正在尝试在Shiny中构建一个界面,用户可以在其中动态创建决策树。我遇到的问题是,当我尝试动态生成树中的第三层时,无法将textInputs列表设置为uiOutput。如果您看一下代码,您会看到我正在树中生成一个级别,并用逗号分隔列表和相应的文本输入。对于第二层,每个节点应有一个单独的文本框,该文本框还将有一个逗号分隔的列表,表示其子节点。但是,当我尝试生成textInputs列表时,我得到了:
Warning in if (!is.na(attribValue)) { :
the condition has length > 1 and only the first element will be used
Warning in charToRaw(enc2utf8(text)) :
argument should be a character vector of length 1
all but the first element will be ignored
我理解这意味着我在不应该通过时传递列表,但是我不明白的是为什么我不能将元素列表传递给uiOutput?我不确定我在这里缺少什么,对于如何更好地做到这一点的任何帮助或建议将不胜感激。
library(shiny)
library(data.tree)
library(DiagrammeR)
# Define UI for application that draws a histogram
ui <- fluidPage(
titlePanel("Tree Builder"),
sidebarLayout(
sidebarPanel(
wellPanel(
h4("Criteria"),
textInput("txtDecison","Decision", placeholder = "Whatever you're trying to decide"),
textInput("txtCriteria","Criteria", placeholder = "i.e.) criteria1,2, etc", value = "yip,yop")
),
wellPanel(
h4("Factors"),
uiOutput("uiDynaFactors")
),
wellPanel(
h4("Alternatives"),
textInput("txtBoxAlternatives", "Alternatives", placeholder = "i.e.) alternative 1, 2, etc")
)
),
# Show a plot of the generated distribution
mainPanel(
grVizOutput("xx")
)
)
)
server <- function(input, output) {
hdp=reactiveValues(tree=NULL,names=NULL,criteria=NULL,factors=NULL,alternatives=NULL,rendered=c(1))
#create main tree
observe({
hdp$tree <- Node$new(input$txtDecison)
nodeSplitter <- unlist(strsplit(input$txtCriteria, ","))
for(v in nodeSplitter) {
hdp$tree$AddChildNode(child=Node$new(v))
}
hdp$names <- hdp$tree$Get('name')
})
#HERE IS THE PROBLEM:
observe({
renderList <- list(1:length(unlist(strsplit(input$txtCriteria, ","))))
output$uiDynaFactors <- renderUI({
lapply(renderList,function(i){
textInput(paste0('criteraFeature_',i), paste0('criteraFeature_',i))
})
})
})
output$xx=renderGrViz({
grViz(DiagrammeR::generate_dot(ToDiagrammeRGraph(hdp$tree)),engine = "dot")
})
}
# Run the application
shinyApp(ui = ui, server = server)
答案 0 :(得分:0)
list(1:n)
创建一个仅包含一个元素的列表:向量1:n
。因此,将renderList
替换为:
renderList <- 1:length(unlist(strsplit(input$txtCriteria, ",")))