繁体   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