【问题标题】:Error "arguments imply differing number of rows" when subsetting data with Shiny/ggplot2使用 Shiny/ggplot2 对数据进行子集化时出现错误“参数暗示行数不同”
【发布时间】:2015-09-07 00:15:39
【问题描述】:

恐怕我被卡住了。

我有一个简单的 Shiny 脚本,目的是根据用户输入对数据框进行子集化,并在散点图中绘制两个变量。运行脚本时,我总是收到错误“data.frame 中的错误(x = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, :参数暗示不同的行数: 1786, 2731”。我所知道的是,当数据是 n_col!=n_row 在数据帧中时会发生此错误。但是,我不明白这可能是这里的问题。如果我执行下面的 sn-p,我会感到困惑,情节绘制没有问题:

#test4 <- subset(test2, grepl("PLANT1", test2$PLANTS))
#ggplot(test4, aes(x=test4$HOUR, y=test4$PRICE_NO)) +
     geom_point(shape=1)

我所做的只是用 ui.r 中的 input$plant 替换字符串。

这是我的主窗口代码:

###################################
# Launch  App
###################################
#install.packages("shiny")
#install.packages("ggplot2")
library(shiny)
library(ggplot2)

#load data
#data <- read.csv2(file="C:/data.csv",head=FALSE)
#test4 <- subset(test2, grepl("PLANT1", test2$PLANTS))
#ggplot(test4, aes(x=test4$HOUR, y=test4$PRICE_NO)) +
     geom_point(shape=1)

runApp("C:/PATH/")

我的服务器.r

library(shiny)
library(ggplot2)

# Define Input to Plot
shinyServer(function(input, output) {

output$distPlot <- renderPlot({
# Draw Plot
test4 <- subset(test2, grepl(input$plant, test2$PLANTS))
ggplot(test4, aes(x=test4$HOUR, y=test4$PRICE_NO)) +
  geom_point(shape=1)
})
})

我的 ui.r

library(shiny)

# Title
shinyUI(fluidPage(

titlePanel("TITLE"),

#Sidebar Layout
sidebarLayout(
 sidebarPanel(
  textInput("plant",
              label = h3("Plant:"),
              value = "PLANT1")
  ),

#
mainPanel(
  plotOutput("distPlot")
  )
)

))

按要求提供样本数据:

测试2

plants HOUR PRICE

plant1 1    12,45
plant1 2    15,52
plant1 3    15,45
plant1 4    78,12
plant1 5    72,12
plant2 1    78,72
plant2 2    72,52
plant2 3    75,52 
plant2 4    78,11

【问题讨论】:

  • 请提供样本数据,以便我们重现您的错误。另外,避免使用subset,原因提到here
  • 提供样本数据并用 test4
  • 请检查我的回答。您在这里没有正确使用grepl(至少对于您的示例数据)。

标签: r ggplot2 shiny


【解决方案1】:

根据我在评论中提到的使用subset的情况,您可以进行如下操作(这里不需要使用grepl

test4 <- subset(test2, test2$plants==input$plant)
    ggplot(test4, aes(x=HOUR, y=PRICE)) +
      geom_point(shape=1)

ui。回复

library(shiny)

# Title
shinyUI(fluidPage(

  titlePanel("TITLE"),

  #Sidebar Layout
  sidebarLayout(
    sidebarPanel(
      selectInput("plant",
                label = h3("Plant:"),
                choices = c("plant1","plant2"),
                selected="plant1")
    ),

    #
    mainPanel(
      plotOutput("distPlot")
    )
  )
))

服务器.R

library(shiny)
library(ggplot2)

test2<-readRDS("data\\test2.rds")

# Define Input to Plot

shinyServer(function(input, output) {

  output$distPlot <- renderPlot({
    # Draw Plot
    test4 <- subset(test2, test2$plants==input$plant)
    ggplot(test4, aes(x=HOUR, y=PRICE)) +
      geom_point(shape=1)
  })
})

应用内数据文件夹中的示例数据:

test2<-structure(list(plants = structure(c(1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L), .Label = c("plant1", "plant2"), class = "factor"), HOUR = c(1L, 
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L), PRICE = structure(c(1L, 3L, 
2L, 8L, 4L, 9L, 5L, 6L, 7L), .Label = c("12,45", "15,45", "15,52", 
"72,12", "72,52", "75,52", "78,11", "78,12", "78,72"), class = "factor")), .Names = c("plants", 
"HOUR", "PRICE"), class = "data.frame", row.names = c(NA, -9L
))

【讨论】:

    猜你喜欢
    • 2021-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-29
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    相关资源
    最近更新 更多