闪亮的散点图
Scatterplot in Shiny
我正在尝试基于已加载的 csv 创建散点图,但是当我 运行 代码时,我要么没有显示任何图,要么在包含 aes 映射时出现错误:"Mapping should be created with aes()
or aes_()
."
任何人都可以指出我哪里出错了吗?
代码:
library(shiny)
library(ggplot2)
ui <- (fluidPage(
titlePanel("Pig Breeds"),
sidebarLayout(
sidebarPanel(
selectInput(inputId = "x",
label = "Pig Breeds:",
choices = c("total_pigs", "female_breeding_herd",
"in_pig_sows", "in_pig_gifts", "other_sows",
"maiden_gilts", "boars_for_service", "other_pigs"),
selected = "total_pigs"),
selectInput(inputId = "y",
label = "Year by year change:",
choices = c(2016, 2017, 2018, "year_on_year_change"),
selected = 2016),
actionButton(inputId = "update", label = "update")
),
mainPanel = (
plotOutput(outputId = "scatterplot")
)
)
)
)
server <- (function(input, output) {
output$scatterplot <- renderPlot({
ggplot(data=(read.csv("eu_pigs.csv")),
aes(x = output$x, y = output$y) +
geom_point())
observeEvent(input$update, {print(as.numeric(input$update))})
}
)
}
)
shinyApp(ui, server)
如错误消息所述,您使用的 aes
不正确。该函数采用 列名称 ,而不是变量引用。即替换
aes(x = output$x, y = output$y)
来自
aes(x = x, y = y)
或者,您更有可能希望能够从输入中控制情节,因此您想使用
aes_string(x = input$x, y = input$y)
您的代码中也有不少括号和大括号。删除那些。此外,mainPanel
是您需要调用的 函数 。您的代码正在为其分配一些内容。
最后,您实际上需要绘制您的情节。这些都搞定后,相关代码如下:
ui <- fluidPage(
titlePanel("Pig Breeds"),
sidebarLayout(
sidebarPanel(…),
mainPanel(
plotOutput(outputId = "scatterplot")
)
)
)
server <- function(input, output) {
output$scatterplot <- renderPlot({
p = ggplot(data = read.csv("eu_pigs.csv")) +
aes_string(x = input$x, y = input$y) +
geom_point()
plot(p)
observeEvent(input$update, print(as.numeric(input$update)))
})
}
如果 绘图对象是您在 renderPlot
函数中执行的最后一件事,您可以省略 plot
:
output$scatterplot <- renderPlot({
ggplot(data = read.csv("eu_pigs.csv")) +
aes_string(x = input$x, y = input$y) +
geom_point()
})
我正在尝试基于已加载的 csv 创建散点图,但是当我 运行 代码时,我要么没有显示任何图,要么在包含 aes 映射时出现错误:"Mapping should be created with aes()
or aes_()
."
任何人都可以指出我哪里出错了吗?
代码:
library(shiny)
library(ggplot2)
ui <- (fluidPage(
titlePanel("Pig Breeds"),
sidebarLayout(
sidebarPanel(
selectInput(inputId = "x",
label = "Pig Breeds:",
choices = c("total_pigs", "female_breeding_herd",
"in_pig_sows", "in_pig_gifts", "other_sows",
"maiden_gilts", "boars_for_service", "other_pigs"),
selected = "total_pigs"),
selectInput(inputId = "y",
label = "Year by year change:",
choices = c(2016, 2017, 2018, "year_on_year_change"),
selected = 2016),
actionButton(inputId = "update", label = "update")
),
mainPanel = (
plotOutput(outputId = "scatterplot")
)
)
)
)
server <- (function(input, output) {
output$scatterplot <- renderPlot({
ggplot(data=(read.csv("eu_pigs.csv")),
aes(x = output$x, y = output$y) +
geom_point())
observeEvent(input$update, {print(as.numeric(input$update))})
}
)
}
)
shinyApp(ui, server)
如错误消息所述,您使用的 aes
不正确。该函数采用 列名称 ,而不是变量引用。即替换
aes(x = output$x, y = output$y)
来自
aes(x = x, y = y)
或者,您更有可能希望能够从输入中控制情节,因此您想使用
aes_string(x = input$x, y = input$y)
您的代码中也有不少括号和大括号。删除那些。此外,mainPanel
是您需要调用的 函数 。您的代码正在为其分配一些内容。
最后,您实际上需要绘制您的情节。这些都搞定后,相关代码如下:
ui <- fluidPage(
titlePanel("Pig Breeds"),
sidebarLayout(
sidebarPanel(…),
mainPanel(
plotOutput(outputId = "scatterplot")
)
)
)
server <- function(input, output) {
output$scatterplot <- renderPlot({
p = ggplot(data = read.csv("eu_pigs.csv")) +
aes_string(x = input$x, y = input$y) +
geom_point()
plot(p)
observeEvent(input$update, print(as.numeric(input$update)))
})
}
如果 绘图对象是您在 renderPlot
函数中执行的最后一件事,您可以省略 plot
:
output$scatterplot <- renderPlot({
ggplot(data = read.csv("eu_pigs.csv")) +
aes_string(x = input$x, y = input$y) +
geom_point()
})