2

I am having difficulty implementing this code on a more complicated setup that I have. My goal is to let the user change column values with the projected values rendered from numericInput button. Global.R is not an option as there are many operations placed on the dataset before the final version is reached. I have everything setup other than being able to observe the input$action and change the dataframe when an action occurs.

ui.R

shinyUI(
fluidPage(
titlePanel("Basic DataTable"),



# Create a new row for the table.
 fluidRow(
  selectInput("select", label = h3("Select box"), 
              choices = unique(data$Projection), 
              selected = unique(data$Projection)[1]),
  numericInput("num", label = h3("Numeric input"), value = unique(data$Projection)[1]),
  submitButton(text = "Apply Changes", icon = NULL),
  dataTableOutput(outputId="table")
)    
)  
)

server.R

library(shiny)

# Load the ggplot2 package which provides
# the 'mpg' dataset.

 # Given that you are not plotting this line is
### useless library(ggplot2)

# Define a server for the Shiny app

shinyServer(function(input, output) {

  # Filter data based on selections
  output$table <- renderDataTable({
    data$User_Prediction[data$Projection==input$select] <<- input$num
    data
  })
})

global.r

 data <- as.data.frame(c(98,99,34))
 names(data) <- "Projection"
 data$User_Prediction <- 0

Data is stored in server.R as such

 data <-reactive({
 ...
 ...
 ...
 data
 })

I am trying to bring in data which is stored in the reactive function into

 d <- reactiveValues(dat=data)

I have tried

 d <- data()
 d <- data
 d <- reactiveValues(dat=data)

Is there any way I could access the data, because otherwise your code works.

5
  • put your datt in a reactiveValues, values <- reactiveValues(dat = data) in the server. The make an observeEvent(input$button, values$data[...] <- new_values) to update it. This is basic stuff is quite well documented at Rstudios site. Commented Nov 9, 2015 at 6:49
  • and this site, github.com/rstudio/shiny-examples, I particulary like. It has heaps of examples. On a side note, the way you are doing it now with <<- is not good. Reactive values are passed by reference so when you use them you only need <- unlike normal R objects. Commented Nov 9, 2015 at 6:50
  • Thank you for the response. observeEvent is not an option at this time so I am trying to find an alternative. Do you know how I can make a dataframe global so that ui and server can access it, without setting it in global.R Commented Nov 9, 2015 at 6:52
  • put in your server as a reactive value! Commented Nov 9, 2015 at 6:53
  • if you have the data in your reaciveValues, say values <- reactiveValues(dat=data), you can access from within a reactive context using values$data. From outside a reactive context, you can use d <- isolate(values$data), or reactiveValuesToList Commented Nov 9, 2015 at 12:08

1 Answer 1

4

If you want to do this without observeEvent you could do this:

data <- as.data.frame(c(98,99,34))
names(data) <- "Projection"
data$User_Prediction <- 0

ui <- shinyUI(
  fluidPage(
    titlePanel("Basic DataTable"),



    # Create a new row for the table.
    fluidRow(
      column(12,
             selectInput("select", label = h3("Select box"), 
                         choices = unique(data$Projection), 
                         selected = unique(data$Projection)[1]),
             numericInput("num", label = h3("Numeric input"), value = unique(data$Projection)[1]),
             actionButton('btn',"Apply Changes"),
             dataTableOutput(outputId="table")
             )
    )    
  )  
)

server <- shinyServer(function(input, output) {
  d <- reactive({
    data
  })

  dat <- reactiveValues(dat=NULL)
  observe({
    dat$dat <- d()
  })

  observe({
    input$btn
    isolate({
      num <- input$num
      sel <- input$select
    })
    dat$dat$User_Prediction[dat$dat$Projection==sel] <- num
    #d2 <- dat
  })

  # Better way
#   observeEvent(input$btn,{
#     dat$dat$User_Prediction[dat$dat$Projection==sel] <- num
#   })

  # Filter data based on selections
  output$table <- renderDataTable({
    dat$dat
  })
})

shinyApp(ui=ui,server=server)
Sign up to request clarification or add additional context in comments.

4 Comments

the data that I am using is in a reactive content. It doesn't work when I put d<-data( ). How could I approach it?
I update the answer so that the data is stored in a reactive list
My data comes out of data <- reactive{(.......process data... return data}) When I try d <- reactiveValues(dat=data), it doesn't work. I also tried d <- reactiveValues(dat=data( )) It says, could not find function
I edited my original post towards the bottom to be more clear. I really appreciate it.

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.