Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 使用shiny创建ggplot条形图时缺少条形图_R_Ggplot2_Shiny_Bar Chart - Fatal编程技术网

R 使用shiny创建ggplot条形图时缺少条形图

R 使用shiny创建ggplot条形图时缺少条形图,r,ggplot2,shiny,bar-chart,R,Ggplot2,Shiny,Bar Chart,我使用shiny并创建了一个app.R文件,希望用ggplot构建一个条形图。我还使用了checkboxGroupInput创建了两个复选框来控制条件。虽然选中所有框后,条的总数应为28,但出于某种原因,最大值似乎只允许17条。因此,缺少一些条(数据行)。缺少的酒吧似乎没有一个模式。有人能帮忙吗 数据集: 我的代码: midterm <- read.csv('midterm-results.csv') library(dplyr) library(tidyr) # get column

我使用shiny并创建了一个
app.R
文件,希望用
ggplot
构建一个条形图。我还使用了
checkboxGroupInput
创建了两个复选框来控制条件。虽然选中所有框后,条的总数应为28,但出于某种原因,最大值似乎只允许17条。因此,缺少一些条(数据行)。缺少的酒吧似乎没有一个模式。有人能帮忙吗

数据集:

我的代码:

midterm <- read.csv('midterm-results.csv')

library(dplyr)
library(tidyr)

# get column number for response time
k <- c(33:88)
v <- c()

for (i in k){
        if (i%%2 == 1){
                v <- c(v,i)
        }
}


#average response time by question
time <- midterm[ , v]
new.col.name <- gsub('_.*', "", colnames(time))
colnames(time) <- new.col.name
avg.time <- data.frame(apply(time, 2, mean))
avg.time$question <- rownames(avg.time)
colnames(avg.time) <- c('response_time', 'question')
rownames(avg.time) <- NULL
avg.time$question <- factor(avg.time$question, 
                            levels = c('Q1','Q2','Q3','Q4','Q5','Q6','Q7','Q8.9',
                                       'Q10','Q11','Q12.13','Q14','Q15','Q16','Q17',
                                       'Q18','Q19','Q20','Q21','Q22','Q23','Q24','Q25',
                                       'Q26','Q27','Q28','Q29','Q30'))

avg.time$question_type <-  c(1,0,1,0,1,0,1,1,1,1,1,0,1,1,1,1,0,1,1,1,0,0,0,0,1,1,0,0)
# I did this manually because the there when data was imported into the  midterm.csv,
# q8 & 9, q12 &13 were accidentally merged (28 v.s 30 question)

avg.time$question_type <- ifelse(avg.time$question_type == 1,
                                 'googleable', 'not googleable')
avg.time$question_type <- factor(avg.time$question_type,
                                 levels = c('googleable', 'not googleable'))


library(shiny)
library(ggplot2)


ui <- fluidPage(
    checkboxGroupInput(inputId = "type",
                       label = "select question type", 
                       choices = levels(avg.time$question_type),
                       selected = TRUE), 
    plotOutput('bar')
    )

server <- function(input, output) {
    output$bar <- renderPlot({
         ggplot(avg.time[avg.time$question_type==input$type, ], 
                aes(x=question, response_time)) +
             geom_bar(aes(fill = question_type), stat='identity', width = 0.5)
    }, height =500, width = 1000)
}

shinyApp(ui = ui, server = server)
期中
库(闪亮)
图书馆(GG2)

非常感谢你。问题解决了。但我想知道为什么%
中的
%和e
==
会产生如此大的差异?他们让R做什么不同的事情?我也是R Loool的新手,但我认为
==
期望并知道如何与单个元素进行比较。另一方面,
%In%
期望并知道如何对单个元素和一组“多”元素进行比较。将
观察(打印(输入$type))
放入服务器功能中,并检查控制台中打印的内容。“我希望你明白了我的想法,现在对你来说已经很清楚了。”爱德华·沃德林很高兴帮助你。如果你觉得有帮助,请接受我的回答。非常感谢。
library(shiny)
library(ggplot2)


ui <- fluidPage(
     checkboxGroupInput(inputId = "type", label = "select question type", 
                 choices = levels(avg.time$question_type), selected = TRUE), 
     plotOutput('bar')
)

server <- function(input, output) {

  data <- reactive(avg.time[avg.time$question_type %in% input$type, ])
  output$bar <- renderPlot({
  ggplot(data(), 
       aes(x=question, response_time)) + geom_bar(stat='identity', width = 0.5, 
                                                  aes(fill = question_type))

 }, height =500, width = 1000)
}

shinyApp(ui = ui, server = server)