Обновление графиков и данных с помощью действий клика в блестящем - PullRequest
1 голос
/ 09 июня 2019

У меня есть приложение с 3 действиями,

  1. загрузка данных нажатием кнопки go (вверху слева) - графики должны выводиться сейчас

  2. удалите слова, наложив слово в теле удаления и нажав «сделать» - графики должны быть обновлены

  3. заменить слово, купить, добавив слово, которое вы хотите заменить в «найти»,и замена в «заменить» и щелкнуть акт

Проблема, с которой я сталкиваюсь, заключается в том, что графики отображаются только в том случае, если я нажимаю «перейти», а затем «действовать», графики отображаются толькоКак только я достигну "действовать"

library(shiny)
library(plyr)
library(sp)
library(stringr)
library(tidyr)
library(tidyverse)
library(tm)
library(ggplot2)
library("stringi")
library(plyr)
library(dplyr) #Data manipulation (also included in the tidyverse package)


ui <- fluidPage(

  fluidRow( 
    column( 4, titlePanel("Twitter Analytics")),
    column( 3),
    column( 4, 
            textInput("searchstring", 
                      label = "",
                      value = "")),
    column(1, 
           br(),
           actionButton("action", "go"))
  ),
  fluidRow(
    column( 12, tabsetPanel(
      tabPanel("one",
               fluidRow(
                 column(3, textInput("removeString", label = "remove", value = ""), actionButton("remove", "do"),
                        textInput("find", label = "find", value = ""),textInput("rep", label = "replace", value = ""),actionButton("replace", "act"), 
                        checkboxGroupInput("checkGroup", "select plots",
                                           choices <- c("Histogram", "Wordcloud", "network")),
                        sliderInput("topTerms",
                                    label = "top (n) terms", 
                                    min = 0, max = 25, value = 0)  ),
                 column(9,fluidRow(column(12,plotOutput("ttext") )),

                          fluidRow(column(12,wordcloud2Output("wc2"))))
               )
      ),    
      tabPanel("two"),
      tabPanel("three")
    )
    )
  )
)




server <- function(input, output) {
  values <- reactiveValues(go = 0, do = 0, act = 0 )


  observeEvent(input$action, {
    values$go <- 1
    values$do <- 0
    values$act <- 0

  })

  observeEvent(input$remove, {
    values$go <- 0
    values$do <- 1
    values$act <- 0
  })

  observeEvent(input$replace, {
    values$go <- 0
    values$do <- 0
    values$act <- 1
  })

  #tweet <- eventReactive(input$action,{
  cs<- reactiveVal(0)

  tweet <-reactive({

    if(values$go){

    num <- c(1,2,3,4,50)
    text <- c("this is love love something", "this is not hate hate hate something", "@something islove  rethched this not", " Shiny is love confusing me", "this is hate also somthing difficult")
    letter<- c("a", "b", "c", "D", "e")
    tweetdf <- data.frame(num, text, letter)
    tweetdf$text <- tolower(tweetdf$text)
    # tweetdf @UserName
    tweetdf$text <- gsub("@\\w+", "", tweetdf$text)
    #remove punctuation
    tweetdf$text <- gsub("[[:punct:]]", "", tweetdf$text)
    #remove links
    tweetdf$text <- gsub("http\\w+", "", tweetdf$text)
    # Remove tabs
    tweetdf$text <- gsub("[ |\t]{2,}", "", tweetdf$text)
    # Remove blank spaces at the beginning
    tweetdf$text <- gsub("^ ", "", tweetdf$text)
    # Remove blank spaces at the end
    corpus <- iconv(tweetdf$text, to = "ASCII")
    corpus <- Corpus(VectorSource(corpus))
    corpus <- tm_map(corpus, removePunctuation)
    corpus <- tm_map(corpus, removeNumbers)
    cleanset <- tm_map(corpus, removeWords, stopwords('english'))
    cs(cleanset)}

     if(values$do){

      cleanset <- cs()
      cleanset <- tm_map(cleanset, removeWords, input$removeString)
      cs(cleanset)
     }

    if(values$act){

      cleanset <- cs()
      cleanset <- tm_map(cleanset, gsub, 
                         pattern = input$find, 
                         replacement = input$rep)
    cs(cleanset)

    }

    else
      {return()}
  })



    output$ttext <- renderPlot({ 
      if(is.null(tweet())){return()}
      else{
      cleanset <-cs()
      tdm <- TermDocumentMatrix(cleanset)
      tdm <- as.matrix(tdm)
      w <- rowSums(tdm)
        library(RColorBrewer)
        barplot(w)}

    })


    output$wc2 <- renderWordcloud2({
      if(is.null(tweet())){return()}
      else{
      library(wordcloud2)
      cleanset <-cs()
      tdm <- TermDocumentMatrix(cleanset)
      tdm <- as.matrix(tdm)
      w <- rowSums(tdm)
      w <- data.frame(names(w), w)
      colnames(w) <- c('word', 'freq')
      wordcloud2(w,
                 color = 'random-dark',
                 size = 0.7,
                 shape = 'circle',
                 rotateRatio = 0.5,
                 minSize = 1)}
    })
  }

  shinyApp(ui, server)

