簡體   English   中英

總結閃亮的SelectInput的字符和數字列

[英]Summarising Character and Numeric Columns for Shiny SelectInput

下面是數據框的結構

Village <- c("Location A" , "Location B", "Location C", "Location C", "Location A")
Farmers_Name <- c("Mary", "John", "Grace","Steph", "Richard")
Practiced_MinimumTillage <- c(0,1,1,0,1)
Practiced_Intercropping <- c(1,1,1,0,0)
Practiced_CropRotation <- c(1,1,1,1,0)
Practiced_ApplicationOfManure <- c(0,1,0,1,0)
farmers <- data.frame(Farmers_Name,Village,Practiced_MinimumTillage,Practiced_Intercropping,Practiced_CropRotation,Practiced_ApplicationOfManure)

dataframe農民的輸出。

 Farmers_Name    Village Practiced_MinimumTillage Practiced_Intercropping Practiced_CropRotation Practiced_ApplicationOfManure
1         Mary Location A                        0                       1                      1                             0
2         John Location B                        1                       1                      1                             1
3        Grace Location C                        1                       1                      1                             0
4        Steph Location C                        0                       0                      1                             1
5      Richard Location A                        1                       0                      0                             0

總結農場實踐以了解用法。 農民在其農場中使用的做法的頻率表。

 practices <-  select(farmers,Practiced_MinimumTillage,Practiced_Intercropping,Practiced_CropRotation,Practiced_ApplicationOfManure)
practices %>%
  summarise_all(sum, na.rm=TRUE) %>%
  gather(var,value) %>%
  arrange(desc(value)) %>%
  ggplot(aes(var, value)) + geom_bar(stat = "Identity") + coord_flip()

farmers數據框中,我想將列“村庄”用於selectInput函數。 由此,如果有人從下拉菜單中選擇“位置A”或“位置B”,則基於頻率表的上圖將呈現在輸出中。 如何使用dplyrdata.table重組數據dplyr以適合此data.table

這很簡單,但是如果您有任何問題請發表評論-

Village <- c("Location A" , "Location B", "Location C", "Location C", "Location A")
Farmers_Name <- c("Mary", "John", "Grace","Steph", "Richard")
Practiced_MinimumTillage <- c(0,1,1,0,1)
Practiced_Intercropping <- c(1,1,1,0,0)
Practiced_CropRotation <- c(1,1,1,1,0)
Practiced_ApplicationOfManure <- c(0,1,0,1,0)
farmers <- data.frame(Farmers_Name,Village,Practiced_MinimumTillage,Practiced_Intercropping,
                      Practiced_CropRotation,Practiced_ApplicationOfManure)

shinyApp(
  ui = fluidPage(
    selectInput("village", "Select Village", choices = unique(farmers$Village)),
    plotOutput("some_plot")
  ),
  server = function(input, output, session) {

    output$some_plot <- renderPlot({
      filter(farmers, Village == input$village) %>%
      select(Practiced_MinimumTillage,Practiced_Intercropping,Practiced_CropRotation,
             Practiced_ApplicationOfManure) %>%
      summarise_all(sum, na.rm=TRUE) %>%
      gather(var,value) %>%
      arrange(desc(value)) %>%
      ggplot(aes(var, value)) + geom_bar(stat = "Identity") + coord_flip()
    })
  }
)

暫無
暫無

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

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