Using reactive variable to change a subset of data using Shiny

I have a data set spanning several years. My shiny app is intended to let the user step through one week at a time, do some analysis and update the data set with new information for that week. e.g. the schedule.

In normal R, I do the following code, where update_schedule is a custom function:

data[indx:(indx+7)] <- update_data(data[indx:(indx+7)], schedule)

Using shiny, I am trying to do something similar, but getting the error:

Error in .getReactiveEnvironment()$currentContext() : Operation not allowed without an active reactive context. (You tried to do something that can only be done from inside a reactive expression or observer.)

#ui shinyUI(pageWithSidebar( sidebarPanel( actionButton("nextButton", "Process Next Week") ), mainPanel( textOutput('week'), plotOutput('myplot') ) )) #server data <<- data.frame(value = rep(1:4, 3:6), open = rep(c(1,1,0), 6)) shinyServer(function(input, output) ({ week <- reactive({as.numeric(input$nextButton)+1}) output$week <- renderText({week()}) #this is the problem line data[(week()*7):((week()+1)*7)]$open <<- rep(1,7) #but here the sub-setting works for plotting output$myplot <- renderPlot({ p1 <- plot(data$value[(week()*7):((week()+1)*7)], col = as.factor(data$open)[(week()*7):((week()+1)*7)]) return(p1) }) }) )

Is there a way to use the week() value to select a portion of data to update? Also, in reality I am trying to update several variables at once, not just the "open" column in this example.

I tried the following to keep my reactive variables in a reactive statement, but got an error:

data <<- reactive({data %>% mutate(open = replace(open, (week()*7):((week()+1)*7, rep(1,7))}) Error in plot(data()$value[(week() * 7):((week() + 1) * 7)], col = as.factor(data()$open)[(week() * : error in evaluating the argument 'x' in selecting a method for function 'plot': Error in UseMethod("mutate_") : no applicable method for 'mutate_' applied to an object of class "reactive"


Did you read and fully understand the reactive expressions chapter of the shiny tutorial?

Any expression that uses a reactive expression (week is reactive since you define it with a reactive({}) must be used inside a reactive context, such as another reactive() call or inside observe or renderXXX. You should also look at reactiveValues() function

Category:r Time:2018-11-07 Views:0
Tags: shiny

Related post

  • Using a variable for Element name in Core Data Predicate 2012-02-13

    I am trying to write a method which tries to retrieve an object based on a predicate using a variable (as part of a NSXMLParser). The code looks like this: I have these variables defined in the class: @property (strong, nonatomic) NSManagedObjectCont

  • how to use the variable of one class into another class using java? 2010-07-16

    Ihave two class files. In that first file, i stored aaa class firl and in the next text file i store the class file bbb.Io want to use the variable of aaa class into bbb class. How to use use it. Note :If i put String value variable as public then it

  • List of files changed since a certain date using SVN 2011-05-05

    What kind of SVN command can I run that will get me a list of files changed since a certain date? Right now I have it as svn log <url> -r {2010-11-01}:{2011-05-04} > log.txt That almost work, but it only shows the revisions and comments but

  • Detecting changes to the source data using the XmlDataProvider and a bound TextBox 2008-11-04

    I have a TreeView bound to an XmlDataProvider. The name of a node is reflected in the tree and the data of the node is in a TextBox. When you click on a tree item, it loads the data in the TextBox. What is the best way to determine when a user has ch

  • Use jsf within jsp (change portion of jsp to use jsf) 2011-06-14

    I need help about converting a portion of jsp page to jsf. My old jsp page is big, and I am trying to change a portion of it (CountryDropdown List) to JSF. So I created a countryDropdown.jsf, I tested to run it directly, it loaded the country-list/wo

  • R: using ddply to apply functions to subsets of data 2011-03-06

    I'm trying to use the ddply method to take a dataframe with various info about 3000 movies and then calculate the mean gross of each genre. I'm new to R, and I've read all the questions on here relating to ddply, but I still can't seem to get it righ

  • Dealing with subsets of data using csv.Dictreader 2012-04-17

    I'm parsing a big csv file using csv.Dictreader . quotes=open( "file.csv", "rb" ) csvReader= csv.DictReader( quotes ) Then for each row I'm converting the time value in the csv in datetime using this : for data in csvReader: year = int(data["Date"].s

  • Using OnClick event of Anchor Tag Retrieved Database Data using php 2011-12-22

    I want that if i click on an anchor tag it shows the mysql database table values for example <a href"#">Click to show comments</a> after clicking it will do something like this SELECT * FROM `comments`LIMIT 0 , 30 --------------Solutions-

  • When do I use static variables/functions in php? 2009-08-10

    I am refreshing myself on OOP with PHP and I saw an example of setting functions and/or variables as static. When and why would I set a variable/function to static? I've done other languages and don't really recall ever using static, I never found a

  • Variable not changing inside jquery click function 2011-08-23

    I have a list of most 17 recent entries from a db table and wanna use jquery/ajax to have a next button that loads the next 17 entries when you click it. It passes the 17 variable to the .load which works fine first time around (this is later used as

  • How do I add a property to a Javascript Object using a variable as the name? 2009-03-29

    I'm pulling items out of the DOM with JQuery and want to set a property on an object using the id of the DOM element. For example: obj = {}; jQuery(itemsFromDom).each(function() { element = jQuery(this); name = element.attr("id"); value = element.att

  • Using a variable from Foreach Loop Container in a SQL Task 2009-12-09

    Ok, I have a simple process... Read a table and get the rows that have a "StatusID" of 1. Simple. Select ProductID from PreorderStatus where StatusID = 1 Foreach row returned from that query, perform an action. For simplicity sake, let's just modify

  • scanf / field lengths : using a variable / macro, C/C++ 2010-07-21

    How can I use a variable to specify the field length when using scanf. For example: char word[20+1]; scanf(file, "%20s", word); Also, is it correct to use 20+1 (since it needs to add a at the end?). Instead, I'd like to have something like: #define M

  • I need a unique subset of data from my table using SQL 2010-07-22

    I am working with a Dbase table via SQL commands. I have the following free table to work with. import1 account, phone, subaccount, locationid I need the first occurrence of each uinique combination of account and phone. No one field in the table is

  • How to Identify the Python version in #! using environment variables 2011-08-03

    I have a problem which is caused by our encapsulated design. Up till now lots of our scripts were written in bash and as a result the #!/bin/bash was always simple. However now that we are rewriting our scripts in python that is a bit more difficult.

  • Using a variable in one class in my service 2011-09-07

    This is a part of my main class: @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if (url.startsWith("")) { try { songURL = new URL(url); } catch (MalformedURLException e1

  • Get data using POST and AJAX 2011-05-09

    I'm trying to send some data in my server asynchronously using AJAX. I need to send the data using the POST method because the data sent are quite many characters and by using GET the created URL will be too big. Well that's not a problem, but for ae

  • How to subset PolySet data in PBSmapping package? 2012-02-23

    This is a repost from gis.stackexchange. The question is fairly long and detailed, so I am not repeating it. The simple problem is that I can't figure out a way of subsetting PolySet data used in the PBSmapping package. One option would be to convert

  • Best way to ship 1GB of structured textual data used in my software? 2009-11-24

    I've around 1GB of structured text data, (which is currently stored in database MySQL, mroe than 1million records) used by software. I've to ship this data along with the software. I also need to protect this data. This data shouldn't be not accessib

Copyright (C), All Rights Reserved.

processed in 0.894 (s). 13 q(s)