1

I am new to shiny and trying to figure out some reactive stuff.

Currently this works for a static csv.

## function to return random row from twitter csv
 tweetData <- read.csv('twitterData1.csv')


## stores reactive values 
appVals <- reactiveValues(
    tweet =  tweetData[sample(nrow(tweetData), 1), ],
    ratings = data.frame(tweet = character(), screen_name = character(), rating = character())
    )

I need the same block of reactive values to be funciton but using a selected csv using input$file.

appVals <- reactiveValues(
    csvName <- paste0('../path/', input$file),
    tweetData <- read.csv(csvName),
    tweet =  tweetData[sample(nrow(tweetData), 1), ],
    ratings = data.frame(tweet = character(), screen_name = character(), rating = character())
    )

I get the error:

Warning: Error in : Can't access reactive value 'file' outside of reactive consumer.

I've tried moving things around but I keep getting stuck, help appreciated!

1
  • Use = rather than <- in reactiveValues(). Those are names parameters, not variables. Commented Apr 12, 2021 at 4:17

1 Answer 1

2

The error is telling that you should update the values inside a reactive expression.

First initialize the reactive values:

tweetData <- read.csv('twitterData1.csv')

appVals <- reactiveValues()
appVals$tweet <-  tweetData[sample(nrow(tweetData), 1), ]
appVals$ratings <- data.frame(tweet = character(), screen_name = character())

Then update them with a reactive:

observeEvent(input$file,{
    csvName <- paste0('../path/', input$file)
    if (file.exists(csvName) {
      tweetData <- read.csv(csvName)
      appVals$tweet =  tweetData[sample(nrow(tweetData), 1), ]
      appVals$ratings = data.frame(tweet = character(), screen_name = character(), rating = character())
     }
    })
Sign up to request clarification or add additional context in comments.

2 Comments

thank you this is helpful! When I make this edit I now get the error that "Warning: Error in ..stacktraceon..: object 'appVals' not found". Is there something I need to do to initialize appVals in the code you recommend?
see my edit with initial creation of the reactiveValues

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.