如何从动态输入创建绘图?

问题描述 投票:0回答:1

我是闪亮编程的新手。我目前正在开发一个应用程序,用户可以在其中上传数据,然后选择应绘制哪些变量。在这里,我遇到了 ggplot 仅返回空图的问题。这不仅适用于我的特定数据集,而且当我使用

iris
数据集时,我也会遇到相同的错误。

这是我的 App.R 文档:

library(shiny)
library(ggplot2)
library(tidyverse)

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("Plot Data"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
                    selectInput(inputId = "x_input",
                      choices = "",
                      label = "Choose x-Axis",
                      multiple = FALSE,
                      selectize = TRUE),
          selectInput(inputId = "y_input",
                      choices = "",
                      label = "Choose y-Axis",
                      multiple = FALSE,
                      selectize = TRUE)
        ),
        # Show a plot of the generated distribution
        mainPanel("Sample of Input Data",
           
           tableOutput("table"),
           "Plot of Input Data",
           plotOutput("plot")

        )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output, session) {

  output$table = renderTable({
    #req(combined_df)
    return(head(iris))
  })
  
  observe({
    updateSelectInput(
      session,
      "y_input",
      choices=names(iris))
  })
  
  observe({
    updateSelectInput(
      session,
      "x_input",
      choices=names(iris))
  })
  
  output$plot = renderPlot({
    iris %>% 
      plot()
   # plot = iris %>% 
   #         ggplot(aes(x = input$x_input, y = input$y_input))
   # print(plot)

    })   

  
}
# Run the application 
shinyApp(ui = ui, server = server)

关于我如何制定

output$plot
命令,我遇到了不同的错误/问题。

  1. 使用

    base
    R 与
    plot()

    output$plot = renderPlot({
        iris %>% 
          plot(x = input$x_input, y = input$y_input)
    
        })   
    

这会引发以下错误:

Warning: Error in plot.window: need finite 'xlim' values
  173: plot.window
  172: localWindow
  171: plot.default
  170: plot
  169: %>%
  168: renderPlot [directory....]
  166: func
  126: drawPlot
  112: <reactive:plotObj>
   96: drawReactive
   83: renderFunc
   82: output$plot
    1: shiny::runApp
  1. 如果我不使用
    base
    R 指定 x 和 y 值,我确实会得到一个绘图输出:
output$plot = renderPlot({
    iris %>% 
      plot()
})  

请参阅此处。这也不能令人满意,因为我只想要一个 xy 图。

  1. 使用
    ggplot()
output$plot = renderPlot({
  plot = iris %>% 
          ggplot(aes(x = input$x_input, y = input$y_input))
})

我得到一个没有任何数据点的空图。如果我将

ggplot()
函数包装在
print()
show()
语句中或根本没有,这将保持不变。

  1. 最后一个组合是数字三的变体,也使用
    ggplot()
    :
output$plot = renderPlot({
  plot = iris %>% 
          ggplot(aes(x = input$x_input, y = input$y_input))+
           geom_point()
})

这将返回一个空图,中间有一个点(请参阅此处)。

重要注意事项:无论是否使用

tidyverse
包/符号,这些错误都保持不变。

此时我已经没有办法解决这个看似简单的问题,所以任何帮助将不胜感激。谢谢!

r ggplot2 shiny
1个回答
0
投票

有两件事:

  1. 我们需要将
    input$
    包裹在
    .data[[]]
    中(或者包裹在
    !! sym()
    中)。
  2. 光打电话
    ggplot
    是不够的,我们还需要
    geom_point()
library(shiny)
library(ggplot2)
library(tidyverse)

# Define UI for application that draws a histogram
ui <- fluidPage(
  
  # Application title
  titlePanel("Plot Data"),
  
  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
      selectInput(inputId = "x_input",
                  choices = "",
                  label = "Choose x-Axis",
                  multiple = FALSE,
                  selectize = TRUE),
      selectInput(inputId = "y_input",
                  choices = "",
                  label = "Choose y-Axis",
                  multiple = FALSE,
                  selectize = TRUE)
    ),
    # Show a plot of the generated distribution
    mainPanel("Sample of Input Data",
              
              tableOutput("table"),
              "Plot of Input Data",
              plotOutput("plot")
              
    )
  )
)

# Define server logic required to draw a histogram
server <- function(input, output, session) {
  
  output$table = renderTable({
    #req(combined_df)
    return(head(iris))
  })
  
  observe({
    updateSelectInput(
      session,
      "y_input",
      choices=names(iris))
  })
  
  observe({
    updateSelectInput(
      session,
      "x_input",
      choices=names(iris))
  })
  
  output$plot = renderPlot({
    iris %>%
      ggplot(aes(x = .data[[input$x_input]],
                 y = .data[[input$y_input]])) + 
      geom_point()
    
    
  })   
  
  
}
# Run the application 
shinyApp(ui = ui, server = server)
© www.soinside.com 2019 - 2024. All rights reserved.