Using reactive dataframe as dataframe for plots and maps in r shiny

Hi all,

I'm trying to use a reactive dataframe as the data for my plots, and am running into trouble.. (one error message after another). Now, the dataframe works but when I try to plot the data, no points show up.

Can someone provide guidance on how I can use the reactive dataframe as the base of my charts/plots? Is there something wrong with the way I have specified data?

I am looking to use this same reactive dataframe for leaflet maps too..

Thanks for your time!


ui <-  
 fluidPage(  sidebarLayout(
              
              sidebarPanel(position = "left",width = 3, h3(strong("User Inputs", align= "left")),
                          
                         numericInput("Phase_1", "Phase 1", 
                                        value = 0, min = 0, max = 5000),

                         
                           numericInput("Phase_2", "Phase 2", 
                                        value = 0, min = 0, max = 12000),

                           
                           numericInput("Phase_3", "Phase 3", 
                                        value = 0, min = 0, max = 20000),


                           


            
              mainPanel(

                dataTableOutput("datatable1"), plotOutput("plot"))))


server <- function(input, output, session) {
  
  newdata <- reactive({
 
  
    Var1 <- as.numeric(input$Phase_1)
    Var2 <- as.numeric(input$Phase_2)
    Var3 <- as.numeric(input$Phase_3)

 
    #total cost 
    dataset$Total_Cost<- Var1 + Var2 + Var3 
 
 dataset$Cost_per_person<-dataset$Total_Cost/dataset$population
dataset$Cost_per_case<-dataset$Total_Cost/dataset$cases
 
    
 newdatadata<-data.frame(dataset$Area, dataset$Total_Cost, dataset$Cost_per_person, dataset$Cost_per_case).  

})

  output$datatable1 <- renderDataTable(newdata())
  output$plot<-renderPlot({
    ggplot(data=newdata(), aes(x=as.numeric(Cost_per_case), y=as.numeric(Cost_per_person))) + geom_point()+xlim(0,1000)+ylim(0,1000)
    

    
    })
                                   
                                   
}

# Run the application 
shinyApp(ui = ui, server = server)


Here is an example of the dataset:
Area 	population	cases 	Phase_1	Phase_2	Phase_3
Area 1	99895	678	0	0	0
Area 2	10000	1002	0	0	0
Area 3	10950	602	0	0	0

It seems you've either made the mistake of not creating this, or you did create it in your real code but didn't share the relevant code here in your post.

1 Like

Hi,

I just edited my post to include an example of the data.

library(shiny)
library(tidyverse)
ui <-
  fluidPage(sidebarLayout(
    sidebarPanel(
      position = "left", width = 3, h3(strong("User Inputs", align = "left")),
      numericInput("Phase_1", "Phase 1",
        value = 0, min = 0, max = 5000
      ),   numericInput("Phase_2", "Phase 2",
        value = 0, min = 0, max = 12000
      ),   numericInput("Phase_3", "Phase 3",
        value = 0, min = 0, max = 20000
      ),
    ),
    mainPanel(
      dataTableOutput("datatable1"), plotOutput("plot")
    )
  ))
    
    
    server <- function(input, output, session) {
      
      newdata <- reactive({
    
        Var1 <- as.numeric(input$Phase_1)
        Var2 <- as.numeric(input$Phase_2)
        Var3 <- as.numeric(input$Phase_3)
                #total cost 
        iris$Total_Cost<- Var1 + Var2 + Var3 
        
        iris$Cost_per_person<-iris$Total_Cost/iris$Sepal.Length
        iris$Cost_per_case<-iris$Total_Cost/iris$Petal.Width
        
        
        select(iris,
               Petal.Width, 
               Total_Cost, 
               Cost_per_person, 
               Cost_per_case)
      })
      
      output$datatable1 <- renderDataTable(newdata())
      output$plot<-renderPlot({
        ggplot(data=newdata(), aes(x=Cost_per_case, y=Cost_per_person)) + geom_point()+xlim(0,1000)+ylim(0,1000)
      })
    }
    
    shinyApp(ui, server)

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.