Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering reactive data set in shiny R

I am facing some trouble in filtering a reactive data set which is dependent in several numeric inputs. Is there any way around available?I have provided a similar code below which replicates the task I am trying to achieve.

ui.R
library(shiny)
shinyUI(fluidPage(
  titlePanel("abc"),
  sidebarLayout(
    sidebarPanel(
      numericInput("first", "A",0),
      numericInput("second", "B",0),
      numericInput("third", "C",0),
      numericInput("fourth", "D",0),
      numericInput("fifth", "E",0),
      actionButton("mybutton","Submit")
    ),
    mainPanel(
      tableOutput("mytable")
      )
  )
  )) 



server.R

library(data.table)
library(shiny)
library(stats)
shinyServer(function(input, output) {
  a<-c("A","B","C","D","E","F","G")
  b<-c("10","20","30","40","50","60","70")
  ab<-data.frame(a,b)
  a<-c("A","B","C","D","E")
  input_vector<-reactive({
    c(input$first,input$second,input$third,input$fourth,input$fifth)
  })
  newdata<-reactive({
    data.frame(a,input_vector())
  })
  merged_data<-reactive({
    merge(newdata(),ab,by.newdata=a)
  })
  mutated_data<-reactive({
    library(dplyr)
    merged_data%>%                                                        #using merged()%>% gives error "Warning in Ops.factor(function ()  : ‘*’ not meaningful for factors"
      mutate(newvalue=input_vector*b)                                 #using merged%>% gives error "no applicable method for 'mutate_' applied to an object of class "reactive"
  })
  output$mytable<-renderTable({
    input$mybutton
    isolate(mutated_data())
  })

})
like image 574
Rajarshi Bhadra Avatar asked Jan 11 '15 16:01

Rajarshi Bhadra


1 Answers

I think this is what you want:

library(data.table)
library(shiny)
library(stats)

shinyServer(function(input, output) {
  a<-c("A","B","C","D","E","F","G")
  #b<-c("10","20","30","40","50","60","70")
  b<-c(10,20,30,40,50,60,70)
  ab<-data.frame(a,b)
  a<-c("A","B","C","D","E")

  input_vector<-reactive({
    c(input$first,input$second,input$third,input$fourth,input$fifth)
  })
  newdata<-reactive({
    data.frame(a,input_vector())
  })
  merged_data<-reactive({
    merge(newdata(),ab,by.newdata=a)
  })
  mutated_data<-reactive({
    library(dplyr)
    mutate(merged_data(),newvalue=input_vector()*b)                       
  })

  output$mytable<-renderTable({
    input$mybutton
    isolate(mutated_data())
  })


})

enter image description here

like image 100
Mike Wise Avatar answered Sep 20 '22 10:09

Mike Wise