Reputation: 467
I am afraid I am stuck.
I have a simple Shiny script with the intention of subsetting a dataframe based on user input and plot two variables in a scatterplot. When running the script I always get the error "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". All I know is this error occurs when data is n_col!=n_row in a dataframe. However, I do not see how this can be the issue here. What buffles me is, if I execute the snippet below , the plot is drawn without problems:
#test4 <- subset(test2, grepl("PLANT1", test2$PLANTS))
#ggplot(test4, aes(x=test4$HOUR, y=test4$PRICE_NO)) +
geom_point(shape=1)
All I am doing is substituting the string with input$plant from ui.r.
Here is my Main window code:
###################################
# 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/")
My 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)
})
})
My ui.r
library(shiny)
# Title
shinyUI(fluidPage(
titlePanel("TITLE"),
#Sidebar Layout
sidebarLayout(
sidebarPanel(
textInput("plant",
label = h3("Plant:"),
value = "PLANT1")
),
#
mainPanel(
plotOutput("distPlot")
)
)
))
Sample data as requested:
test2
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
Upvotes: 0
Views: 1446
Reputation: 3194
Conditional on what I mentioned in the comment regarding the use of subset
, you can proceed as follows (you don't need to use grepl
here)
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)
})
})
Your sample data which is in data folder inside the app:
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
))
Upvotes: 2