0

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
3
  • Please provide a sample data so that we can reproduce your error. Also, avoid using subset for the reason mentioned here Commented Jun 21, 2015 at 13:26
  • Provided sample data and replaced substitute() with test4 <- test2[test2$UNIT_REFERENCE_NO == input$plant,]. Whoever, no effect on the issue. I can see the problem is with ggplot, but I fail to see why. Commented Jun 21, 2015 at 13:44
  • Please check my answer. You are not correctly using grepl here (at least to your sample data). Commented Jun 21, 2015 at 13:51

1 Answer 1

2

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
))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.