Shiny - renaming factors in reactive data frame
<p>I'm building a shiny app that queries an SQL database so the user can ggplot the data. I would like the user to be able to rename factors manually but am struggling to get going. Here is an example of what I want to do:</p>

<p>ui.R</p>

<pre><code>library(markdown)

shinyUI(fluidPage(
  titlePanel("Reactive factor label"),
  sidebarLayout(
    sidebarPanel(

      numericInput("wafer", label = h3("Input wafer ID:"), value = NULL),

      actionButton("do", "Search wafer"),
      textInput("text", label = h3("Factor name to change"), value = ""),
      textInput("text", label = h3("New factor name"), value = ""),
      actionButton("do2", "Change name")

       ),

    mainPanel(
      verbatimTextOutput("waf"), 
      verbatimTextOutput("que"), 
      verbatimTextOutput("pos"),
      dataTableOutput(outputId="tstat")
    )
  )
)
)
</code></pre>

<p>server.R</p>

<pre><code># Create example data

Name &lt;- factor(c("Happy", "New", "Year"))
Id &lt;- 1:3

dd &lt;- data.frame(Id, Name)

con &lt;- dbConnect(RSQLite::SQLite(), ":memory:")
dbWriteTable(con, "dd", dd)
query &lt;-  function(...) dbGetQuery(con, ...)

wq = data.frame()
sq = data.frame()


shinyServer(function(input, output){

  # create data frame to store reactive data set from query
  values &lt;- reactiveValues()
  values$df &lt;- data.frame()

  # Wait for user to search
  d &lt;- eventReactive(input$do, { input$wafer })

  # Reactive SQL query
  a &lt;- reactive({ paste0("Select * from dd where Id=",d()) })
  wq &lt;- reactive({  query( a() ) })

  # Check outputs
  output$waf &lt;- renderPrint(input$wafer)
  output$que &lt;- renderPrint({ a() }) 
  output$pos &lt;- renderPrint( wq()[1,1] )

  # observe d() so that data is not added until user presses action button  
  observe({
    if (!is.null(d())) {
      sq &lt;- reactive({  query( a() ) })

      # add query to reactive data frame
      values$df &lt;- rbind(isolate(values$df), sq())
    }
  })

  output$tstat &lt;- renderDataTable({
    data &lt;- values$df
  })

})
</code></pre>

<p>In static R I would normally use data table to rename factors i.e.:</p>

<pre><code>DT &lt;- data.table(df)
DT[Name=="Happy", Name:="Sad"]
</code></pre>

<p>But I'm not sure how to go about this with a reactiveValues i.e. <code>values$df</code>.</p>

<p>I have read this (<a href="https://stackoverflow.com/questions/29716868/r-shiny-how-to-get-an-reactive-data-frame-updated-each-time-pressing-an-actionb">R shiny: How to get an reactive data frame updated each time pressing an actionButton without creating a new reactive data frame?</a>). This lead me to try this but it doesn't do anything (even no error):</p>

<pre><code>observeEvent(input$do2, {
    DT &lt;- data.table(values$df) 
    DT[Name == input$text1, Name := input$text2]
    values$df &lt;- data.frame(values$df)

  })
</code></pre>

<p>Perhaps there is a way around this..maybe there is a way to use an action button to "lock in" the data as a new data frame, which can then be used to rename?</p>

<p>Sorry for such a long winded question. My real app is much longer and more complex. I have tried to strip it down.</p>