我正试图建立一个R Shiny仪表盘,为我的运动数据显示几个页面。我被如何编写显示每个训练者所消耗的卡路里分布的boxplots仪表盘的代码所困扰。 我想发生的是有一个selectInput,允许用户从训练者列表中选择,这个选择将更新boxplot。 但是,所选择的变量将是一个虚拟变量,我将所选择的训练者的boxplot与 "所有其他 "训练者并排比较。
下面是我的数据集的一个例子。
VisitNum VisitNumMonth Date ClassLength Calories AvgHR Location Trainer Visits_Trainer
1 1 12/15/19 56 602 128 A Mike 4
2 1 12/16/19 55 610 130 A Mike 4
3 2 01/04/20 52 550 120 A Sally 2
4 2 01/05/20 61 575 134 B Jennie 5
5 2 01/10/20 57 654 133 A Tim 1
6 2 01/17/20 55 592 119 A Rachael 1
7 3 02/04/20 50 632 129 B Jennie 5
8 3 02/22/20 48 630 125 B Jennie 5
9 3 02/28/20 59 609 136 B Marshall 6
10 4 03/03/20 53 598 134 A Mike 4
这是我的代码
library(RSQLite)
library(sqldf)
library(shiny)
library(shinydashboard)
library(DT)
library(ggplot2)
library(shiny)
data=as.data.frame(read_excel("mydata.xlsx",sheet=1))
#Create "Other" value for Trainers with less than 3 visits
data$Trainer2=ifelse(data$Visits_Trainer>=3,data$Trainer,"Other")
#Remove "Other" as possible selection in SelectInput
TrainerChoices=unique(data$Trainer2)
TrainerChoices=TrainerChoices[!TrainerChoices %in% "Other"]
Table1=sqldf("select VisitNum as 'Visit #', Date, Location as 'Studio', Trainer, Calories, AvgHR
from data
group by VisitNum, Date, Location, Trainer")
# Define UI for application
ui <- fluidPage(
titlePanel("Exercise Data Analysis"),
dashboardPage(
dashboardHeader(title=""),
dashboardSidebar(
sidebarMenu(
menuItem("All Data",
tabName = "AllData",
icon=icon("table")
),
menuItem("Trainer Boxplots",
tabName = "trainbox",
icon=icon("dumbbell")
),
selectInput(inputId = "trainerselect",label="Select a Trainer:",choices=TrainerChoices)
)
),
dashboardBody(
tabItems(
tabItem(
tabName = "AllData",
DTOutput("alldata")
),
tabItem(
tabName = "trainbox",
plotOutput("trainbox")
))
)
)
)
# Define server logic
server <- function(input, output) {
#----------ALL DATA TABLE----------#
output$alldata=renderDT({
datatable(Table1,options=list(pageLength=10,
lengthMenu=c(10,15,20,25)
),rownames = FALSE)
})
#---------- TRAINER BOXPLOTS ----------#
TrainData=reactive({
NewDummy=ifelse(data$Trainer2==input$TrainerChoices,input$TrainerChoices,"Other")
return(NewDummy)
})
output$trainbox=renderPlot({
plot=ggplot(TrainData(),aes(y=Calories,x=as.factor(NewDummy)))+geom_boxplot(aes(col=as.factor(NewDummy)))
plot
})
}
# Run the application
shinyApp(ui = ui, server = server)
当我运行这段代码时,"所有数据 "选项卡显示得很好, 但我不能让训练者图表适当地显示出来。 页面上出现了一个错误,内容如下。
`data` must be a data frame, or other object coercible by `fortify()`, not a logical vector
谁能帮我指出正确的方向? 任何帮助将被感激! 谢谢您
我不确定这是否是你想要的,但也许这将会有所帮助。
首先,你的 selectInput
在 ui
有一个ID trainerselect
,所以你需要通过 input$trainerselect
不 TrainerChoices
.
您的 TrainData
反应式表达式将返回一个字符值(培训师的名字或 "其他");我假设你想使用 data
为你的数据源,而不是这个。NewDummy
是反应式表达式中的一个局部变量。也许这就是你想从 TrainData()
.
如果我弄错了,请告诉我。
server <- function(input, output) {
#----------ALL DATA TABLE----------#
output$alldata=renderDT({
datatable(Table1,options=list(pageLength=10,
lengthMenu=c(10,15,20,25)
),rownames = FALSE)
})
#---------- TRAINER BOXPLOTS ----------#
TrainData=reactive({
NewDummy=ifelse(data$Trainer2==input$trainerselect,input$trainerselect,"Other")
return(NewDummy)
})
output$trainbox=renderPlot({
NewDummy <- TrainData()
plot=ggplot(data,aes(y=Calories,x=as.factor(NewDummy)))+
geom_boxplot(aes(col=as.factor(NewDummy)))
plot
})
}