RStudio Shiny Error - number of items to replace is not a multiple of replacement length

umutesen

I am fairly new to R and currently working on a Shiny web app using RStuido to recognise handwritten digits.

The data I am using is from a Kaggle competition: Digit-Recogniser

I have the following function to render average representations of digits

 # Produce a plot containing the average representations of digits

  output$digitAvgRep <- renderImage({

    ## For visualising traing data
    dataFrame<-as.matrix(data())

    ##Color ramp def.
    colors<-c('white','red')
    cus_col<-colorRampPalette(colors=colors)

    ## Plot the average image of each digit
    par(mfrow=c(4,3),pty='s',mar=c(1,1,1,1),xaxt='n',yaxt='n')
    all_img<-array(dim=c(10,28*28))
    for(di in 0:9)
    {
      print(di)
      all_img[di+1,]<-apply(dataFrame[dataFrame[,1]==di,-1],2,sum)
      all_img[di+1,]<-all_img[di+1,]/max(all_img[di+1,])*255

      z<-array(all_img[di+1,],dim=c(28,28))
      z<-z[,28:1] ##right side up1
      image(1:28,1:28,z,main=di,col=cus_col(256))
    }
  })

and I pass the above variable to ui.R using:

# User renderUI to dynamically create objects in the ui.R 
  output$tb <- renderUI({
    if(is.null(data()))
      h5("No data loaded.")
    else
      tabsetPanel(
        tabPanel("About file", tableOutput("filedf")),
        tabPanel("RawData", tableOutput("table")),
        tabPanel("Summary", tableOutput("sum")),
        tabPanel("Digit Distribution",plotOutput("digitDist")),
        tabPanel("Average Digit Representation", imageOutput("digitAvgRep")))
  })

I get the following output when I run the app:

[1] 0
Error in all_img[di + 1, ] <- apply(dataFrame[dataFrame[, 1] == di, -1],  : 
  number of items to replace is not a multiple of replacement length

Anyone know how to solve this?

Thanks

biobirdman

As the error suggest, you are trying to insert a new vector of length that is shorter than the column that you are placed and the number of items is not a multiple of replacement length

using the following as an example :

## create a matrix that is 6 rows and 2 columns
mat <- matrix(1:12, 6,2) 

## try to replace first column with a vector of length 3
## works fine because 6 is a multiple of 3

mat[,1] <- 1:3 

## try to replace first column with a vector of length 5
## Fails because 6 is not a multiple of 5, try the same exception.

mat[,1] <- 1:5 

##   number of items to replace is not a multiple of replacement length

Hence, you may want to reconsider your apply statement. I will consider creating list to store the results instead of trying to store it in a matrix.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

R error in '[<-.data.frame'... replacement has # items, need #

From Dev

RStudio Shiny ERROR: there is no package called "shinydashboard"

From Dev

Error in boot() related to replacement length and data or data types? - R

From Dev

R number of items to replace is not a multiple of replacement length

From Dev

R error: "number of items to replace is not a multiple of replacement length"

From Dev

R number of items to replace is not a multiple of replacement length / results however correct

From Dev

RStudio Shiny Error mysqlNewConnection maximum of 16 connections

From Dev

How to look for multiple data sets for use in Shiny Interactive Document in RStudio?

From Dev

R Error: replacement diagonal has wrong length

From Dev

R: number of items to replace is not a multiple of replacement

From Dev

RStudio - Shiny - Error "Operation not allowed without an active reactive context"

From Dev

Merge equal items with multiple lists but different length

From Dev

Linux, find replace on a folder of files using a list of items for replacement?

From Dev

array in R number of items to replace is not a multiple of replacement length

From Dev

Replace multiple Regex Matches each with a different replacement

From Dev

Why do I get "number of items to replace is not a multiple of replacement length"

From Dev

R Error: replacement diagonal has wrong length

From Dev

Replace the value with "X" with same number of length

From Dev

Validate() function for multiple conditions in Shiny, Rstudio

From Dev

Error: Replacement has length zero R

From Dev

RMOA predict error (number of items to replace is not a multiple of replacement length)

From Dev

'replacement has length zero' error in R

From Dev

Error in R :Number of items to replace is not a multiple of replacement length

From Dev

number of items to replace is not a multiple of replacement length for a data frame

From Dev

TSLM Error: Replacement Has Length Zero

From Dev

Nested loops in R multiple replacement length

From Dev

Error number of items to replace is not a multiple of replacement length

From Dev

RStudio Server and Shiny Server Websocket Error 400: The fix for Shiny Server didn't work for RStudio Server

From Dev

Counting number of items in multiple groups

Related Related

  1. 1

    R error in '[<-.data.frame'... replacement has # items, need #

  2. 2

    RStudio Shiny ERROR: there is no package called "shinydashboard"

  3. 3

    Error in boot() related to replacement length and data or data types? - R

  4. 4

    R number of items to replace is not a multiple of replacement length

  5. 5

    R error: "number of items to replace is not a multiple of replacement length"

  6. 6

    R number of items to replace is not a multiple of replacement length / results however correct

  7. 7

    RStudio Shiny Error mysqlNewConnection maximum of 16 connections

  8. 8

    How to look for multiple data sets for use in Shiny Interactive Document in RStudio?

  9. 9

    R Error: replacement diagonal has wrong length

  10. 10

    R: number of items to replace is not a multiple of replacement

  11. 11

    RStudio - Shiny - Error "Operation not allowed without an active reactive context"

  12. 12

    Merge equal items with multiple lists but different length

  13. 13

    Linux, find replace on a folder of files using a list of items for replacement?

  14. 14

    array in R number of items to replace is not a multiple of replacement length

  15. 15

    Replace multiple Regex Matches each with a different replacement

  16. 16

    Why do I get "number of items to replace is not a multiple of replacement length"

  17. 17

    R Error: replacement diagonal has wrong length

  18. 18

    Replace the value with "X" with same number of length

  19. 19

    Validate() function for multiple conditions in Shiny, Rstudio

  20. 20

    Error: Replacement has length zero R

  21. 21

    RMOA predict error (number of items to replace is not a multiple of replacement length)

  22. 22

    'replacement has length zero' error in R

  23. 23

    Error in R :Number of items to replace is not a multiple of replacement length

  24. 24

    number of items to replace is not a multiple of replacement length for a data frame

  25. 25

    TSLM Error: Replacement Has Length Zero

  26. 26

    Nested loops in R multiple replacement length

  27. 27

    Error number of items to replace is not a multiple of replacement length

  28. 28

    RStudio Server and Shiny Server Websocket Error 400: The fix for Shiny Server didn't work for RStudio Server

  29. 29

    Counting number of items in multiple groups

HotTag

Archive