【问题标题】:Shiny: $ operator is invalid for atomic vectors闪亮:$ 运算符对原子向量无效
【发布时间】: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(类型为列表)如下所示:

df.physical

谁能帮帮我?

【问题讨论】:

  • 问题在于data_availabledata_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

标签: r dataframe shiny


【解决方案1】:

您的代码存在很多问题。你的问题是关于$ operator is invalid for atomic vectors。确实,这是由您的选择方法引起的。首先,您从 data.frame 中选择一个向量,然后尝试按其在 data.frame 中的名称再次对该向量进行子集化。

基本上,您会尝试两次应用相同的选择。简单的解决方法是在第一个选择中省略列名:

data_available=df.physical[df.physical$Sport == input$sport, ]

此外,我不确定您为什么将htmlOutputrenderUI 结合使用。如果您想在selectInput 中更新/限制您的选择,您可以将函数updateSelectInputObserveEvent 结合使用。这使您的应用程序更具可读性,并且不易出错。

工作示例:

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)

【讨论】:

    猜你喜欢
    • 2021-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-17
    • 1970-01-01
    • 2014-05-15
    相关资源
    最近更新 更多