【发布时间】:2019-04-29 13:11:33
【问题描述】:
我试图用闪亮的散点图显示涉及通过用户点击过滤数据的散点图。 但是,我收到一个错误:
警告:$ 中的错误:$ 运算符对原子向量无效 [否 堆栈跟踪可用]
警告: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(类型为列表)如下所示:
谁能帮帮我?
【问题讨论】:
-
问题在于
data_available:data_available=df.physical[df.physical$Sport == input$sport, "Year"]返回一个原子向量,这意味着data_available$Year无效。此问题在后续对data_available的引用中仍然存在。 -
你所做的相当于:
mtcars[mtcars$cyl == 6,"mpg"]$mpg- 你会注意到它会引发同样的错误 -
修复,我相信你可以从
choices = unique(...)行中删除$Year、$Sex等 -
您好,我尝试了您的解决方案,但删除 $Year $Sex 等后得到的结果不正确。 data_available 显然仍然丢失,我在体育部分没有任何选择。有没有其他办法?
-
它现在丢失了,因为它不是全局变量。您完全以错误的方式使用闪亮。为什么不使用
updateSelectInput?