Может кто-нибудь сказать, пожалуйста, что происходит не так, как я только начал использовать блестящий несколько дней назад?

1 Ответ

1 голос
/ 09 июня 2019

Проблема заключается в том, что вы используете несколько if.Р не знает, что вернуть.Таким образом, вы можете использовать этот сервер, вам не нужно заменять 0 на FALSE.

server <- function(input, output) {
  values <- reactiveValues(go = 0, do = 0, act = 0 )

  observeEvent(input$action, {
    values$go <- T
    values$do <- F
    values$act <- F
  })

  observeEvent(input$remove, {
    values$go <- F
    values$do <- T
    values$act <- F
  })

  observeEvent(input$replace, {
    values$go <- F
    values$do <- F
    values$act <- T
  })

  #tweet <- eventReactive(input$action,{
  cs <- reactiveVal(0)

  tweet <- reactive({
    cleanset <- cs()
    if(values$go){
      num <- c(1, 2, 3, 4, 50)
      text <- c("this is love love something", "this is not hate hate hate something",
                "@something islove  rethched this not", " Shiny is love confusing me", "this is hate also somthing difficult")
      letter<- c("a", "b", "c", "D", "e")
      tweetdf <- data.frame(num, text, letter)
      tweetdf$text <- tolower(tweetdf$text)
      # tweetdf @UserName
      tweetdf$text <- gsub("@\\w+", "", tweetdf$text)
      # Remove punctuation
      tweetdf$text <- gsub("[[:punct:]]", "", tweetdf$text)
      # Remove links
      tweetdf$text <- gsub("http\\w+", "", tweetdf$text)
      # Remove tabs
      tweetdf$text <- gsub("[ |\t]{2,}", "", tweetdf$text)
      # Remove blank spaces at the beginning
      tweetdf$text <- gsub("^ ", "", tweetdf$text)
      # Remove blank spaces at the end
      corpus <- iconv(tweetdf$text, to = "ASCII")
      corpus <- Corpus(VectorSource(corpus))
      corpus <- tm_map(corpus, removePunctuation)
      corpus <- tm_map(corpus, removeNumbers)
      cleanset <- tm_map(corpus, removeWords, stopwords('english'))
      return(cs(cleanset))}

    else if(values$do){
      cleanset <- tm_map(cleanset, removeWords, input$removeString)
      return(cs(cleanset))
    }

    else if(values$act){
      cleanset <- tm_map(cleanset, gsub, 
                         pattern = "input$find", 
                         replacement = "input$rep")
      return(cs(cleanset))
    }
    else
    {return()}
  })


  output$ttext <- renderPlot({
    if(is.null(tweet())){
      return()}
    else{
      cleanset <- cs()
      tdm <- TermDocumentMatrix(cleanset)
      tdm <- as.matrix(tdm)
      w <- rowSums(tdm)
      barplot(w)}

  })


  output$wc2 <- renderWordcloud2({
    if(is.null(tweet())){return()}
    else{
      cleanset <-cs()
      tdm <- TermDocumentMatrix(cleanset)
      tdm <- as.matrix(tdm)
      w <- rowSums(tdm)
      w <- data.frame(names(w), w)
      colnames(w) <- c('word', 'freq')
      wordcloud2(w,
                 color = 'random-dark',
                 size = 0.7,
                 shape = 'circle',
                 rotateRatio = 0.5,
                 minSize = 1)}
  })
}
...