简体   繁体   English

$运算符对原子向量无效:: R Shiny

[英]$ operator is invalid for atomic vectors :: R shiny

I have a customer survey of 25 questions. 我有一个针对25个问题的客户调查。 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. 问题的答案有“ 1”,“ 2”,“ 3”,“ 4”(1-非常好,2-好,3-正常,4-坏)可用。每行包含被调查者姓名,并给出所有答案他。

Data is in this format, respondent ID and the response value, Column header contains the Question name. 数据采用这种格式,包括响应者ID和响应值,列标题包含问题名称。

21044194- 1- 2- 4- 1- 3- 1- 1- 2- 1- 2- 1- 3- 2- 2- 1- 2- 4- 2 21044194- 1- 2- 4- 1- 3- 1- 1- 2- 1- 1- 1- 3- 2- 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 21044198- 1- 2- 4- 4- 3- 1- 1- 2- 1- 2- 1- 3- 2- 4- 4- 1- 3- 4- 2
21044199- 1- 2- 3- 1- 2- 3- 2- 1- 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- 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. 现在,我想创建一个闪亮的应用程序,其中包含所有25个问题的列表作为输入,并且在选择的问题的基础上,我需要显示答案的饼图。 Like this for 1 question 31% ppl choose Very Good, 22% choose Good, 31% choose Normal and 17% choose Bad. 像这样针对1个问题,31%的人选择非常好,22%的人选择好,31%的人选择正常,17%的人选择差。

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)

str(maxraw) 'data.frame': 43 obs. str(maxraw)'data.frame':43磅。 of 48 variables: $ Response.ID : int 21044194 21044264 21044287 21044402 21044435 21044481 21044529 21059249 21059266 21059297 ... 48个变量中的每一个:$ 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 ... $ IP.Address:带有6个级别的因数“”,“ 122.177.157.116”,..:5 5 5 5 5 5 5 5 5 5 ...
$ Timestamp..MM.DD.YYYY. $ Timestamp..MM.DD.YYYY。 : Factor w/ 44 levels "","02/12/2014 04:30:20",..: 2 3 4 5 6 7 8 9 10 11 ... :具有44个级别的系数“”,“ 2014年2月12日04:30:20”,..:2 3 4 5 6 7 8 9 10 11 ...
$ Duplicate : logi FALSE FALSE FALSE FALSE FALSE FALSE ... $复制:logi FALSE FALSE FALSE FALSE FALSE FALSE ...
$ Time.Taken.to.Complete..Seconds. $ Time.Taken.to.Complete..Seconds。 : int 146 125 181 94 111 112 575 149 115 0 ... :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 ... $ Response.Status:具有3个级别的因子“”,“ Complete”,..:2 2 2 2 2 2 2 2 2 3 ...
$ Seq..Number : int 1 1 1 1 1 1 1 1 1 1 ... $ Seq.Number:int 1 1 1 1 1 1 1 1 1 1 1 ...
$ External.Reference : logi NA NA NA NA NA NA ... $ External.Reference:logi不适用不适用不适用不适用...
$ Custom.Variable.1 : logi NA NA NA NA NA NA ... $ Custom.Variable.1:logi不适用不适用不适用不适用...
$ Custom.Variable.2 : logi NA NA NA NA NA NA ... $ Custom.Variable.2:登录名不适用不适用不适用不适用...
$ Custom.Variable.3 : logi NA NA NA NA NA NA ... $ Custom.Variable.3:登录名不适用不适用不适用不适用...
$ Custom.Variable.4 : logi NA NA NA NA NA NA ... $ Custom.Variable.4:logi不适用不适用不适用不适用...
$ Custom.Variable.5 : logi NA NA NA NA NA NA ... $ Custom.Variable.5:logi不适用不适用不适用不适用...
$ Respondent.Email : 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 ... $ 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 ... $ Country.Code:具有2个级别的“”,“ IN”因子:2 2 2 2 2 2 2 2 2 2 2 ...
$ Region : int 10 10 10 10 10 10 10 10 10 10 ... $地区:int 10 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. $请花一分钟的时间给您您的反馈,以帮助您非常感谢您的时间和支持。单击下面的B.Continue.B。按钮,开始进行调查。 : logi NA NA NA NA NA NA ... :logi NA 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 ... $ Date.Of.Visit:具有28个级别的系数“”,“ 2014年1月1日”,..: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 ... $ First.Name:具有39个级别的因子“”,“ 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 ... $ Last.Name:具有40个级别的因数“”,“ 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 ... $电话: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 ... $ Email.Address:具有40个级别的系数““,” 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 ... :29级水平“”,“ Jholu博士”,..:29 17 14 28 28 26 12 5 18 1 ...
$ Max.ID. $最大ID。 : num 45367298 65438900 67534373 67543923 78654389 ... :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 ... :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 ... :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 ... :int 4 3 2 3 3 3 4 4 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 ... :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 ... :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 ... :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 ... :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 ... :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 ... :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. $ Front.Office.and.Billing.Did.Front.Office.Staff.Expain.and.Resolve.Query.regard.registration.Consult.DiagnosticsCharges。高效地诊断和收费。 : int 4 3 2 3 2 3 4 2 1 NA ... :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. $ Front.Office.and.Billing.Was.h.and.accurate.accurate.manner。 : int 4 2 3 2 1 2 4 NA 1 NA ... :整数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 ... :整数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 ... :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 ... :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 ... :int 4 3 NA 2 1 4 4 2 1 NA ...
$ Max.Chemist.Did.you.find.the.services.at.the.pharmacy.efficient.and.timely. $ Max.Chemist.Did.you.find.theservices.at.thepharmacy.efficiency.and。 : int 4 4 NA 3 1 2 4 2 1 NA ... :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 ... :int 4 3 3 3 3 3 4 4 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 ... :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 ... $ Any.additional.suggestions.or.comments:31级水平“”,“否”,“ Abhinz很好”的系数..: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 ... :logi NA NA NA NA NA NA NA ...
$ A. : Factor w/ 25 levels "","Abhinav","Abhinav ",..: 21 21 21 20 14 8 9 12 16 1 ... $ A .:具有25个级别的因子“”,“ 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 ... $ B .:具有24个级别的因子“”,“ 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 ... $ C .:具有17个级别的系数“”,“ 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. 该错误似乎是由于selectedData返回向量而不是data.frame所引起的,但是您正在尝试将其用作data.frame。

