【发布时间】:2019-01-08 10:02:59
【问题描述】:
我正在尝试在 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)
【问题讨论】: