r-闪亮的应用程序填充取决于输入的输出不起作用



我正在研究一些文本挖掘。根据用户的输入,我为下一个单词生成了许多建议。该部分正常工作。但是,建议的数量可能很大,所以我想在Shiny中最多显示10个建议,而我不想显示Na值。

我创建了一个可再现的示例,以表现出相同的问题。我要使用的技巧是用i粘贴"建议"。当我的输出不取决于我的输入时,这起作用。我从http://shiny.rstudio.com/gallery/creating-a-ui-from-a-loop.html。

我的ui.r文件

    library(shiny)
    fluidPage(
            titlePanel("Test"),
            fluidRow(
                    textAreaInput("userText", label="Enter your text")
            ),
            fluidRow(
                    lapply(1:5, function(i) {
                            textOutput(paste0("suggestions", i))})
            )
    )

我的server.r

    library(shiny)
    mySuggestions <- c("this", "is", "a", "test", "of", "getting", "reactive", "list", "length")
    function(input, output, session) {
            getWords <- function(i, wrds) {
                    output[[paste0("suggestions", i)]] <- renderText({ wrds()[i] })
            }
            userText <- reactive({ 
                    # Leaves this function when input$userText is NULL or ""
                    req(input$userText)
                    input$userText })
            words <- reactive({
                    mySuggestions[1:userText()]
            })
            # Problem
            lapply(reactive({ 1:min(5,  length(words())) }), getWords(), wrds=words()) 
    }

当您在UI文本字段中输入一个正整数时,应用程序应该显示大量单词,但最多只能显示5个。

上面的server.r的版本会导致警告"警告:paste0:groment:groment" i"缺失,没有默认值"我尝试了这条问题的几个版本。

    reactive({ lapply(1:min(5,  length(words())), getWords(), wrds=words() ) })

没有任何错误,但在输出中什么也没有显示。

    lapply(1:min(5,  length(words())), getWords() , wrds=words())

导致警告"警告:paste0中的错误:参数" i"缺失,没有默认值"

    lapply(reactive({1:min(5,  length(words()))}), getWords(), wrds=words())

导致警告"警告:paste0中的错误:参数" i"缺失,没有默认值"

     lapply(reactive({1:min(5,  length(words))}), function(i) {
              output[[paste0("suggestions", i)]] <- renderText({ words[i] }) } )

导致AS.Vector(x," list")中的错误: 无法将类型的"关闭"类型"关闭"到类型'列表'的向量'

     lapply(reactive({1:min(5,  length(words()))}), function(i) {
              output[[paste0("suggestions", i)]] <- renderText({ words()[i] }) } )

导致AS.Vector(x," list")中的错误: 无法将类型的"关闭"类型"关闭"到类型'列表'的向量'

     reactive({lapply(1:min(5,  length(words)), function(i) {
              output[[paste0("suggestions", i)]] <- renderText({ words[i] }) }) })

没有任何错误,但在输出中什么也没有显示。

     reactive({lapply(1:min(5,  length(words())), function(i) {
              output[[paste0("suggestions", i)]] <- renderText({ words()[i] }) }) })

没有任何错误,但在输出中什么也没有显示。

     lapply(1:min(5,  reactive({ length(words )})), function(i) {
             output[[paste0("suggestions", i)]] <- renderText({ words[i] }) }) 

导致最小的错误(5,反应性({:无效的'type'(闭合)参数

     lapply(1:min(5,  reactive({ length(words() )})), function(i) {
             output[[paste0("suggestions", i)]] <- renderText({ words()[i] }) }) 

导致最小的错误(5,反应性({:无效的'type'(闭合)参数

现在,以下行显示了单个文本字段中输入的单词数量。当我输入2时,它显示2个单词,当我输入20时,它显示了5个单词。这是我想要的行为,但是我想要单独的文本字段中的每个单词。

    output$suggestions1 <- renderText(words()[1:min(5, length(words()))])

我迷路了。我变得如此绝望,以至于我尝试了一些我没想到会工作的事情。有可能做我想做的事吗?如果是这样,怎么样?如果没有,有什么问题?我还没有找到任何解决这个特定问题的东西。

outputui和renderui的组合效果很好,并保持代码相对简单。

ui.r

    ...
    fluidRow(
            uiOutput("suggestions")
    )

server.r

    library(shiny)
    mySuggestions <- c("this", "is", "a", "test", "of", "getting", "reactive", "list", "length")
    function(input, output, session) {
            ...
            output$suggestions <- renderUI({ 
                    lapply(1:min(5, length(words())), function(i) {
                    output[[paste0("suggestions", i)]] <- renderText({ words()[i] })
            }) })
    }

我不知道outputui和renderui做了什么,但是它们对于这种情况似乎很完美。

最新更新