Sam Kingston
Sam Kingston

Reputation: 877

change dataframe values when user inputs numericInput shiny

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.

Upvotes: 2

Views: 3783

Answers (1)

RmIu
RmIu

Reputation: 4487

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)

Upvotes: 4

Related Questions