0

I have a table that I want to update in Shiny based on the inputs. Pretty Basic.

The table structure is as follows:

-It's a single row with many columns.

-The Columns to be updated are numbered(lets say 1-10).

-These columns are like identifiers.

OldData TEXT TEXT TEXT 1 2 3 4 5 6 7 8 9 10
First Character Character Character Numeric Numeric Numeric Numeric Numeric Numeric Numeric Numeric Numeric Numeric

The input table is variable in size and here is its structure:

Rows in the first column contain how ever many of the identifiers we need.

Rows in the second column contain the the value that needs to replace the corresponding identifiers's value in the old data frame

NewData IDColumn VALUEColumn
First 8 Numeric
Second 4 Numeric
Third 5 Numeric
Fourth 1 Numeric
Fifth 7 Numeric

The end result is newly named data frame that is the same structure as the first but with the values updated with the info from the new dataframe.

Updated Table Structure:

OldData A B C 1 2 3 4 5 6 7 8 9 10
First Character Character Character NewNumeric OldNumeric OldNumeric NewNumeric NewNumeric OldNumeric NewNumeric NewNumeric OldNumeric OldNumeric

I thought I would try a for-loop but am unsure where to start.

 updated_table_df <- reactive({
    for (i in 1:nrow(newtable$columnA)){
      for(v in 1:nrow(newtable$columnB)){
      updated_table <- oldtable() %>% 
        replace(.,i,v) %>% 
        select(1:10) # This was an attempt to output the new table to see if it worked
      }
    }
  })

I hope this made sense. If you have any questions let me know.

Thanks.

3
  • Can you share a little sample data to make the example reproducible? Use dput() or other valid R syntax so that the sample data copy/pasteable. Commented May 10, 2021 at 15:50
  • I'm a bit confused by column names - is it just a coincidence that the displayed table and input table both have columns named "A" and "B"? Or is there a relationship between those columns? And maybe you could give names for the tables so we can talk about them? When I look at your code and try to compare to your text oldtable seems clear, but newtable and updated_table aren't so clear... Commented May 10, 2021 at 15:54
  • @Gregor Thomas oldtable and newtable combine into updated_table. I tried to update according to be more clear. Commented May 10, 2021 at 16:18

1 Answer 1

1

I'd be inclined to gather() your oldTable into two columns, A (the label) and value. Then prep newTable so that B is renamed to "replacement_value". Then left join oldTable and newTable to create updated_table, using mutate() to change value to replacement_value wherever it's available. Finally use spread() to flip it back into a single row.

Maybe in your question it would be good to replace A and B in newTable with something else (ID and value?) since you already have A and B as columns in your main table and it might get a bit confusing.

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.