Error sourcing while creating Shiny app

shinydashboard

#1

I am trying to create a shiny dashboard to summarize customer feedback, but get error as below

Warning: Error in inherits: Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)
  56: <Anonymous>
Error in inherits(x, "Source") : 
  Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)

Below is the coding part, I have used.

library(shiny)

#Define UI
shinyUI(fluidPage(
  #Title Panel
  titlePanel(title=h4("Initial Feedback Survey",align="center")),
  sidebarLayout(
  #Sidebar panel
    sidebarPanel(
      selectInput("var","Select the variable",choices=names(data),selected=names(data)[11]),
      br(),
      radioButtons("color","Select the color",choices=c("Green","Red","Yellow"),selected="Green")
    ),
    #Main Panel
    mainPanel(
      tabsetPanel(type="tab",
                  tabPanel("Data",tableOutput("data")),
                  tabPanel("Summary",tableOutput("counts")),
                  tabPanel("Plot",plotOutput("bar")))
      
    )
    )
)
)

library(shiny)
library(shinydashboard)
library(dplyr)
library(ggplot2)
library(tm)
data=read.csv("C:\\Users\\Kafeel Basha\\Desktop\\R Files\\RShiny\\Jigsaw Data Science with R.csv",header=TRUE,stringsAsFactors = FALSE,na.strings=c("  "," ","NA",NULL,""))
data=na.omit(data)
names(data) <- gsub("[..]", " ", names(data))
names(data) <- gsub("[.]", " ", names(data))

shinyServer(function(input,output){
  demo=Corpus(VectorSource(input$var))
  demo=tm_map(demo,content_transformer(tolower))
  demo=tm_map(demo,removePunctuation)
  df<- data.frame(text=unlist(sapply(demo,`[`)),stringsAsFactors=F)
  df$text=ifelse(df$text=="poor"|df$text=="fair","BAD","GOOD")
  df%>%group_by(text)%>%summarise(count=n(),percentage=count/nrow(df))%>%as.data.frame()->stats
  rownames(stats)<-stats$text
  
  output$data<-renderTable({head(data)})
  output$counts<-renderTable({table(stats$text)})
  output$bar <- renderPlot({
    ggplot(stats,aes(x=stats$text,y=stats$percentage,fill=text))+geom_bar(stat="identity")+geom_text(label=stats$percentage,vjust=-0.5)+ggtitle("Bar Chart of Proportion")
  })})

#2

Hi, yes, the value frominput$var can only be called inside a Shiny reactive function. You probably want to use the observeEvent() function to execute all of your data transformation. So it would be something like: `observeEvent(intput$var, [data transformation goes here])


#3

Hi! Welcome to RStudio Community!

It looks like your code was not formatted correctly to make it easy to read for people trying to help you. Formatting code allows for people to more easily identify where issues may be occuring, and makes it easier to read, in general. I have edited you post to format the code properly.

In the future please put code that is inline (such as a function name, like mutate or filter) inside of backticks (`mutate`) and chunks of code (including error messages and code copied from the console) can be put between sets of three backticks:

```
example <- foo %>%
  filter(a == 1)
```

This process can be done automatically by highlighting your code, either inline or in a chunk, and clicking the </> button on the toolbar of the reply window!

This will help keep our community tidy and help you get the help you are looking for!

For more information, please take a look at the community's FAQ on formating code


#4

Yes, Thanks. I am trying to build the dashboard using dashboardPage() instead of fluidPage().

Let me complete and get back if required.