闪亮和普通R中函数的不同行为?

时间:2015-08-18 08:35:38

标签: r shiny-server shiny

所以,我在R中使用预测算法,我有这个数据集,其最后3列是:

  1. 给定二重奏的“先前”概率。
  2. 第二段二重奏。
  3. 第二段二重奏。
  4. A有一个函数,对于相关的双字母(第一部分与最后一个字匹配的那些)从其他有意义的单词列表中更新概率。

    该功能在本地工作。

    caption <- "She's the sister of my "
    
    
    
    probUpdate <- function(caption) {
      words <- unlist(strsplit(caption, " "))
    
      last <- words[length(words)]
      others <- words[-length(words)]
    
    
    
      chances <- dataset[which(dataset$lastWord == last),]
    
      for (j in 1:length(others)){
        if (others[j] %in% colnames(chances)){
          for (i in 1:nrow(chances)){
            chances[i, 1410] <- chances[i, 1410] * chances[i, which( colnames(chances)==others[j] )]
          }
        }
      }
    
      chances <- chances[order(chances[1410], decreasing = TRUE), ]
      return(chances[1, 1412])
    }
    
    probUpdate(caption)
    

    这给了“丈夫。

    但是当我尝试将它放在一个闪亮的应用程序中时,相同的输入会给出:“1464”。

    server.R:

    library(shiny)
    load(file = "PROGRESSING.RData")
    source(file = "probUpdating.R")
    
    shinyServer(function(input, output) {
    
      datasetInput <- reactive({
        switch(input$dataset)
      })
    
    
    
      output$caption <- renderText({
        probUpdate(input$caption)
      })
    
    })
    

    如果我提出:

    output$caption <- renderText({
    print(probUpdate(input$caption))
    

    })

    这显示了控制台中正确的输出。

      

    参数1(类型'封闭')不能由'cat'处理

    这是我的ui.R:

    library(shiny)
    
    shinyUI(pageWithSidebar(
    
      headerPanel("Reactivity"),
      sidebarPanel(
        textInput("caption", "Caption:", "She's the sister of my")
      ),
    
    
      # Show the caption, a summary of the dataset and an HTML table with
      # the requested number of observation
      mainPanel(
        h3(textOutput("caption"))
      )
    ))
    

    知道为什么会这样吗?

    提前谢谢!

1 个答案:

答案 0 :(得分:0)

renderPrint而不是renderText部分解决了这个问题。