简体   繁体   English

如何从 Shiny 中的过滤数据创建条形图?

[英]How to create barplot from filtered data in Shiny?

In this example it is always the first combination of Type and Period that is used in the barplot.在此示例中,条形图中使用的始终是 Type 和 Period 的第一个组合。 No matter what combination is selected it is always Type A and Period 01 that is plotted.无论选择何种组合,始终绘制类型 A 和周期 01。 What I am doing wrong?我做错了什么? I am guessing that it must be something in the renderDataTable-part in server.我猜它一定是服务器的renderDataTable-part中的东西。

A reproducible example:一个可重现的例子:

packages=c(
  'shiny', 'DT', 'shinyWidgets'
)

for (p in packages){
  if (!require(p, character.only=T)){          
    install.packages(p,dependencies = T)           
  }
  library(p, character.only=T)       
}

data <- expand.grid(c("A", "B", "C", "D"), c("01", "02", "03", "04", "05", "06"))
names(data) <- c("Type", "Period")
data <- data[order(data$Type, data$Period),]
data <- cbind(data,  "1970" = sample(c(1:100), dim(data)[1], rep = T),  "1971" = sample(c(1:100), dim(data)[1], rep = T))

ui <- basicPage(
  h2("Data"),
  pickerInput("Type",
              label=div(HTML("Select type:"),style="color:darkblue"),width="auto",
              choices=c("",unique(as.character(data$Type))),
              selected="",
              multiple=FALSE,
              options = list(
                `actions-box` = TRUE,
                `none-selected-text` = "No type selected",
                `selected-text-format` = paste0("count>",length(c("",unique(as.character(data$Type))))-1)
              ),
              choicesOpt = list(
                style = rep(("color:darkgreen; background: white; font-weight: bold;"),
                            length(c(unique(as.character(data$Type))))+1)
              )
  ),
  pickerInput("Period",
              label=div(HTML("Select period:"),style="color:darkblue"),width="auto",
              choices=c("",unique(as.character(data$Period))),
              selected="",
              multiple=FALSE,
              options = list(
                `actions-box` = TRUE,
                `none-selected-text` = "No period selected",
                `selected-text-format` = paste0("count>",length(c("",unique(as.character(data$Period))))-1)
              ),
              choicesOpt = list(
                style = rep(("color:darkgreen; background: white; font-weight: bold;"),
                            length(c(unique(as.character(data$Period))))+1)
              )
  ),
  DT::dataTableOutput("mydata"),
  plotOutput('plot1')
)

server <- function(input, output, session) {
  
  data <- data
  
  output$mydata = DT::renderDataTable({
    if (input$Type != "All the types") {
      data <- data[data$Type %in%  input$Type,]
    }
    else{data}
    
    if (input$Period != "All the periods") {
      data <- data[data$Period %in%  input$Period,]
    }
    else{data}
  })
  
  filtered_table <- reactive({
    req(input$mydata_rows_all)
    data[input$mydata_rows_all, ]
  })
  
  output$plot1 <- renderPlot({
    barplot(c(t(filtered_table()[,c("1970", "1971")])),
            col = rainbow(2),
            horiz = T,
            names.arg=c("1970", "1971"))
  })
}

shinyApp(ui, server)

Try this code:试试这个代码:

  • Created filtered_table as reactiveValues .创建filtered_table作为reactiveValues
  • Changed the check if(input$Type != "All the types") to if (input$Type != "") since there is no Type value which is "All the types" .将检查if(input$Type != "All the types")更改为if (input$Type != "")因为没有Type值是"All the types"
  • Added some checks before showing the plot.在显示 plot 之前添加了一些检查。
library(shiny)
library(DT)
library(shinyWidgets)

data <- expand.grid(c("A", "B", "C", "D"), c("01", "02", "03", "04", "05", "06"))
names(data) <- c("Type", "Period")
data <- data[order(data$Type, data$Period),]
data <- cbind(data,  "1970" = sample(c(1:100), dim(data)[1], rep = T),  "1971" = sample(c(1:100), dim(data)[1], rep = T))

ui <- basicPage(
  h2("Data"),
  pickerInput("Type",
              label=div(HTML("Select type:"),style="color:darkblue"),width="auto",
              choices=c("",unique(as.character(data$Type))),
              selected="",
              multiple=FALSE,
              options = list(
                `actions-box` = TRUE,
                `none-selected-text` = "No type selected",
                `selected-text-format` = paste0("count>",length(c("",unique(as.character(data$Type))))-1)
              ),
              choicesOpt = list(
                style = rep(("color:darkgreen; background: white; font-weight: bold;"),
                            length(c(unique(as.character(data$Type))))+1)
              )
  ),
  pickerInput("Period",
              label=div(HTML("Select period:"),style="color:darkblue"),width="auto",
              choices=c("",unique(as.character(data$Period))),
              selected="",
              multiple=FALSE,
              options = list(
                `actions-box` = TRUE,
                `none-selected-text` = "No period selected",
                `selected-text-format` = paste0("count>",length(c("",unique(as.character(data$Period))))-1)
              ),
              choicesOpt = list(
                style = rep(("color:darkgreen; background: white; font-weight: bold;"),
                            length(c(unique(as.character(data$Period))))+1)
              )
  ),
  DT::dataTableOutput("mydata"),
  plotOutput('plot1')
)

server <- function(input, output, session) {
  
  filtered_table <- reactiveValues(data = NULL)
  
  output$mydata = DT::renderDataTable({
    if (input$Type != "") {
      data <- data[data$Type %in%  input$Type,]
    }
    else{data}
    
    if (input$Period != "") {
      data <- data[data$Period %in%  input$Period,]
    }
    else{data}
    
    filtered_table$data <- data
  })
  
  
  
  output$plot1 <- renderPlot({
    req(filtered_table$data, input$Type, input$Period)
    barplot(c(t(filtered_table$data[,c("1970", "1971")])),
            col = rainbow(2),
            horiz = T,
            names.arg=c("1970", "1971"))
  })
}

shinyApp(ui, server)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 R Shiny-如何创建根据时间单位(周,月,年)做出反应并按时间单位汇总数据的条形图 - R Shiny - How to create a barplot that reacts according to the time unit (Week, Month, Year) and aggregates data by time unit 从Shiny中的renderDataTable()下载过滤后的数据 - Download filtered data from renderDataTable() in Shiny R:如何从多列频率数据创建条形图? - R: How to create a barplot from a multi-column frequency data? R Shiny 如何在根据用户选择从 mysqlDB 检索数据时使用 renderPlot 构建条形图 - R Shiny How to use renderPlot to build barplot while retrieving data from mysqlDB based on user selection R Shiny - 如何使用函数 dateRangeInput 和 selectInput 创建反应式条形图 - R Shiny - How to create a reactive barplot with functions dateRangeInput and selectInput 以交互方式从R Shiny中的小节检索源数据信息 - Retrieving source data information from a barplot in R shiny interactively 具有Shiny过滤数据的Downloadhandler - Downloadhandler with filtered data in Shiny 如何为闪亮的仪表板创建基于汇总和过滤数据的折线图? - How to create a line chart which is based on aggregated and filtered data for a shiny dashboard? 从 data.frame 创建条形图 - Create barplot from data.frame 如何基于表中的名称创建过滤的data.frame? - How to create filtered data.frames based on names from a table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM