【问题标题】:$ operator is invalid for atomic vectors :: R shiny$ 运算符对原子向量无效 :: R shiny
【发布时间】:2014-05-15 07:13:44
【问题描述】:

我有一个包含 25 个问题的客户调查。问题的答案有“1”、“2”、“3”、“4”(1-非常好、2-好、3-正常、4-差) 每行都包含被访者的姓名以及他给出的所有答案。

数据采用这种格式,被访者ID和响应值,列标题包含问题名称。

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

现在我想创建一个闪亮的应用程序,其中我有一个包含所有 25 个问题的列表作为输入,并且我需要根据选定的问题显示答案的饼图。对于 1 个问题,31% 的人选择非常好,22% 的人选择好,31% 的人选择正常,17% 的人选择差。

我写了以下代码->

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")
 })

 })

这里是 str(maxraw)

str(maxraw) 'data.frame':43 obs。 48 个变量: $ Response.ID : int 21044194 21044264 21044287 21044402 21044435 21044481 21044529 21059249 21059266 21059297 ...
$ IP.Address : 6 个级别的因子 "","122.177.157.116",..: 5 5 5 5 5 5 5 5 5 5 ...
$ 时间戳..MM.DD.YYYY。 : 因子 w/ 44 个级别 "","02/12/2014 04:30:20",..: 2 3 4 5 6 7 8 9 10 11 ...
$ 重复:logi FALSE FALSE FALSE FALSE FALSE FALSE ...
$ Time.Taken.to.Complete..Seconds。 : 整数 146 125 181 94 111 112 575 149 115 0 ...
$ Response.Status : 因子 w/ 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 ...
$ 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 : 因子 w/ 2 个级别 "","IN": 2 2 2 2 2 2 2 2 2 2 ...
$ 区域: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..拜托。 start.with.the.survey.now.by.clicking.on.the..B.Continue..B..button.below. : 逻辑 NA NA NA NA NA NA ...
$ Date.Of.Visit : 因子 w/ 28 个级别 "","01/01/2014",..: 22 6 24 1 19 2 21 7 5 1 ...
$ First.Name : 因子 w/ 39 个级别 "","Abhi","Afsar",..: 16 21 39 15 14 29 26 38 17 1 ...
$ Last.Name : 因子 w/ 40 个级别 "","Abhinav","Ali",..: 24 37 35 19 33 13 29 25 9 1 ...
$电话:号码 4.1e+07 4.1e+07 4.1e+07 4.1e+07 4.1e+07 ...
$ Email.Address : 因子 w/ 40 级别 "","aali@gmail.com",..: 17 24 39 16 15 29 28 38 18 1 ...
$ 医生姓名。 : 因子 w/ 29 个级别 "","Dr Jholu",..: 29 17 14 28 28 26 12 5 18 1 ...
$ 最大 ID。 : 号码 45367298 65438900 67534373 67543923 78654389 ...
$ Satisfaction.With.Doctor.Was.the.Doctor.available.on.time。 : int 4 3 2 3 不适用 3 4 2 1 不适用 ...
$ Satisfaction.With.Doctor.Did.the.Doctor.treat.you.with.courtesy.and.respect。 : int 4 2 3 4 2 3 4 3 1 不适用 ...
$ 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 不适用 ...
$ Satisfaction.with.Nurses.Did.the.Nurses.treat.you.with.courtesy.and.respect。 : int 4 3 2 4 3 4 4 3 1 不适用 ...
$ Appointment.Was.your.appointment.call.handled.efficiently.and.queries.resolved.to.your.satisfaction。 : int 4 2 3 3 4 3 4 3 1 不适用 ...
$ Reception.Helpdesk.Was.the.Help.Desk.staff.at.the.hospital.helpful.and.courteous。 : int 4 3 4 3 4 3 4 2 1 不适用 ...
$ Hospital.Infrastructure.Environment.Was.the.out.patient.department.location.convenient.to.identify。 : int 4 2 2 3 4 2 4 3 1 不适用 ...
$ 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 不适用 ...
$ Hospital.Infrastructure.Environment.Were.the.public.area.washrooms.clean.and.hygienic。 : int 4 3 2 3 4 2 4 2 1 不适用 ...
$ 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 不适用 ...
$ 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 不适用 ...
$ Diagnostics.Services.Were.the.diagnostic.tests.conducted.efficiently.and.sensitively。 : int 4 3 3 1 2 3 4 2 1 不适用 ...
$ 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 不适用 2 1 4 4 2 1 不适用 ...
$ Max.Chemist.Did.you.find.the.services.at.the.pharmacy.efficient.and.timely。 : int 4 4 不适用 3 1 2 4 2 1 不适用 ...
$ 安全...Parking.Did.you.find.our.car.parking.Valet.service.polite.and.efficient。 : int 4 3 3 3 3 3 4 2 1 不适用 ...
$ How.likely.is.that.you.would.recommend.Max.Healthcare.to.a.friend.or.colleague. : int 9 7 6 4 7 8 10 6 1 不适用 ...
$ Any.additional.suggestions.or.cmets :因子 w/ 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。 : 逻辑 NA NA NA NA NA NA ...
$ A. : 因子 w/ 25 个级别 "","Abhinav","Abhinav ",..: 21 21 21 20 14 8 9 12 16 1 ...
$ B. : 因子 w/ 24 个级别 "","Abhinav","balu",..: 21 18 19 20 14 10 16 22 1 1 ...
$ C. : 17 个级别的因子 "","Chiya","Dimple",..: 1 15 1 7 9 1 3 4 1 1 ...

