【问题标题】:R Shiny Density PlotsR闪亮密度图
【发布时间】:2021-04-21 18:56:00
【问题描述】:

我正在做一个项目,我们必须创建一个显示数据的 R Shiny 应用程序。我有四个定量变量(个人、社交、体重和饮食)。我希望在应用程序中指定时,在学校的每一年和每种性别都有 4 个密度图相互叠加。密度图不会出现,无论我做什么,它们都不会出现。任何建议都会很有帮助!

library(shiny)
library(ggplot2)
library(purrr)
library(dplyr)

#plotting theme for ggplot2
.theme<- theme(
  axis.line = element_line(colour = 'gray', size = .75),
  panel.background = element_blank(),
  plot.background = element_blank()
)


# UI for app
ui<-(pageWithSidebar(
  # title
  headerPanel("Select Options for Body Image and Disordered Eating Among UW-Madison Students"),

  #input
  sidebarPanel
  ( # Input: Select what to display
  selectInput("gender", "Gender:",
                  choices = c("Female", 
                            "Male",
                            "Nonbinary", 
                            "Do not care to say"),
                  selected = "Female"),
    selectInput("school", "Year in School:",
                  choices = c("First Year", 
                            "Second Year",
                            "Third Year",
                            "Fourth Year"),
                  selected = "First Year")),
  mainPanel(plotOutput("densityplot"))
  )
)


# --------------------------------------------SERVER---------------------------------------------
server<-(function(input, output){
  output$plot <- renderUI({
    plotOutput("p")
    
    data = switch(input$gender,
                  "Female"="Female", 
                  "Male"= "Male",
                  "Nonbinary"= "Nonbinary", 
                  "Do not care to say" = "Do not care to say")  
    data = switch(input$school,
                  "First Year"="First Year", 
                  "Second Year"= "Second Year",
                  "Third Year"= "Third Year", 
                  "Fourth Year" = "Fourth Year")    
    data = switch(output$var,
                  "Personal"= cleandatafull$personal, 
                  "Social"= cleandatafull$social,
                  "Weight"= cleandatafull$weight, 
                  "Eating" = cleandatafull$eating)
    
     output$densityplot<-renderPlot({
      
      ggplot(cleandatafull, aes(x=output$var)) +
        geom_density(adjust=1.5, alpha=.4)  })
 # There is something going wrong here with the density plot and I just want something to show up so I can fix it to look how I want it to.
     
  })
  })
  


shinyApp(ui, server)

【问题讨论】:

    标签: r ggplot2 shiny density-plot


    【解决方案1】:

    我无权访问您的数据,但我知道是什么导致您的绘图没有出现在您的代码中。简而言之,你的情节代码永远不会运行,这就是为什么你什么都看不到的原因......即使它确实运行了,你也可能会得到一个错误。

    为什么你什么也看不见?

    你的server代码结构如下:

    server <-...
      output$plot <- renderUI({...
        plotOutput("p")
    
        output$densityplot <- renderPlot({...})
      })
    

    ui 仅命名一个输出函数,即output$densityplot。这部分永远不会运行,因为它位于output$plot 的内部,在 UI 的任何地方都找不到它的引用。这就是为什么你什么也看不见。 output$plot 中的所有代码将永远不会运行,因为在 ui 中没有以该名称引用的输出对象。

    如果你将output$densityplot 拉到output$plot 之外,它运行,但你肯定会遇到错误,正如你参考x 轴美学aes(output$var) 一样。您引用了一个 output 对象,该对象位于 server 函数内部,因此不在 ui 中。

    你如何解决这个问题?

    老实说,专门解决这个问题有点过分,而且您还没有共享数据。我可以向您展示我将如何处理您的一般问题的工作示例。也就是说,您似乎希望根据用户选择的 UI 元素更改或过滤图中显示的数据。

    您可以使用几种方法,但我喜欢使用的一般想法是创建一个反应函数,该函数输出将用于绘图的数据框。在响应式函数中,我将我可能想要用于过滤数据集的所有元素放入,然后从响应式函数返回的数据帧用于与plotOutput() 中命名的对象关联的renderPlot() 函数ui.

    在以下示例中,我正在创建一个应用程序来为 diamonds 数据集创建密度曲线。我给出了一个下拉菜单来选择 x 轴,然后是其他 3 个用于过滤数据集的下拉菜单。我还添加了一些逻辑和功能,以允许当用户在其中选择“全部”时不过滤数据集(默认)。

    这里有一些最后的说明:我使用aes_string(input$s_axis) 代替aes(...)。原因是从input$s_axis 获得的输出将是字符类型,而不是列名。您使用get()aes(),但aes_string() 在这里更简单一些,并且工作正常。其次,我经常发现在renderPlot() 中明确print() 的情节通常会更好,尽管这不是严格要求的。

    library(shiny)
    library(ggplot2)
    library(dplyr)
    library(tidyr)
    
    
    ui <- pageWithSidebar(
      headerPanel('Example of Filtering a Plot'),
        sidebarPanel(
          h3("Filters"),
          selectInput("s_color", label="Color:", choices = c("All", as.character(unique(diamonds$color)))),
          selectInput("s_cut", label="Cut:", choices=c("All", as.character(unique(diamonds$cut)))),
          selectInput("s_clarity", label="Clarity:", choices=c("All", as.character(unique(diamonds$clarity)))),
          br(),
          selectInput("s_axis", label="X Axis:", choices=c("carat","price", "depth", "table"))
        ),
        mainPanel(plotOutput('dazzle'))
    )
    
    server <- function(input, output) {
      
      glittering_diamonds <- reactive({
        d <- diamonds
        if(input$s_color != "All")
          d <- d %>% dplyr::filter(color==input$s_color)
        if(input$s_cut != "All")
          d <- d %>% dplyr::filter(cut==input$s_cut)
        if(input$s_clarity != "All")
          d <- d %>% dplyr::filter(clarity==input$s_clarity)
        return(d)
      })
        
      output$dazzle <- renderPlot({
        p <- ggplot(glittering_diamonds(), aes_string(x=input$s_axis)) +
          geom_density(fill='blue', alpha=0.2) +
          theme_classic()
        print(p)
      })
    }
    
    shinyApp(ui = ui, server = server)
    

    用它作为你的项目的一些灵感和好运。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-14
      • 2015-01-11
      • 2019-03-07
      • 2017-04-04
      • 2019-01-04
      • 2021-06-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多