Home > Blockchain >  How can I run multiple expressions with single file in Shiny R?
How can I run multiple expressions with single file in Shiny R?

Time:08-03

I'm working on a shiny app that produces a wordcloud and a simple plot from selected input file. However, when I try to use observeEvent, only one expression works. Here is my code

library(shiny)
library(pdftools)
library(tidyverse)
library(tm)
library(SnowballC)
library(wordcloud)
library(RColorBrewer)


library(shiny)

ui <- fluidPage(

    titlePanel("Wordcloud"),

    sidebarLayout(
        sidebarPanel(
            fileInput("file1",
                        "Choose file")
        ),

        mainPanel(
           plotOutput("plot")
        )
    )
)

server <- function(input, output) {

    observeEvent(input$file1,{
        
        output$plot <- renderPlot({
            text1 <- pdf_text(input$file1$datapath)
            docs <- Corpus(VectorSource(text1))
            docs <- tm_map(docs, removeWords, stopwords("english"))
            dtm <- TermDocumentMatrix(docs)
            m <- as.matrix(dtm)
            v <- sort(rowSums(m),decreasing=TRUE)
            d <- data.frame(word = names(v),freq=v)
            
            wordcloud(words = d$word, freq = d$freq, min.freq = 1,
                      max.words=50, random.order=FALSE, rot.per=0.10, 
                      colors=brewer.pal(8, "Pastel1"))
        })
    })
    
}


shinyApp(ui = ui, server = server)

On the other hand, I also want to make a plot with ggplot by using same file1 like this:

ggplot(d, aes(x = word, y = freq))  
                geom_col()  
                labs(title="Wordcloud",
                     x = NULL,
                     y = "Frequency")  
                coord_flip()

When I try to produce this two plots (wordcloud and ggplot plot) only one of them works. I also can't use another observeEvent , because I can't use same id in observeEventas far as I know. So code below also does not work:

observeEvent(input$file1,{
    
    output$plot <- renderPlot({
        text1 <- pdf_text(input$file1$datapath)
        docs <- Corpus(VectorSource(text1))
        docs <- tm_map(docs, removeWords, stopwords("english"))
        dtm <- TermDocumentMatrix(docs)
        m <- as.matrix(dtm)
        v <- sort(rowSums(m),decreasing=TRUE)
        d <- data.frame(word = names(v),freq=v)
        
        ggplot(d, aes(x = word, y = freq))  
            geom_col()  
            labs(title="Wordcloud",
                 x = NULL,
                 y = "Frequency")  
            coord_flip()

    })
observeEvent(input$file1,{
    
    output$plot <- renderPlot({
        text1 <- pdf_text(input$file1$datapath)
        docs <- Corpus(VectorSource(text1))
        docs <- tm_map(docs, removeWords, stopwords("english"))
        dtm <- TermDocumentMatrix(docs)
        m <- as.matrix(dtm)
        v <- sort(rowSums(m),decreasing=TRUE)
        d <- data.frame(word = names(v),freq=v)
        
         wordcloud(words = d$word, freq = d$freq, min.freq = 1,
                      max.words=50, random.order=FALSE, rot.per=0.10, 
                      colors=brewer.pal(8, "Pastel1"))

    })

Is there any solution?

CodePudding user response:

Your code above is actually very nearly correct.

You can use 2 observeEvents in your server function, but they need to have different ids, e.g. plot1 and plot2. But you also should have 2 plotOutputs in your UI in order to show both of them in your app.

So for example:

library(shiny)

ui <- fluidPage(

    titlePanel("Wordcloud"),

    sidebarLayout(
        sidebarPanel(
            fileInput("file1",
                        "Choose file")
        ),

        mainPanel(
           plotOutput("plot1"),
           plotOutput("plot2")
        )
    )
)

server <- function(input, output) {

    observeEvent(input$file1,{
        
        output$plot1 <- renderPlot({
           ...
           ... code to create plot ...
           ...
        })
    })
    
    observeEvent(input$file1,{
        
        output$plot2 <- renderPlot({
           ...
           ... code to create word cloud ...
           ...
        })
    })
}

PS - In your last code block in your question there was a }) missing at the end of the first observeEvent.

  • Related