Manipulating dataframe imported as xlsx file and using manipulated data for plotting

Hi! I am planning to create an app which allows a user to import a xlsx file within the ui which in turn will be manipulated by a function I established outside of the server (xlsx file is exported from another software and always has the same structure/header names etc.). This manipulated and summarized data I would like to plot with ggplot2. I don't know if it's even possible since I am relatively new to shiny.
Here is the code I am working with so far.

ui <- fluidPage(
titlePanel("data analysis"),
sidebarLayout(
sidebarPanel(
fileInput("upload",label="data",buttonLabel="Upload...",multiple=F,
placeholder="No file selected",accept=".xlsx"),
selectInput("parameter",
"select parameter for plotting",
selected="parameter 1",
choices=c("parameter 1","parameter 2",
"parameter 3","parameter 4")),
actionButton("trigger","Run Analysis",icon=icon("play"))
),
mainPanel(
plotOutput("plot")
)
)
)
server <- function(input,output,session){
data <- reactive({
req(input$upload)
inFile <- input$upload
read_excel(inFile$datapath,1)
})

manipulated <- reactive(input$upload,data.frame(analysis(data())))

output$plot <- eventReactive(input$run_button,renderPlot({
  ggplot(manipulated(),aes(x=timepoint_description,y=get(input$parameter),group=treatment))+
    geom_point(aes(color=treatment),size=3)+
    geom_line()+
    geom_errorbar(aes(ymin=get(input$parameter)-get(paste(input$parameter,"_sd",sep="")),
                      ymax=get(input$parameter)+get(paste(input$parameter,"_sd",sep=""))),width=0.3)+
    labs(x="time",y=paste("average",input$parameter))+
    theme_classic()
})

}

Looking forward to you help

Welcome to the community @shinyman! Without a sample of the data, I am unable to test the example below, but I believe the approach will give the desired result. Changes were only made to the server section. To summarize, manipulated was changed to an eventReactive() triggered by input$trigger (which requires the data reactive) and output$plot was reduced to a renderPlot() call only (which requires the manipulated reactive).

server <- function(input,output,session){
  
  data <- reactive({
    req(input$upload)
    inFile <- input$upload
    read_excel(inFile$datapath,1)
  })
  
  manipulated <- eventReactive(input$trigger, {
    req(data())
    data.frame(analysis(data()))
    })
  
  output$plot <- renderPlot({
    req(manipulated())
    
    ggplot(manipulated(),aes(x=timepoint_description,y=get(input$parameter),group=treatment))+
      geom_point(aes(color=treatment),size=3)+
      geom_line()+
      geom_errorbar(aes(ymin=get(input$parameter)-get(paste(input$parameter,"_sd",sep="")),
                        ymax=get(input$parameter)+get(paste(input$parameter,"_sd",sep=""))),width=0.3)+
      labs(x="time",y=paste("average",input$parameter))+
      theme_classic()
  })
}

thank you @scottyd22 for welcoming me! Due to your help I could solve all my problems! It seems I wasn't linking the precesses within the server properly but req() was finally providing the solution!
Thanks!!

1 Like

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.