gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 21:28:40 25 4
gpt4 key购买 nike

我有一项包含 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

现在我想创建一个 Shiny 的应用程序,其中我有一个包含所有 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')
)
))

服务器.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. 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 ...

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

谢谢。

最佳答案

该错误似乎是由 selectedData 引起的返回一个向量,而不是 data.frame,但您试图将其用作 data.frame。

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

关于r - $ 运算符对于原子向量无效::R Shiny ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22837964/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com