簡體   English   中英

R Shiny 密度圖

[英]R Shiny Density Plots

我正在做一個項目,我們必須創建一個顯示數據的 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)

我無權訪問您的數據,但我知道是什么導致您的 plot 不出現在您的代碼中。 簡而言之,您的 plot 代碼永遠不會運行,這就是為什么您什么也看不到的原因……即使它確實運行了,您也可能會收到所寫的錯誤。

為什么什么都看不到?

您的server代碼結構如下:

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

    output$densityplot <- renderPlot({...})
  })

ui僅命名一個 output function,即output$densityplot 這部分永遠不會運行,因為它在output$plot的內部,在 UI 的任何地方都找不到引用。 這就是為什么你什么也看不見。 output$plot中的所有代碼將永遠不會運行,因為在ui中沒有以該名稱引用的 output object。

如果您將output$densityplot拉到 output$ output$plot之外,它運行,但您肯定會遇到錯誤,因為您參考了 x 軸美學aes(output$var) 您引用了output object,它位於server function 內部,因此不在ui中。

你如何解決這個問題?

老實說,專門解決這個問題有點過分,而且您還沒有共享數據。 我可以向您展示我將如何處理您的一般問題的工作示例。 也就是說,您似乎希望根據用戶選擇的 UI 元素更改或過濾 plot 中顯示的數據。

您可以使用幾種方法,但我喜歡使用的一般想法是創建一個反應式 function 輸出將用於繪圖的數據幀。 Inside the reactive function, I'd put all the elements I may want to use for filtering a dataset, and then the returned data frame from the reactive function is used in the renderPlot() function associated with the plotOutput() object named in the ui

在以下示例中,我正在創建一個應用程序來為diamonds數據集創建密度曲線。 我在 x 軸上給 select 一個下拉菜單,然后是其他 3 個允許過濾數據集的下拉菜單。 我還添加了一些邏輯和功能,以允許當用戶在其中選擇“全部”時不過濾數據集(默認)。

這里有一些最后的注釋:我使用aes_string(input$s_axis)代替aes(...) 原因是從input$s_axis得到的output會是字符類型,而不是列名。 您使用get()aes() ,但aes_string()在這里更簡單一些並且工作正常。 其次,我經常發現在renderPlot()中顯式地print() plot 通常會更好,盡管這不是嚴格要求的。

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)

用它作為你的項目的一些靈感和好運。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM