【发布时间】: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(至少对于您的示例数据)。