Error in model.frame.default, variable lengths differ - r

I ran a glmer, and got the following the error message "Error in model.frame.default(data = data.density.EM.gra, weights = number_of_nest.boxes, : variable lengths differ (found for 'year')". I don't understand what this means despite reading a number of different posts regarding the same error.
here is my model:
model.1.EM.gra<-glmer(cbind(data.density$number.nest.boxes.occupied.that.year,data.density$number_of_nest.boxes)~ caterpillar.sc +(1|year),data = data.density.EM.gra,weights = number_of_nest.boxes,family = binomial)
I appreciate any suggestions you may have.
setwd("~/Word/UQAM/Master's_Reale/DATA/Blue tits data and instructions/csv") # work station
install.packages("dplyr")
#calling libraries.
library(dplyr)
library (reprex)
library(lme4)
data.density<-read.csv ("nest_box_caterpillar_density.csv")
data.density$year<-factor (data.density$year)# making year a factor (categorical variable)
str(data.density) # now we see year as a factor in the data.
#> 'data.frame': 63 obs. of 16 variables:
#> $ year : Factor w/ 9 levels "2011","2012",..: 1 2 3 4 5 6 7 8 9 1 ...
#> $ number.nest.boxes.occupied.that.year: int 17 13 12 16 16 16 15 17 12 17 ...
#> $ number_of_nest.boxes : int 20 20 20 20 20 20 20 20 20 30 ...
#> $ failure : int 3 3 3 3 3 3 3 3 3 13 ...
#> $ proportion_occupied_boxes : num 0.85 0.65 0.6 0.8 0.8 0.8 0.75 0.85 0.6 0.57 ...
#> $ site : Factor w/ 7 levels "ari","ava","fel",..: 5 5 5 5 5 5 5 5 5 1 ...
#> $ population : Factor w/ 3 levels "D-Muro","E-Muro",..: 2 2 2 2 2 2 2 2 2 2 ...
#> $ mean_yearly_frass : num 295 231 437 263 426 ...
#> $ site_ID : Factor w/ 63 levels "2011_ari_","2011_ava_",..: 5 12 19 26 33 40 47 54 61 1 ...
#> $ exploration_avg : num 13.28 14.19 9.85 9.42 8.67 ...
#> $ X : logi NA NA NA NA NA NA ...
#> $ X.1 : logi NA NA NA NA NA NA ...
#> $ X.2 : Factor w/ 2 levels "","failure means the total number of nest boxes -the number of nest boxes occupied. ": 1 1 1 1 1 1 1 1 1 2 ...
#> $ X.3 : logi NA NA NA NA NA NA ...
#> $ X.4 : logi NA NA NA NA NA NA ...
#> $ X.5 : Factor w/ 5 levels "","1 column with number of nest boxes used. ",..: 1 1 4 3 1 2 5 1 1 1 ...
#making new objects
density<-data.density$proportion_occupied_boxes # making a new object called density
caterpillar<-data.density$mean_yearly_frass # making new object called caterpillar
caterpillar.sc<-scale(caterpillar)
data.density.EM<-filter(data.density,population=='E-Muro') # data for population 'E-Muro'
data.density.EM.gra<-filter(data.density.EM,site=='gra') # data for site gra in in the E-Muro population.
View(data.density.EM.gra)
model.1.EM.gra<-glmer(cbind(data.density$number.nest.boxes.occupied.that.year,data.density$number_of_nest.boxes)~ caterpillar.sc +(1|year),
data = data.density.EM.gra,
weights = number_of_nest.boxes,
family = binomial)
#> Error in model.frame.default(data = data.density.EM.gra, weights = number_of_nest.boxes, : variable lengths differ (found for 'year')

Related

SensoMineR panelperf error in contrasts , contrasts can only be applied to factors with 2 or more levels

