错误:$运算符对原子向量无效(r闪亮的ggplot)

时间:2019-07-16 07:31:40

标签: r ggplot2 shiny

我正在尝试使用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

2 个答案:

答案 0 :(得分:0)

tabsetPanel仅接受一些tabPanel。您应该这样做:

mainPanel(
  tabsetPanel(
    tabPanel("Plot", plotOutput(outputId = "lineChart")), 
    type="tab"
  )
)

答案 1 :(得分:0)

错误在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)