-2

I have a code that looks something like this:

d_frame<-reactive(unique(as.data.frame(do.call("rbind", sapply(1:(length(intarr())), 
FUN = function(i) c(substr(readlinesop()
[intarr()[i]+1],17,26),substr(readlinesop()[intarr()[i]+2],17,26)), simplify = FALSE)))))

bv<-reactive(ncol(d_frame()))
colnames(d_frame) <- c("Sand", "Water")
subset_dataset <-eventReactive(input$go, {d<-bv()})

The first line which has the output d_frame creates a data frame. When I try to change the column name of this it throws an error :

enter image description here

So, I tried to find the number of columns in the d_frame using ncol as mentioned above which returned 2. But, I don't know what's causing the error. Can you please help me with this?

Aizen
  • 561
  • 1
  • 9
  • 19
  • 1
    Your `d_frame` is a function hence and all updates need to be within reactive expression or observer – Pork Chop Nov 29 '16 at 18:16
  • @PorkChop: I changed as mentioned as below, but still no luck. Can you please help. I am a newbie to R shiny. – Aizen Nov 30 '16 at 08:14

2 Answers2

1

Try something like this. Note that the d_frame() is a reactive function. In your calculations you will use d_frame2()

 d <- NULL
 d_frame <-
   reactive(unique(as.data.frame(do.call(
     "rbind", sapply(
       1:(length(intarr())),
       FUN = function(i)
         c(substr(readlinesop()
                  [intarr()[i] +
                      1], 17, 26), substr(readlinesop()[intarr()[i] + 2], 17, 26)),
       simplify = FALSE
     )
   ))))

 bv <- reactive(ncol(d_frame()))
 d_frame2 <- reactive({
   testdata <- d_frame()
   colnames(testdata) <- c("Sand", "Water")
   testdata
 })

 subset_dataset <- eventReactive(input$go, {
   d <<- bv()
 })
user5249203
  • 4,436
  • 1
  • 19
  • 45
Pork Chop
  • 28,528
  • 5
  • 63
  • 77
  • It gives error: "Error in <-: invalid (NULL) left side of assignment". If I remove () from d_frame in colnames(d_frame()) <- c("Sand", "Water"), I again get the same error what I mentioned in my question. – Aizen Nov 30 '16 at 08:52
  • I don't know exactly how you're constructing your dataframe `d_frame()` you will need to give more code that include the `intarr()` and `readlinesop()` – Pork Chop Nov 30 '16 at 08:53
  • This works!! Thanks. But can you please explain what was causing the error? – Aizen Nov 30 '16 at 08:58
  • 1
    I don't think we can directly change the column names of a reactive function, you have to put the contents into a variable first – Pork Chop Nov 30 '16 at 09:04
0

In your example, d_frame is a reactive function returning a data.frame, not a data.frame itself.

You can set its column names when you build it:

d_frame <-
   reactive(unique(as.data.frame(
     do.call("rbind", sapply(
       1:(length(intarr())),
       FUN = function(i)
         c(substr(readlinesop()[intarr()[i] + 1], 17, 26),
           substr(readlinesop()[intarr()[i] + 2], 17, 26)),
       simplify = FALSE
     )), col.names <- c("Sand", "Water")
   )))
EvilTak
  • 7,091
  • 27
  • 36
HubertL
  • 19,246
  • 3
  • 32
  • 51
  • I added the col.names as mentioned above but I still couldn't see the column names, it is still coming as V1 & V2. Further code is something like this: subset_dataset <-eventReactive(input$go, {d_frame()}) – Aizen Nov 29 '16 at 18:30
  • Any more info on this? – Aizen Nov 30 '16 at 08:15