使用 Shiny/ggplot2 对数据进行子集化时出现错误 "arguments imply differing number of rows"
Error "arguments imply differing number of rows" when subsetting data with Shiny/ggplot2
恐怕卡住了。
我有一个简单的闪亮脚本,目的是根据用户输入对数据帧进行子集化,并在散点图中绘制两个变量。当 运行 脚本时,我总是得到错误 "Error in data.frame(x = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, : arguments imply differing number of rows: 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 替换字符串。
这是我的主要 window 代码:
###################################
# 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/")
我的server.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
的使用,你可以进行如下操作(这里不需要使用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")
)
)
))
server.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
))
恐怕卡住了。
我有一个简单的闪亮脚本,目的是根据用户输入对数据帧进行子集化,并在散点图中绘制两个变量。当 运行 脚本时,我总是得到错误 "Error in data.frame(x = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, : arguments imply differing number of rows: 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 替换字符串。
这是我的主要 window 代码:
###################################
# 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/")
我的server.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
的使用,你可以进行如下操作(这里不需要使用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")
)
)
))
server.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
))