簡體   English   中英

閃亮的分類數據可視化問題

[英]categorical data visualization problem in shiny

我想可視化數據類別,但我無法通過下面的代碼得到我想要的。 當我想將數據分類時,它會將其作為一個整體進行評估。 我無法對圖表進行分類。 我究竟做錯了什么?

library(shiny)
library(ggplot2)

ui <- fluidPage(

titlePanel("shiny demo"),

 sidebarLayout(

   sidebarPanel(
  
  fileInput(inputId = "file",
            label = "choose file",
            multiple = F),
  
  selectInput("p","variable1",
              choices = c("sex","smoke","happines","city","work")),
  
  selectInput("q", "variable2",
              choices = c("hopefulnes","happines")))
,
mainPanel(
  tabsetPanel(
    
    tabPanel(title = "Data",tableOutput("data_out")),
    
    tabPanel(title = "Bar",plotOutput("barp"))
    
      )
    )
  )
)
server <- function(input,output,session) {

 data <- reactive({
 req(input$file)

 df=read.csv(input$file$datapath,sep = ";",header = T)

 })
 output$data_out <- renderTable({
 data() 
 })
  
 output$barp <- renderPlot({      
 ggplot(data(), aes(input$p, ..count..)) + geom_bar(aes(fill = input$q), position = "dodge")  
 })

}
shinyApp(ui,server)

數據;

id  sex   smoke happines     hopefulness   work city
1   man   yes   very happy   very hopeful   yes   A
2   man   no    very happy   very hopeful   yes   A
3   man   no    unstable     not hopeful    no    C
4   woman no    unstable     not hopeful    yes   A
5   woman no    unstable     not hopeful    yes   B
6   man   yes   very happy   hopeful        yes   C
7   woman yes   happy        unstable       no    D
8   man   yes   not happy    not hopeful    yes   A
9   woman no    not happy    unstable       yes   B
10  man   no    very happy   very hopeful   yes   D

錯誤代碼;

asJSON(keep_vec_names=TRUE) 的輸入是一個命名向量。 在未來的 jsonlite 版本中,將不支持此選項,命名向量將被轉換為數組而不是對象。 如果您想要 JSON 對象輸出,請改用命名列表。 見?toJSON。

感謝幫助

selectInput()被設置為輸出一個字符串,而aes()被設置為期望一個命名列。 如果您從input$p選擇 "smoke" 並從input$q選擇 "happiness" ,這將作為"smoke""happiness" input$q函數,而不是smokehappiness 因此,它將按照您發送此函數的方式進行繪制:

ggplot(df, aes("smoke", ..count..)) +
  geom_bar(aes(fill = "happiness"), position = "dodge")

在此處輸入圖片說明

要處理selectInput()的字符串輸出,您應該使用aes_string()代替aes() 您需要將..count..更改為"..count.." ,但它會像您要求評估以下代碼塊一樣工作:

ggplot(df, aes_string("smoke", "..count..")) +
  geom_bar(aes_string(fill = "happiness"), position = "dodge")

在此處輸入圖片說明

另一種方法是使用aes()進行維護,但只需將input$pinput$q的字符串作為變量名進行評估。 您可以使用get()正常執行此操作,因此我相信它也適用於您的應用程序。 在這種情況下,您不必將..count..更改為"..count.."

# yields the same plot as above
ggplot(df, aes(get("smoke"), ..count..)) +
  geom_bar(aes(fill = get("happiness")), position = "dodge")

暫無
暫無

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

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