簡體   English   中英

使用Shiny / ggplot2設置數據子集時出現錯誤“參數表示行數不同”

[英]Error “arguments imply differing number of rows” when subsetting data with Shiny/ggplot2

恐怕我被卡住了。

我有一個簡單的Shiny腳本,旨在根據用戶輸入設置數據框並在散點圖中繪制兩個變量。 運行腳本時,我總是收到錯誤“ data.frame(x = c(1L,2L,3L,4L,5L,6L,7L,8L,9L,10L,11L, 1786,2731“。我所知道的是,當數據為數據幀中的n_col!= n_row時,會發生此錯誤。但是,我在這里看不到這是個問題。令我困惑的是,如果我執行以下代碼段,繪制沒有問題的情節:

#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/")

我的服務器

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)
})
})

我的用戶界面

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條件為條件,您可以按以下步驟進行操作(此處無需使用grepl

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

ui。 [R

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")
    )
  )
))

服務器

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
))

暫無
暫無

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

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