R-闪亮的网络应用-文字云功能无法在仪表板中运行

时间:2019-02-27 14:15:02

标签: r web-applications shiny

对于在互联网上提问非常陌生。

但是,我对无法修复在Shiny中构建的Web应用程序感到厌烦。我有两个问题需要解决:

  1. 如何在仪表板上使用云这个词?

  2. Shiny不理解我需要在仪表板的不同部分中显示一些输出。

任何帮助将不胜感激。

## app.R ##
library(shiny)
library(shinydashboard)
options(shiny.maxRequestSize = 9*1024^2)

header <- dashboardHeader(title = "Text analytics platform")

sidebar <- dashboardSidebar(
  sidebarMenu(
    menuItem("Dataset", tabName = "dataset", icon = icon("cog", lib = "glyphicon")),
    menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
    menuItem("Analytics", tabName = "analytics", icon = icon("th"), badgeLabel = "new", badgeColor = "green")
  )
)
# A body for two datatables to show the data input
body <- dashboardBody(
  tabItems(
    tabItem(tabName = "dataset", h2("Dataset"),
            fluidPage(fluidRow(
              box(title = "Upload file for analysis", fileInput(inputId = "file1",
                            label = "Select file in .csv format",
                            multiple = F),
                  #helpText("File should be in csv format"),
                  checkboxInput(inputId = "header", label = "Header", value = T),
                  checkboxInput(inputId = "stringAsFactors", label = "stringAsFactors", F),
                  radioButtons(inputId = "sep", label = "Separator", 
                               choices = c(Comma = ",", Semicolon = ";", Tab = "/t", Space = " "),
                               selected = ","),
                  uiOutput("selectfile"), status = "success", solidHeader = T, collapsible = T
              ),
              box(title = "File preview", uiOutput("tb"), status = "info", solidHeader = T, collapsible = T
              )
            )
            )
    )
  ),
  tabItem(tabName = "dashboard", h2("Dashboard"),
          fluidPage(fluidRow(
            box(title = "Wordcloud", plotOutput("wordcloud"),
                status = "warning", solidHeader = T, collapsible = T,
                actionButton("update", "Change"),
                hr(),
                sliderInput("freq", "Select minimum frequency:",
                            min = 1, max = 50, value = 20),
                sliderInput("max", "Select maximum number of words:",
                            min = 1, max = 300, value = 100))


          )
        )

)
)

shinyUI <- dashboardPage(title = "Turner & Townsend/PA/Digital Solutions", header, sidebar, body, skin = "yellow")

shinyServer <- function(input, output) {

  ## code for text analysis
      ## S1.1 - installing all the packages required
      # install.packages("tm")
      # install.packages("RColorBrewer")
      # install.packages("wordcloud")
      # install.packages("SnowballC")
      # install.packages("ggplot2")
      # install.packages("corpus")

      library(tm)
      library(RColorBrewer)
      library(wordcloud)
      library(SnowballC)
      library(ggplot2)
      library(readxl)
      library(corpus)

      terms <- reactive({
        input$update

        isolate({
          withProgress({
            setProgress(message = "Processing corpus....")
          })
        })

      ## S1.2 - creating the first dataset
      file1_clean <- na.omit(input$file1) # omit records with NAs
      names(file1_clean)[1] <- "text"

      ## S1.3 - creating a corpus of the file uploaded 
      corpus_file1 <- Corpus(VectorSource(as.vector(file1_clean$text)))

      ## S1.4 - cleaning the corpus of unwanted words and punctuation
      corpus_file1_clean <- tm_map(corpus_file1, removePunctuation)
      corpus_file1_clean <- tm_map(corpus_file1_clean, removeNumbers)
      corpus_file1_clean <- tm_map(corpus_file1_clean, tolower) 
      corpus_file1_clean <- tm_map(corpus_file1_clean, removeWords, stopwords("english")) 
      corpus_file1_clean <- tm_map(corpus_file1_clean, removeWords, c(stopwords("SMART"),"des", "will", "page", "works", "etc", "Not Answered")) ##input from the user only and see if it's possible to input multiple text responses

      ## S1.5 - creating the dtm for frequency charts
      dtm_file1 <- DocumentTermMatrix(corpus_file1_clean)
      m <- as.matrix(dtm_file1)
      freq <- colSums(as.matrix(dtm_file1))
      freq <- sort(colSums(as.matrix(dtm_file1)), decreasing=TRUE)
      findFreqTerms(dtm_file1, lowfreq=5)
      wf <- data.frame(word=names(freq), freq=freq) 

      # library(ggplot2)   
      # p <- ggplot(subset(wf, freq>5), aes(word, freq))    
      # p <- p + geom_bar(stat="identity")   
      # p <- p + theme(axis.text.x=element_text(angle=45, hjust=1))  

      ## S2.0 - creating the word cloud    
      set.seed(142)   
      dark2 <- brewer.pal(6, "Dark2")   
      # creating output to show in the tab for Dashboard - how to do that
      wordcloud_rep <- repeatable(wordcloud)

      })

  output$wordcloud <- renderPlot({
    v <- terms()
    wordcloud(names(v), v, scale = c(4,0.5), min.freq = input$freq, max.words = input$max,  rot.per = 0.2, 
                  colors = brewer.pal(8,"Dark2"))

  })    

  output$filedf <- renderTable({
    if(is.null(input$file1)) {return()}
    input$file1$datapath
  })

  output$table <- renderTable({
    if(is.null(input$file1)) {return()}
    read.table(file = input$file1$datapath, 
                       sep = input$sep, 
                       header = input$header,
                       stringsAsFactors = input$stringAsFactors,
                       skipNul = T)
  })


    output$selectfile <- renderUI({
      if(is.null(input$file1)) {return()}
    })

    output$tb <- renderUI ({
      if(is.null(input$file1)) {return()}
      else
        tabsetPanel(
          tabPanel("Input file object DF", tableOutput("filedf")),
          tabPanel("Dataset", tableOutput("table"))
        )


    })

}

shinyApp(ui = shinyUI, server = shinyServer)

0 个答案:

没有答案