I am trying to run the following code using the panelperf function from the SensoMineR package:
panelperf<-panelperf(data,formul="~Product+Panelist+rep+Product:Panelist+Product:rep+Panelist:rep", firstvar=4)
My data frame consists on: column 1: panelists, filled with names of panelists
column 2: product: filled with the names of the products
column 3: rep , the replicate of the product measured
column 4 till the end: variables that were measured (light fruit, dark fruit, alcohol, sourness, etc)
All my variables are dbl
But I get the following error when running the function:
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : contrasts can be applied only to factors with 2 or more levels In addition: Warning messages: 1: In xtfrm.data.frame(x) : cannot xtfrm data frames 2: In xtfrm.data.frame(x) : cannot xtfrm data frames 3: In xtfrm.data.frame(x) : cannot xtfrm data frames
sapply(lapply(data, unique), length)
Product Panelist rep Lightfruit Darkfruit Applepear Citrus DryFruit Nutty Vegetables Earthy Floral
13 12 2 87 83 72 67 76 69 76 67 66
Woddy hgt Chemical Chocolate Honey Cheesy Alcohol Overallaroma Astringent Sour Hot Viscocity
62 64 80 57 65 69 86 85 88 85 85 83
Sweet Bitter
87 86
So none of the variables has only 2 levels as the error suggests
I have been reading answers about this error, but either the answers dont apply to my case or, as a non very experienced R user, I do not follow what they suggest.
I would appreciate your help a lot!
Thank you!
And let me know if you need more information!
The code to check the factor levels can be
i1 <- sapply(data, \(x) is.factor(x) && nlevels(x) > 1)
i2 <- !sapply(data, is.factor)
data1 <- data[i1|i2]
Here is an example that shows the issue - with the data having nlevels for factor > 1, it works
library(SensoMineR)
data(chocolates)
res <- panelperf(sensochoc, firstvar = 5, formul = "~Product+Panelist+
Session+Product:Panelist+Session:Product+Panelist:Session")
> str(res)
List of 4
$ p.value : num [1:14, 1:6] 8.85e-14 6.44e-08 1.75e-28 3.74e-40 1.18e-22 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:14] "CocoaA" "MilkA" "CocoaF" "MilkF" ...
.. ..$ : chr [1:6] "Product " "Panelist " "Session " "Product:Panelist" ...
$ variability: num [1:14, 1:6] 0.139 0.103 0.397 0.532 0.267 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:14] "CocoaA" "MilkA" "CocoaF" "MilkF" ...
.. ..$ : chr [1:6] "Product " "Panelist " "Session " "Product:Panelist" ...
$ res : num [1:14, 1] 1.87 1.89 1.41 1.47 1.63 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:14] "CocoaA" "MilkA" "CocoaF" "MilkF" ...
.. ..$ : chr "stdev residual"
$ r2 : num [1:14, 1] 0.673 0.761 0.846 0.882 0.862 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:14] "CocoaA" "MilkA" "CocoaF" "MilkF" ...
.. ..$ : chr "r2"
> str(sensochoc)
'data.frame': 348 obs. of 18 variables:
$ Panelist : Factor w/ 29 levels "1","2","3","4",..: 1 1 1 1 1 1 2 2 2 2 ...
$ Session : Factor w/ 2 levels "1","2": 1 1 1 1 1 1 1 1 1 1 ...
$ Rank : Factor w/ 6 levels "1","2","3","4",..: 1 6 3 5 2 4 1 4 3 5 ...
$ Product : Factor w/ 6 levels "choc1","choc2",..: 6 3 2 1 4 5 4 3 6 2 ...
$ CocoaA : int 7 6 8 7 8 7 6 4 5 5 ...
$ MilkA : int 6 7 6 8 5 5 1 2 1 2 ...
$ CocoaF : int 6 2 5 8 4 3 8 3 8 8 ...
$ MilkF : int 5 7 4 3 4 5 1 4 1 1 ...
$ Caramel : int 5 8 7 3 4 6 0 0 0 0 ...
$ Vanilla : int 3 4 4 2 4 2 0 0 0 0 ...
$ Sweetness : int 7 7 5 4 5 5 1 5 1 0 ...
$ Acidity : int 2 2 5 7 6 4 0 0 0 0 ...
$ Bitterness : int 4 2 6 8 6 7 3 0 3 6 ...
$ Astringency: int 5 3 6 6 4 4 0 0 0 0 ...
$ Crunchy : int 8 3 7 3 6 6 8 4 6 8 ...
$ Melting : int 3 8 5 2 3 6 5 8 2 2 ...
$ Sticky : int 4 6 4 3 7 4 0 3 1 4 ...
$ Granular : int 3 5 3 5 3 7 0 1 1 0 ...
If we change the 'Session' level 2 to NA (which have only 2 levels), it shows the error
levels(sensochoc$Session)[2] <- NA
res1 <- panelperf(sensochoc, firstvar = 5, formul = "~Product+Panelist+
Session+Product:Panelist+Session:Product+Panelist:Session")
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels
With OP's code, it still shows the data to be having more than 1 level because unique returns NA as well if present and thus the length will include the NA element, here it is 2 in total
> sapply(lapply(sensochoc, unique), length)
Panelist Session Rank Product CocoaA MilkA CocoaF MilkF Caramel Vanilla Sweetness Acidity
29 2 6 6 11 11 11 11 11 10 11 11
Bitterness Astringency Crunchy Melting Sticky Granular
11 11 11 11 11 11
where as with the specific code in this post, nlevels remove the NA an return only the count of non-NA levels
i1 <- sapply(sensochoc, \(x) is.factor(x) && nlevels(x) > 1)
i2 <- !sapply(sensochoc, is.factor)
names(sensochoc)[i1]
[1] "Panelist" "Rank" "Product"
names(sensochoc)[sapply(sensochoc, is.factor)]
[1] "Panelist" "Session" "Rank" "Product"
Session is omitted. We may need to change the formula to omit the terms that have Session

Error when trying to use one_hot encoding

I know this may be a potential duplicate question, but I found other answers didn't work in my situation.
I am using the following dataset:
> str(total_data)
'data.frame': 32260 obs. of 13 variables:
$ age : int 40 42 44 32 25 31 30 30 27 28 ...
$ workclass : Factor w/ 4 levels "Other-Unknown",..: 3 2 2 1 2 2 2 3 2 3 ...
$ education : Ord.factor w/ 7 levels "1"<"2"<"3"<"4"<..: 2 3 2 2 2 3 2 2 2 2 ...
$ marital.status : Factor w/ 5 levels "Divorced","Married",..: 2 1 2 3 3 3 3 2 2 3 ...
$ occupation : Factor w/ 6 levels "Blue-Collar",..: 5 3 6 2 1 6 6 1 1 6 ...
$ race : Factor w/ 5 levels "Amer-Indian-Eskimo",..: 1 5 1 1 5 5 5 5 5 5 ...
$ sex : Factor w/ 2 levels "Female","Male": 2 2 2 1 2 2 2 2 1 1 ...
$ hours.per.week : int 84 40 40 38 40 38 48 70 35 38 ...
$ naitive.country: Factor w/ 41 levels "?","Cambodia",..: 39 39 39 39 39 39 39 12 39 39 ...
$ classifier : chr "<=50K" "<=50K" ">50K" "<=50K" ...
$ class_num : Factor w/ 2 levels "1","2": 1 1 2 1 1 1 1 2 1 1 ...
$ age_norm : num 0.315 0.342 0.37 0.205 0.11 ...
$ hours_norm : num 0.847 0.398 0.398 0.378 0.398 ...
I'm trying to encode the factors into binary using one_hot() but receive the following error message:
encoded_data <- one_hot(total_data, dropCols = FALSE)
ERROR MESSAGE:
Error in `[.data.frame`(dt, , cols, with = FALSE) :
unused argument (with = FALSE)
I'm not sure what the "with" argument is as I don't see it in the R documentation.
I also saw that someone suggested to use model.matrix. However, when I use that, my ordered factor gets encoded as well, which is what I'm trying to avoid.
This is what happens to my ordered factor variable:
education.L education.Q education.C education^4 education^5 education^6
-3.779645e-01 9.690821e-17 4.082483e-01 -0.5640761 4.364358e-01 -0.19738551
-1.889822e-01 -3.273268e-01 4.082483e-01 0.0805823 -5.455447e-01 0.49346377
I'm also not sure why there are sometimes letters or numbers after the attribute name. i.e. education**.L** vs education**^5**
Convert the data.frame into a data.table and it should work fine.
library(data.table)
dt = data.table(total_data)
one_hot(dt)

Issues producing a ROC curve with a KNN Model - undefined columns

I do apologize if this is rudimentary however I have run through the tracebook and tried googling to no real avail. Every time I try and run my code to produce a ROC curve I keep getting returned
Error in `[.data.frame`(data, , class) : undefined columns selected
I have checked the data and they are single column characters (as required)
library(cutpointr)
Temp1 <- predict(KnnModel, newdata=TestData, type="prob")
KnnProbs <- predict(object = KnnModel, newdata = TestData, type = "prob")
KnnProbs <- as.character(KnnProbs$`0`)
clch <- as.character(TrainData$loan_status)
KnnROC <- roc(data = TestData$loan_status, x = KnnProbs, class = clch)
plot(KnnROC, print.auc = T)
Any ideas as to what I am doing wrong and how to fix this
EDIT: The TrainData is of the following
'data.frame': 1500 obs. of 13 variables:
$ loan_amnt : num 6000 17625 8500 5000 10000 ...
$ loan_status : Factor w/ 2 levels "0","1": 1 2 1 1 1 1 1 1 1 1 ...
$ int_rate : num 13.33 15.61 6.68 6.92 14.98 ...
$ term : num 1 1 1 1 1 1 2 1 1 1 ...
$ installment : num 203 616 261 154 347 ...
$ grade : num 3 4 1 1 3 4 4 3 2 1 ...
$ emp_length : num 10 11 3 3 8 3 3 3 2 1 ...
$ annual_inc : num 30000 49000 53100 60000 37000 ...
$ dti : num 25.5 12.2 26.2 27.8 31.4 ...
$ sub_grade : num 13 16 3 4 13 16 18 12 8 4 ...
$ verification_status: num 1 2 2 3 3 3 3 1 3 1 ...
$ home_ownership : Factor w/ 6 levels "ANY","MORTGAGE",..: 6 6 6 5 2 2 2 2 6 2 ...
$ pymnt_plan : Factor w/ 2 levels "n","y": 1 1 1 1 1 1 1 1 1 1 ...

Contingency table with conditional probability using R

I have those data: http://www.unige.ch/ses/spo/static/simonhug/madi/Mitchell_et_al_1984.csv
> str(dataset)
'data.frame': 135 obs. of 13 variables:
$ CCode : int 2 20 40 41 42 51 52 70 90 91 ...
$ StateAbb : Factor w/ 130 levels "AFG","ALB","ALG",..: 124 19 28 52 33 62 117 75 49 53 ...
$ StateNme : Factor w/ 130 levels "Afghanistan",..: 122 20 27 51 33 62 116 76 47 52 ...
$ prison_score : Factor w/ 5 levels "never","often",..: 1 1 2 4 5 1 NA 4 5 4 ...
$ torture_score : Factor w/ 5 levels "never","often",..: 1 3 1 4 2 1 NA 2 5 2 ...
$ ht_colonial : Factor w/ 10 levels "0. Never colonized by a Western overseas colonial power",..: 1 1 4 8 4 7 7 4 4 4 ...
$ british : int NA NA 0 0 0 1 1 0 0 0 ...
$ british_colony : Factor w/ 2 levels "no","yes": NA NA 1 1 1 2 2 1 1 1 ...
$ continent : Factor w/ 5 levels "Africa","Americas",..: 2 2 2 2 2 2 2 2 2 2 ...
$ region_wb : Factor w/ 19 levels "Australia and New Zealand",..: 10 10 2 2 2 2 2 3 3 3 ...
$ gdppc_l1 : num 25839 23550 10095 1846 4758 ...
$ colonialExperience: chr NA NA "Other Colonial Background" "Other Colonial Background" ...
And have to create a similar result
With this code
# Copy the torture_score in a new col
dataset$torture_score_new = dataset$torture_score
# Add a level to the factor torture_score_new so we can t
levels(dataset$torture_score_new) = c(levels(dataset$torture_score_new), "rarely or never")
### Recode variables
# Torture score
dataset$torture_score_new[dataset$torture_score == "rarely"] = "rarely or never"
dataset$torture_score_new[dataset$torture_score == "never"] = "rarely or never"
dataset$torture_score_new = droplevels(dataset$torture_score_new)
dataset$torture_score_new = ordered(dataset$torture_score_new, levels =c("rarely or never", "somtimes", "often", "very often"))
### Text
dataset$colonialExperience = ifelse(dataset$british_colony == "yes",
"Former British Colony",
"Other Colonial Background")
useOfTortureByColonialExperience = table(dataset$torture_score_new, dataset$colonialExperience)
addmargins(round(prop.table(useOfTortureByColonialExperience)*100,2),1)
and get this result
Former British Colony Other Colonial Background
rarely or never 9.76 20.73
somtimes 10.98 15.85
often 6.10 18.29
very often 10.98 7.32
Sum 37.82 62.19
But I don't understand how to use conditional stat and get the Chi Square.
(I'm a programmer, but a total newbe to R)
Ok it's what I end up doing.
useOfTortureByColonialExperience = table(dataset$torture_score_new, dataset$colonialExperience)
# Get the number of observation
addmargins(useOfTortureByColonialExperience,1);
# Contingency table with conditional probability
useOfTortureByColonialExperienceProp = prop.table(useOfTortureByColonialExperience,2)
print(addmargins(useOfTortureByColonialExperienceProp*100,1),3)
## Chisq
chisq.test(useOfTortureByColonialExperience)
cramersV(useOfTortureByColonialExperience)

$ operator is invalid for atomic vectors :: R shiny

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 "","aali#gmail.com",..: 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.
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)
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.

Resources