Shiny App:对多个输入有React的散点图

dauxcl2d  于 2023-09-27  发布在  React
关注(0)|答案(1)|浏览(132)

我正在尝试制作一个闪亮的应用程序,允许用户为散点图的x轴和y轴选择一个组(cut)和一个变量(即depthtableprice)。下面的UI代码可以很好地允许用户进行选择,但是,我在服务器上遇到了麻烦。我认为问题在于将UI选择过滤为两个数据框,然后使用rbind()合并它们,但我不知道其他方法。
如何使ggplot()响应用户对x和y轴的两个选择?
范例:

  1. library(shiny)
  2. library(tidyverse)
  3. dat <- diamonds %>%
  4. select(cut,depth,table,price) %>%
  5. pivot_longer(cols = c('depth','table','price'),
  6. names_to = 'Variable',
  7. values_to = 'Value')
  8. ui <- fluidPage(
  9. selectInput("x_cut", "X-axis Cut", choices = unique(dat$cut)),
  10. selectInput("x_variable", "X-axis Variable", choices = unique(dat$Variable)),
  11. selectInput("y_cut", "Y-axis Cut", choices = unique(dat$cut)),
  12. selectInput("y_variable", "Y-axis Variable", choices = unique(dat$Variable)),
  13. plotOutput("plot")
  14. )
  15. server <- function(input, output) {
  16. output$plot <- renderPlot({
  17. filtered_xdat <- dat %>%
  18. filter(cut == input$x_cut, Variable == input$x_variable)
  19. filtered_ydat <- dat %>%
  20. filter(cut == input$y_cut, Variable == input$y_variable)
  21. filtered_dat <- rbind(filtered_xdat, filtered_ydat)
  22. filtered_dat %>%
  23. ggplot() +
  24. geom_point(aes(x = input$x_variable,
  25. y = input$y_variable)) +
  26. geom_point()
  27. })
  28. }
  29. shinyApp(ui, server)
  30. # Warning: Error in geom_point: Problem while setting up geom.
  31. # ℹ Error occurred in the 2nd layer.
  32. # Caused by error in `compute_geom_1()`:
  33. # ! `geom_point()` requires the following missing aesthetics: x and y

我试过这个:

  1. server <- function(input, output) {
  2. output$plot <- renderPlot({
  3. filtered_xdat <- dat %>%
  4. filter(cut == input$x_cut,
  5. xVariable == input$x_variable) %>%
  6. as.data.frame()
  7. filtered_ydat <- dat %>%
  8. filter(cut == input$y_cut,
  9. yVariable == input$y_variable) %>%
  10. as.data.frame()
  11. ggplot() +
  12. geom_point(aes(x = filtered_xdat$xVariable, y = filtered_ydat$yVariable)) +
  13. theme_minimal()
  14. })
  15. }
  16. # Warning: Error in filter: ℹ In argument: `xVariable == input$x_variable`.
  17. # Caused by error:
  18. # ! object 'xVariable' not found

还有这个

  1. server <- function(input, output) {
  2. output$plot <- renderPlot({
  3. filtered_dat <- dat %>%
  4. filter(cut %in% c(input$x_cut, input$y_cut),
  5. Variable %in% c(input$x_variable, input$y_variable))
  6. ggplot(data = filtered_dat, aes(x = !!sym(input$x_variable), y = !!sym(input$y_variable))) +
  7. geom_point() +
  8. theme_minimal()
  9. })
  10. }
  11. # Warning: Error in geom_point: Problem while computing aesthetics.
  12. # ℹ Error occurred in the 1st layer.
  13. # Caused by error in `FUN()`:
  14. # ! object 'depth' not found
u0sqgete

u0sqgete1#

rbind可能不是最好的选择。虽然cbind应该正常工作,但在您的示例中,x和y变量之间没有关系。所以,我只是cbind一个新的函数cbindPad,允许不同的行数。在您的真实的用例中,您可能只需要使用cbind。试试这个

  1. library(shiny)
  2. library(tidyverse)
  3. ######### cbind datasets with different number of rows ######
  4. cbindPad <- function(...){
  5. args <- list(...)
  6. n <- sapply(args,nrow)
  7. mx <- max(n)
  8. pad <- function(x, mx){
  9. if (nrow(x) < mx){
  10. nms <- colnames(x)
  11. padTemp <- matrix(NA, mx - nrow(x), ncol(x))
  12. colnames(padTemp) <- nms
  13. if (ncol(x)==0) {
  14. return(padTemp)
  15. } else {
  16. return(rbind(x,padTemp))
  17. }
  18. }
  19. else{
  20. return(x)
  21. }
  22. }
  23. rs <- lapply(args,pad,mx)
  24. return(do.call(cbind,rs))
  25. }
  26. dat <- diamonds %>%
  27. select(cut,depth,table,price) %>%
  28. pivot_longer(cols = c('depth','table','price'),
  29. names_to = 'Variable',
  30. values_to = 'Value')
  31. ui <- fluidPage(
  32. selectInput("x_cut", "X-axis Cut", choices = unique(dat$cut)),
  33. selectInput("x_variable", "X-axis Variable", choices = unique(dat$Variable)),
  34. selectInput("y_cut", "Y-axis Cut", choices = unique(dat$cut)),
  35. selectInput("y_variable", "Y-axis Variable", choices = unique(dat$Variable)),
  36. plotOutput("plot")
  37. )
  38. server <- function(input, output) {
  39. output$plot <- renderPlot({
  40. filtered_xdat <- dat %>%
  41. filter(cut == input$x_cut, Variable == input$x_variable) %>%
  42. rename(cutx=cut, Variablex=Variable, Valuex = Value)
  43. filtered_ydat <- dat %>%
  44. filter(cut == input$y_cut, Variable == input$y_variable)
  45. filtered_dat <- cbindPad(filtered_xdat, filtered_ydat)
  46. filtered_dat %>%
  47. ggplot() +
  48. geom_point(aes(x = Valuex,
  49. y = Value )) # +
  50. # geom_point()
  51. })
  52. }
  53. shinyApp(ui, server)
展开查看全部

相关问题