1

我正在尝试从 iris 数据集创建一个基本的闪亮应用程序,代码如下。但是,当我尝试查看生成的图表时,我的所有点都被折叠了,好像两个轴都没有刻度。

# 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()

我想我可以在两个轴上添加一个比例,但是当我查看其他闪亮的应用程序示例时,似乎不需要它来正确显示。闪亮的应用程序缺少什么步骤?

4

1 回答 1

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)

在此处输入图像描述

于 2020-07-31T00:46:13.697 回答