【问题标题】:Dynamic text box list in ShinyShiny中的动态文本框列表
【发布时间】: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)

【问题讨论】:

    标签: r dynamic shiny lapply


    【解决方案1】:

    list(1:n) 创建一个只有一个元素的列表:向量1:n。所以将renderList替换为:

    renderList <- 1:length(unlist(strsplit(input$txtCriteria, ",")))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-05
      • 1970-01-01
      • 2015-01-11
      相关资源
      最近更新 更多