r - 错误:$ 运算符对原子向量无效(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
解决方案
错误出现在 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)
推荐阅读
- sql - 我可以使用调整大小的模型进行部分选择吗?
- python - 类型错误:get() 缺少 1 个必需的位置参数:'index1'
- c - xdotool 选项的问题
- python - 进程不加入多处理python
- python-3.x - Pyspark-Jupyter- Docker:在 jupyter/pyspark-notebook 中定义主节点和工作节点后如何重建和部署映像/容器?
- r - R: t.test 在 facet_grid (ggplot)
- python - 这两种从 DataFrame 中选择第一个和最后一个数据的方法有什么区别?
- html - 使用 Sass 和 Gulp 的背景图像路径
- reactjs - 如何将 es2020 功能添加到现有的 React 应用程序?
- oracle - OCI:尽管复制了示例格式,但 CLI json 字符串仍抱怨 json 不正确