如何从ui.r调用server.r中的函数

时间:2017-06-19 07:40:59

标签: r shiny rstudio personality-insights

Server.r

文本分析成为5大人格特质(O,C,E,A,N)

这里我想要在用户单击“更新视图”按钮时处理文本。 然后该文本将存储在"句子"变量,然后清理语料库,加载每个人格特征的词汇表文件,将词汇输入与词汇表匹配,对个性得分和总和进行求和。显示分数&特质用户得分高

library(shiny)
library(purrr)
library(dplyr)
library(plyr)
library(stringr)
#Analysing text entered by the user 
function(input, output,session) {
textAnalysis <- eventReactive(input$update,{
  function(textdata){
  sentence <- input$textdata
  word.list <- str_split(sentence,"\\s+")
  words <- unlist(word.list)
  sentence <- gsub('[[:punct:]]', "", sentence) #gsub is global substitution from stringr library
  sentence <- gsub('[[:cntrl:]]', "", sentence) #cntrl are control words
  sentence <- gsub('\\d+', "", sentence)        #d is substituting digits with null
  sentence <- tolower(sentence)
  #Include word list in environment
  o.words = scan("F:/MTech_Project/OpenEnd/OpennessWordList.txt", what='character',comment.char = ";")
  c.words = scan("F:/MTech_Project/OpenEnd/ConscientiousnessWordList.txt", what='character',comment.char = ";")
  e.words = scan("F:/MTech_Project/OpenEnd/ExtraversionWordList.txt", what='character',comment.char = ";")
  a.words = scan("F:/MTech_Project/OpenEnd/AgreeablenessWordList.txt", what='character',comment.char = ";")
  n.words = scan("F:/MTech_Project/OpenEnd/NeuroticismWordList.txt", what='character',comment.char = ";")
  o.matches <- match(words,o.words)
  c.matches <- match(words,c.words)
  a.matches <- match(words,e.words)
  e.matches <- match(words,a.words)
  n.matches <- match(words,n.words)
  o.matches <- !is.na(o.matches)
  c.matches <- !is.na(c.matches)
  e.matches <- !is.na(e.matches)
  a.matches <- !is.na(a.matches)
  n.matches <- !is.na(n.matches)
  o.score <- sum(o.matches)
  #o.score
  c.score <- sum(c.matches)
  #c.score
  e.score <- sum(e.matches)
  #e.score
  a.score <- sum(a.matches)
  #a.score
  n.score <- sum(n.matches)
  #n.score
  score <- max(o.score,c.score,e.score,a.score,n.score)
  return(score)
  }}, ignoreNULL = FALSE)


  output$value <- renderPrint({
    textdata <- textAnalysis()

    })

}

Ui.r

这里我试图从用户&amp;呼唤&#34;价值&#34;在server.r

library(shiny)
fluidPage(

  # Application title.
  titlePanel("Text Analytics"),


  sidebarLayout(
    sidebarPanel(



    textInput("textdata",label = "Tell something about you..what you are..what you like",""),

      actionButton("update", "Update View")
    ),
    mainPanel(
      verbatimTextOutput("value"),  
    )))

我很困惑如何将文本传递给server.r&amp;如何在ui.r中显示结果请帮助

控制台结果

Listening on http://127.0.0.1:5547
Warning: Error in tag: argument is missing, with no default
Stack trace (innermost first):
    52: tag
    51: tags$div
    50: div
    49: mainPanel
    48: sidebarLayout
    47: tag
    46: tags$div
    45: div
    44: tagList
    43: attachDependencies
    42: bootstrapPage
    41: fluidPage
     1: runApp
Error : argument is missing, with no default  

我该如何解决?请帮忙

1 个答案:

答案 0 :(得分:0)

renderUI()可用于获取UI输出。此链接中提供了一个示例:https://shiny.rstudio.com/reference/shiny/latest/renderUI.html