You need to explicitly return the full data.frame from selectedData (eg, add return(a) at the bottom of that function). 您需要从selectedData显式返回完整的data.frame(例如,在该函数底部添加return(a) )。 Then, you need to actually call selectedData() within your renderPlot call (eg, start with a <- selectedData() in output$plot1 ) 然后,您需要在renderPlot调用中实际调用selectedData() (例如,从output$plot1 a <- selectedData() output$plot1 a <- selectedData() 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. 对于其他拥有令人恐惧的$ operator is invalid for atomic vectors消息$ operator is invalid for atomic vectors For those having a >nuclear meltdown< see below... 对于那些>核熔毁<的人,请参见下文...

  1. Have you tried updating shinyapps ? 您是否尝试过更新Shinyapps You have to do this from their github not the normal way (like in Rstudio update packages button), but from R command line. 您必须从他们的github而不是正常方式(例如在Rstudio更新包按钮中)执行此操作,而是从R命令行执行。

    a) get devtools (if you don't have it) a)获取devtools(如果您没有)

     install.packages('devtools') 

    b) re-install shiny apps from command line b)从命令行重新安装闪亮的应用程序

     devtools::install_github('rstudio/shinyapps') 
  2. 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 account $ server中的错误:$运算符对原子向量无效

    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. 这是他们的shinyapps新角色中的一个错误,更新Shinyapps版本可以清除我的原子错误。

I hope Aarithmo you came right. 希望Aarithmo您来对了。 Debugging shiny is tricky. 调试光泽是很棘手的。 Note you can put browser() statements in your shiny code to debug. 请注意,您可以在闪亮的代码中放置browser()语句进行调试。 It will stop the code at that point and you check your variables for issues. 此时它将停止代码,并检查变量是否存在问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM