passing dataframe from outside function to server in shiny app

377 views Asked by At

I want to send my from function summ() to server function. I am performing all operations in summ() function{operationsi am not showing} and at last dataframes is prepared. In server, I want to plot two separate graphs using those dataframes named df and df1. Can anybody help me how to send these dataframes from summ function into .

 library(shiny)
 library(ggplot2)
 ui<-fluidPage(
sidebarLayout(
sidebarPanel(
  sliderInput(inputId = "Y",label = "Price", value = 2, min =1,max = 6),
  sliderInput(inputId = "d_km",label = "Distance", value = 5, min =1,max = 20),
  sliderInput(inputId = "fact",label = "Multiplication", value = 2, min =1,max = 5),
  actionButton(inputId = "button", label = "Simulate")
),
mainPanel(
  plotOutput("X"),
  plotOutput("HH")


    )

 )
)

  summ<-function(Y,d_km,fact)
{

  ### code   ###
 df <- data.frame("x"=x,"y1"=y1,"y2"=y2)
 df1<- data.frame("obj"=obj_vector)

returns <- list(plot = df, plots=df1)

}
 server<-function(input, output,session){
 default_list <- list(plot = df, plots=df1)

  # daf <- data.frame()
  rv<-reactiveValues(data=default_list)
  rv$data <- reactive(summ (input$Y, input$d_km, input$fact))

  frame <- eventReactive(input$button, {
returned <- rv$data()
cost <- returned$plot
 })

  frame1 <- eventReactive(input$button, {
returned <- rv$data()
coost <- returned$plots
 })


   output$X <- renderPlot({
    ggplot(frame(), aes(x)) +                    # basic graphical object
  geom_line(aes(y=y1), colour="red") +  # first layer
  geom_line(aes(y=y2), colour="green")+
  xlab("Bag_no.")+ylab("Price in USD")+
  geom_text(aes(y=y1,label=y1),size=3,angle=45)
  })

  output$HH <- renderPlot({
   ggplot(frame1(),aes(x=seq(1,runs,1),y=obj_vector))+geom_line(colour="blue")+
    xlab("Simulation_run")+ylab("Objective_value")
   # grid.arrange(p1,p2,ncol=2)
 })
 }
   shinyApp(ui = ui, server = server) 

Appreciate your help.

1

There are 1 answers

0
Mark Akritas On BEST ANSWER
  1. In your server you define default_list using df and df1 which are not defined not before nor after in your global or server's local environments. Also you don't actualy use default_list even after assigning it to rv using reactiveValues(), so I don't see the goal of default_list in your App. You can replace that one like following, if you still need it in your program:
default_list <- list(plot = c(), plots = c())
  1. In server frame and frame1 are functions and they are assumed to return smth. So you can change it like this:
frame <- eventReactive(input$button, {
  returned <- rv$data()
  cost <- returned$plot
  cost # <= Add This
})

frame1 <- eventReactive(input$button, {
  returned <- rv$data()
  coost <- returned$plots
  coost # <= And This
})
  1. In output$HH visualization you use variable runs which is not defined anywhere in your App, you need to define it beforehand to use (maybe you have defined it in code that you don't show, but now it causes error).
  2. Again in output$HH you use y = object_vector, but in summ() you assign it to data.frame('obj' = object_vector), so you need to change your code like this:

Or in summ():

data.frame('object_vector' = object_vector)

Either in output$HH:

y = obj