2
votes

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.

1
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.Rorschach
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.Rorschach
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.RSam Kingston
put in your server as a reactive value!Rorschach
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 reactiveValuesToListRorschach

1 Answers

4
votes

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)