我正在尝试从虹膜数据集创建一个基本的闪亮应用程序,下面的代码。但是,当我尝试查看生成的图形时,我所有的点都被折叠了,好像两个轴都没有刻度一样。
# Load libraries
library(ggplot2)
library(shiny)
library(dplyr)
# Load dataset locally
df <- iris
# Define UI for application
ui <- fluidPage(# Application title
titlePanel("Shiny Attempt"),
sidebarLayout(
# sidebar
sidebarPanel(
# Graph 1 input
checkboxGroupInput(
inputId = "x",
label = "X axis",
choices = c("Sepal Length" = "Sepal.Length",
"Sepal Width" = "Sepal.Width"),
selected = "Sepal.Width"
),
checkboxGroupInput(
inputId = "y",
label = "Y axis",
choices = c("Petal Length" = "Petal.Length",
"Petal Width" = "Petal.Width"),
selected = "Petal.Width"
)
),
# main panel
mainPanel(
# Graph 1 output
plotOutput(outputId = "graph_1"))
))
# Define server logic required to draw plot
server <- function(input, output) {
output$graph_1 <- renderPlot({
# plot inputs
# draw the visualization
ggplot(df, aes(
x = input$x,
y = input$y,
color = Species
)) +
geom_point()
})
}
# Run the application
shinyApp(ui = ui, server = server)
如果我将ggplot部分与闪亮的应用分开运行,则图形将正确显示。
ggplot(iris, aes(
x = Sepal.Width,
y = Petal.Width,
color = Species
)) +
geom_point()
我想我可以在两个轴上都添加一个比例,但是当我看其他闪亮的应用示例时,似乎不需要正确显示它。闪亮的应用程序让我错过了哪一步?
答案 0 :(得分:1)
尝试一下,您必须使用aes_string()
,因为您的值是字符串:
# Load libraries
library(ggplot2)
library(shiny)
library(dplyr)
# Load dataset locally
df <- iris
# Define UI for application
ui <- fluidPage(# Application title
titlePanel("Shiny Attempt"),
sidebarLayout(
# sidebar
sidebarPanel(
# Graph 1 input
checkboxGroupInput(
inputId = "x",
label = "X axis",
choices = c("Sepal Length" = "Sepal.Length",
"Sepal Width" = "Sepal.Width"),
selected = "Sepal.Width"
),
checkboxGroupInput(
inputId = "y",
label = "Y axis",
choices = c("Petal Length" = "Petal.Length",
"Petal Width" = "Petal.Width"),
selected = "Petal.Width"
)
),
# main panel
mainPanel(
# Graph 1 output
plotOutput(outputId = "graph_1"))
))
# Define server logic required to draw plot
server <- function(input, output) {
output$graph_1 <- renderPlot({
# plot inputs
# draw the visualization
ggplot(df, aes_string(
x = input$x,
y = input$y,
color = 'Species'
)) +
geom_point()
})
}
# Run the application
shinyApp(ui = ui, server = server)