首页 > 解决方案 > 错误:$ 运算符对原子向量无效(r 闪亮的 ggplot)

问题描述

我正在尝试使用 shiny 和 ggplot 绘制一条平滑曲线,其中 x 轴是剩余的租赁年限,y 轴是平均转售价值。它应该能够对输入框做出反应。

但是,每当我运行该应用程序时,我都会不断收到此错误

错误:$ 运算符对原子向量无效

这是我的数据示例

flat_model flat_type remaining_lease resale_price
1    MODEL A    5 ROOM              70       200000
2    MODEL A    3 ROOM              70        64300
3    MODEL A    3 ROOM              70        60000
4    MODEL A    3 ROOM              70        59000
5    MODEL A    4 ROOM              70        78000
6    MODEL A    4 ROOM              70       104000

这是我的代码

> #shiny
> #Define UI
> library("shiny")
> ui <- fluidPage(
+     titlePanel("Average Resale Price for Model A houses has decline sharply by 50% when remaining lease years for model A houses starts reaching below 93 and 77 years "),
+     sidebarLayout(
+         sidebarPanel(
+             selectInput("room","Rooms",choices=c("All","2 ROOM","3 ROOM","4 ROOM","5 ROOM"),selected = "All")
+         ),
+     mainPanel(tabsetPanel(type="tab","Plot",plotOutput(outputId = "lineChart"))
+               )
+     ))
Error: $ operator is invalid for atomic vectors
> 
> 
> 
> # Define server logic required to draw a line graph ----
> 
> server <- function(input, output, session){
+     df1<-reactive({
+         if(input$room =="All"){
+             modeladata1%>%
+                 dplyr::filter(flat_type %in% c("2 ROOM","3 ROOM","4 ROOM","5 ROOM")  )
+         }
+         
+         else{
+             headlinedata%>%
+                 dplyr::filter(flat_type %in% input$room)
+         }
+     })
+     
+     output$lineChart <- renderPlot({
+         ggplot(data = df1(),aes(x=df1()$remaining_lease,y=df1()$resale_price))+
+             geom_smooth()
+     })
+ }
> 
> 
> # Create Shiny object
> shinyApp(ui = ui, server = server)

Error in force(ui) : object 'ui' not found

标签: rggplot2shiny

解决方案


错误出现在 ui 中的 tabsetPanel 中。为了使用 tabsetPanel,您必须在其中指定 tabPanel。有关更多详细信息,请参阅此https://shiny.rstudio.com/reference/shiny/0.14/tabsetPanel.html

这是工作代码

library("shiny")
library('ggplot2')
ui <- fluidPage(
    titlePanel("Average Resale Price for Model A houses has decline sharply by 50% when remaining lease years for model A houses starts reaching below 93 and 77 years "),
    sidebarLayout(
        sidebarPanel(
            selectInput("room", "Rooms", choices=c("All", "2 ROOM", "3 ROOM", "4 ROOM", "5 ROOM"), selected = "All")
        ),
        mainPanel(
            tabsetPanel(type = "tab", tabPanel("Plot", plotOutput(outputId = "lineChart")))
        )
    )
)



# Define server logic required to draw a line graph ----

server <- function(input, output, session){
    df1<-reactive({
        if(input$room =="All"){
            modeladata1%>%
                dplyr::filter(flat_type %in% c("2 ROOM","3 ROOM","4 ROOM","5 ROOM")  )
        }

        else{
            headlinedata%>%
                dplyr::filter(flat_type %in% input$room)
        }
    })

    output$lineChart <- renderPlot({
        ggplot(data = df1(),aes(x=df1()$remaining_lease,y=df1()$resale_price))+
            geom_smooth()
    })
}


# Create Shiny object
shinyApp(ui = ui, server = server)

推荐阅读