我收到错误“$ 运算符对原子向量无效”。 有人可以建议一下吗?

谢谢。

【问题讨论】:

  • 运行调试器或回溯以查看导致问题的行
  • 这肯定与读取 csv.file 的格式有关。您的问题表述得很好,但由于数据的格式不是唯一的,因此尚不清楚读取的内容。将您的 read.csv 放入global.r(无需阅读两次),然后发布str(maxraw)
  • 谢谢大家的快速响应。我不知道它的全球声明。会使用它。还有 Dieter Menne,我已经添加了 str(maxraw).. 你能建议解决这个问题的方法吗..

标签: r shiny


【解决方案1】:

该错误似乎是由于 selectedData 返回的是矢量而不是 data.frame,但您正试图将其用作 data.frame。

您需要从selectedData 显式返回完整的data.frame(例如,在该函数的底部添加return(a))。然后,您需要在您的renderPlot 调用中实际调用selectedData()(例如,在output$plot1 中以a &lt;- selectedData() 开头)

【讨论】:

    【解决方案2】:

    我知道这是一个旧的回复,你可能已经修复了这个问题。这更适用于其他有可怕的$ operator is invalid for atomic vectors 消息的人。对于那些有>核熔毁

    1. 您是否尝试过更新 shinyapps?您必须从他们的 github 执行此操作,而不是常规方式(例如在 Rstudio 更新包按钮中),而是从 R 命令行执行此操作。

      a) 获取 devtools(如果你没有的话)

      install.packages('devtools')
      

      b) 从命令行重新安装闪亮的应用程序

      devtools::install_github('rstudio/shinyapps')
      
    2. 提示“哪里出错了?”查看邮件前面的文字,因为它可能有助于追踪它。我的是:

      account$server 中的错误:$ 运算符对于原子向量无效

      这比我的代码更能说明闪亮的服务器端的问题。这是他们新角色shinyapps 中的一个错误,更新shinyapps 版本清除了我的原子错误。

    我希望 Aarithmo 你来对了。调试闪亮是棘手的。请注意,您可以将 browser() 语句放在闪亮的代码中进行调试。它将在此时停止代码,并检查变量是否存在问题。

    【讨论】:

      猜你喜欢
      • 2021-08-20
      • 2013-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-23
      • 1970-01-01
      相关资源
      最近更新 更多