我正在尝试显示一个散点图,其中包含通过用户点击过滤数据的闪亮效果。 但是,我得到一个错误:
警告:$:$运算符对原子向量无效[否 可用的堆栈跟踪]
警告:renderUI中的错误:객체无法找到“ data_available” [否 可用的堆栈跟踪]
我不知道哪里出了问题。 以下是我的闪亮应用程序的代码:
library(shiny)
library(ggplot2)
library(dplyr)
data_events <- read.csv("Desktop/athlete_events.csv")
df.physical <-data_events %>% select(Height,Weight,Sex,Year,Sport) %>%
filter(!is.na(Height),!is.na(Weight))
df.physical <-as.data.frame(df.physical)
ui<- fluidPage(
titlePanel("Distribution of Height/Weight for Each Sport"),
sidebarLayout(
sidebarPanel(
helpText("Create a scatter plot of height/weight for every sport in the
Olympics."),
htmlOutput("sport_selector"),
htmlOutput("year_selector"),
htmlOutput("sex_selector")
)
,
mainPanel(
plotOutput("olmypicphysical"))
)
)
server=shinyServer(function(input,output){
output$sport_selector = renderUI({
selectInput(inputId="sport", label= "Sport:",
choices=as.character(unique(df.physical$Sport))
)})
output$year_selector = renderUI({
data_available=df.physical[df.physical$Sport == input$sport, "Year"]
selectInput(inputId = "year", label="Year:",
choices=unique(data_available$Year))
})
output$sex_selector = renderUI({
data_available1=data_available[data_available$Year == input$year, "Sex"]
selectInput(inputId = "Sex",label="Sex:",
choices=unique(data_available1$Sex))
data_available2=data_available1[data_available1$Sex ==input$sex,
"Physical"]
output$olympicphysical = renderPlot({
ggplot(data_available2,aes(x=data_available2$Height,y=data_available2$Weight)
)+ geom_point()+theme_classic()+labs(x="Height",y="Weight")
})
})
})
shinyApp(ui = ui, server = server)
我创建的数据集df.physical(类型为列表)如下所示:
请问有人可以帮我吗?
答案 0 :(得分:0)
您的代码存在很多问题。您的问题是关于$ operator is invalid for atomic vectors
。确实,这是由您的选择方法引起的。首先,从data.frame中选择一个向量,然后尝试再次使用data.frame中具有的名称对该向量进行子集化。
基本上,您尝试两次应用相同的选择。简单的解决方法是在第一个选择中省略列名:
data_available=df.physical[df.physical$Sport == input$sport, ]
此外,我不确定为什么将htmlOutput
与renderUI
结合使用。如果要更新/限制selectInput
中的选择,则可以仅将功能updateSelectInput
与ObserveEvent
结合使用。这样可以使您的应用程序更具可读性,并且不易出错。
工作示例:
library(shiny)
library(ggplot2)
ui<- fluidPage(
titlePanel("Distribution of Height/Weight for Each Sport"),
sidebarLayout(
sidebarPanel(
helpText("Create a scatter plot of height/weight for every sport in the
Olympics."),
selectInput("sport_selector", choices=c(''), label='Sport:'),
selectInput("year_selector", choices=c(''), label='Year:'),
selectInput("sex_selector", choices=c(''), label='Sex:')
)
,
mainPanel(
plotOutput("olympicphysical"))
)
)
server<-shinyServer(function(input,output, session){
df.physical = data.frame(Height=c(170,180),Weight=c(80,80),Sex=c('M','F'), Year=c(1992,2012), Sport=c('Basketball', 'Judo'))
updateSelectInput(session, 'sport_selector', choices = unique(df.physical$Sport))
observeEvent(input$sport_selector, {
df.physical_subset = subset(df.physical, Sport == input$sport_selector)
updateSelectInput(session, 'year_selector', choices = unique(df.physical_subset$Year))
})
observeEvent({input$year_selector}, {
df.physical_subset = subset(df.physical, Sport == input$sport_selector & Year == input$year_selector)
updateSelectInput(session, 'sex_selector', choices = unique(df.physical_subset$Sex))
})
observeEvent({input$sex_selector},{
df.physical_subset <- subset(df.physical, Sport == input$sport_selector & Year == input$year_selector & Sex==input$sex_selector)
print(df.physical_subset)
output$olympicphysical = renderPlot({
ggplot(df.physical_subset,aes(x=df.physical_subset$Height,y=df.physical_subset$Weight)
)+ geom_point()+theme_classic()+labs(x="Height",y="Weight")})
})
})
shinyApp(ui = ui, server = server)