Reputation: 21
I have a customer survey of 25 questions. Question's answer are available in "1", "2", "3", "4" (1-Very Good, 2-Good, 3-Normal, 4-Bad) Each rows contains the respondent name with all the answers given by him.
Data is in this format, respondent ID and the response value, Column header contains the Question name.
21044194- 1- 2- 4- 1- 3- 1- 1- 2- 1- 2- 1- 3- 2- 2- 1- 2- 4- 2
21044198- 1- 2- 4- 4- 3- 1- 1- 2- 1- 2- 1- 3- 2- 4- 1- 3- 4- 2
21044199- 1- 2- 3- 1- 2- 3- 2- 1- 1- 2- 1- 3- 2- 4- 1- 3- 4- 2
Now I want to create a shiny app, in which I have a list of all 25 questions as an input and on the basis of selected question I need to display the pie chart of answers. Like this for 1 question 31% ppl choose Very Good, 22% choose Good, 31% choose Normal and 17% choose Bad.
I have written the following code ->
Ui.R
library(shiny)
maxraw <- read.csv("C:/Users/Suchita/Desktop/maxraw.csv")
coln <- colnames(maxraw)
# Define UI for dataset viewer application
shinyUI(pageWithSidebar(
headerPanel('Iris k-means clustering'),
sidebarPanel(
selectInput('xcol', 'X Variable', choices = c(coln[26], coln[27], coln[28], coln[29])),
#selectInput('ycol', 'Y Variable', names(iris),
#selected=names(iris)[[2]]),
numericInput('clusters', 'Cluster count', 3,
min = 1, max = 9)
),
mainPanel(
plotOutput('plot1')
)
))
Server.R
library(shiny)
library(datasets)
maxraw <- read.csv("C:/Users/Suchita/Desktop/maxraw.csv")
# Define server logic required to summarize and view the selected
# dataset
shinyServer(function(input, output, session) {
# Combine the selected variables into a new data frame
selectedData <- reactive({
ss <- switch(input$xcol,
"Question1." = 26,
"Question2" = 27,
"Question3" = 28)
a = table(maxraw[,ss])
a = as.data.frame(a)
a$pct <- round(a$Freq/sum(a$Freq)*100) #calculated percentage
a$pcts <- paste(a$pct, "%") # add percents to labels
})
output$plot1 <- renderPlot({
pie(a$pct,labels = a$pcts, main = "Hospital Survey")
})
})
Here is the str(maxraw)
str(maxraw) 'data.frame': 43 obs. of 48 variables: $ Response.ID : int 21044194 21044264 21044287 21044402 21044435 21044481 21044529 21059249 21059266 21059297 ...
$ IP.Address : Factor w/ 6 levels "","122.177.157.116",..: 5 5 5 5 5 5 5 5 5 5 ...
$ Timestamp..MM.DD.YYYY. : Factor w/ 44 levels "","02/12/2014 04:30:20",..: 2 3 4 5 6 7 8 9 10 11 ...
$ Duplicate : logi FALSE FALSE FALSE FALSE FALSE FALSE ...
$ Time.Taken.to.Complete..Seconds. : int 146 125 181 94 111 112 575 149 115 0 ...
$ Response.Status : Factor w/ 3 levels "","Complete",..: 2 2 2 2 2 2 2 2 2 3 ...
$ Seq..Number : int 1 1 1 1 1 1 1 1 1 1 ...
$ External.Reference : logi NA NA NA NA NA NA ...
$ Custom.Variable.1 : logi NA NA NA NA NA NA ...
$ Custom.Variable.2 : logi NA NA NA NA NA NA ...
$ Custom.Variable.3 : logi NA NA NA NA NA NA ...
$ Custom.Variable.4 : logi NA NA NA NA NA NA ...
$ Custom.Variable.5 : logi NA NA NA NA NA NA ...
$ Respondent.Email : logi NA NA NA NA NA NA ...
$ Email.Group.Code : logi NA NA NA NA NA NA ...
$ Country.Code : Factor w/ 2 levels "","IN": 2 2 2 2 2 2 2 2 2 2 ...
$ Region : int 10 10 10 10 10 10 10 10 10 10 ...
$ Please.take.a.minute.to.give.us.your.feedback...it.helps.us.improve.Thank.you.very.much.for.your.time.and.support..Please.start.with.the.survey.now.by.clicking.on.the..B.Continue..B..button.below. : logi NA NA NA NA NA NA ...
$ Date.Of.Visit : Factor w/ 28 levels "","01/01/2014",..: 22 6 24 1 19 2 21 7 5 1 ...
$ First.Name : Factor w/ 39 levels "","Abhi","Afsar",..: 16 21 39 15 14 29 26 38 17 1 ...
$ Last.Name : Factor w/ 40 levels "","Abhinav","Ali",..: 24 37 35 19 33 13 29 25 9 1 ...
$ Phone : num 4.1e+07 4.1e+07 4.1e+07 4.1e+07 4.1e+07 ...
$ Email.Address : Factor w/ 40 levels "","[email protected]",..: 17 24 39 16 15 29 28 38 18 1 ...
$ Name.of.the.doctor. : Factor w/ 29 levels "","Dr Jholu",..: 29 17 14 28 28 26 12 5 18 1 ...
$ Max.ID. : num 45367298 65438900 67534373 67543923 78654389 ...
$ Satisfaction.With.Doctor.Was.the.Doctor.available.on.time. : int 4 3 2 3 NA 3 4 2 1 NA ...
$ Satisfaction.With.Doctor.Did.the.Doctor.treat.you.with.courtesy.and.respect. : int 4 2 3 4 2 3 4 3 1 NA ...
$ Satisfaction.With.Doctor.Did.the.Doctor.explain.your.diagnosis.and.treatment.plan.in.a.way.you.could.understand. : int 4 3 2 3 3 3 4 3 1 NA ...
$ Satisfaction.with.Nurses.Did.the.Nurses.treat.you.with.courtesy.and.respect. : int 4 3 2 4 3 4 4 3 1 NA ...
$ Appointment.Was.your.appointment.call.handled.efficiently.and.queries.resolved.to.your.satisfaction. : int 4 2 3 3 4 3 4 3 1 NA ...
$ Reception.Helpdesk.Was.the.Help.Desk.staff.at.the.hospital.helpful.and.courteous. : int 4 3 4 3 4 3 4 2 1 NA ...
$ Hospital.Infrastructure.Environment.Was.the.out.patient.department.location.convenient.to.identify. : int 4 2 2 3 4 2 4 3 1 NA ...
$ Hospital.Infrastructure.Environment.Did.the.areas.you.visited.in.the.hospital.look.clean.and.orderly. : int 4 3 3 4 3 1 4 2 1 NA ...
$ Hospital.Infrastructure.Environment.Were.the.public.area.washrooms.clean.and.hygienic. : int 4 3 2 3 4 2 4 2 1 NA ...
$ Front.Office.and.Billing.Did.the.front.office.staff.explain.and.resolve.your.query.regarding.registration.consult.diagnostics.charges.efficiently. : int 4 3 2 3 2 3 4 2 1 NA ...
$ Front.Office.and.Billing.Was.your.billing.handled.in.a.timely.and.accurate.manner. : int 4 2 3 2 1 2 4 NA 1 NA ...
$ Diagnostics.Services.Were.the.diagnostic.tests.conducted.in.a.timely.manner. : int 4 3 2 1 1 3 4 2 1 NA ...
$ Diagnostics.Services.Were.the.diagnostic.tests.conducted.efficiently.and.sensitively. : int 4 3 3 1 2 3 4 2 1 NA ...
$ Diagnostics.Services.Were.you.clearly.informed.about.report.delivery.time.and.mode.of.collection. : int 4 3 3 1 2 NA 4 2 1 NA ...
$ Max.Chemist.Were.all.the.prescribed.medicines.or.substitutes.available.at.the.chemist. : int 4 3 NA 2 1 4 4 2 1 NA ...
$ Max.Chemist.Did.you.find.the.services.at.the.pharmacy.efficient.and.timely. : int 4 4 NA 3 1 2 4 2 1 NA ...
$ Security...Parking.Did.you.find.our.car.parking.Valet.service.polite.and.efficient. : int 4 3 3 3 3 3 4 2 1 NA ...
$ How.likely.is.that.you.would.recommend.Max.Healthcare.to.a.friend.or.colleague. : int 9 7 6 4 7 8 10 6 1 NA ...
$ Any.additional.suggestions.or.comments : Factor w/ 31 levels ""," No","Abhinz was good",..: 28 29 18 21 31 NA 28 30 6 1 ...
$ Help.us.recognize.any.of.our.staff.who.served.you.exceptionally.well..by.providing.his.her.name. : logi NA NA NA NA NA NA ...
$ A. : Factor w/ 25 levels "","Abhinav","Abhinav ",..: 21 21 21 20 14 8 9 12 16 1 ...
$ B. : Factor w/ 24 levels "","Abhinav","balu ",..: 21 18 19 20 14 10 16 22 1 1 ...
$ C. : Factor w/ 17 levels "","Chiya","Dimple",..: 1 15 1 7 9 1 3 4 1 1 ...
I'm getting the error "$ operator is invalid for atomic vectors". Can someone pls suggest the way around.
Thanks.
Upvotes: 2
Views: 16731
Reputation: 9570
The error appears to be caused by the fact that selectedData
is returning a vector, rather than a data.frame, but you are trying to use it as a data.frame.
You need to explicitly return the full data.frame from selectedData
(e.g., add return(a)
at the bottom of that function). Then, you need to actually call selectedData()
within your renderPlot
call (e.g., start with a <- selectedData()
in output$plot1
)
Upvotes: 3
Reputation: 5206
I realise this is an old response and you might have fixed this already. This is more for others who have the dreaded $ operator is invalid for atomic vectors
message. For those having a >nuclear meltdown< see below...
Have you tried updating shinyapps? You have to do this from their github not the normal way (like in Rstudio update packages button), but from R
command line.
a) get devtools (if you don't have it)
install.packages('devtools')
b) re-install shiny apps from command line
devtools::install_github('rstudio/shinyapps')
Clue to "where is error?" Look at text ahead of the message as it might help track it down. Mine was:
Error in account$server : $ operator is invalid for atomic vectors
Which suggested a problem on shiny server side more than my code. It was a bug in their new roleout of shinyapps
, and updating shinyapps version cleared out my atomic error.
I hope Aarithmo you came right. Debugging shiny is tricky. Note you can put browser()
statements in your shiny code to debug. It will stop the code at that point and you check your variables for issues.
Upvotes: 2