0

I need to get the dataframe from a function in rShiny server. But that function returns a Plot and the return value cannot be changed as the plots are used in the future use.

have not pasted the whole code as its like 200 lines each for the function and also for the rshiny server.

Hist_Read_data4 <- full_join(Hist_Read_data1,Hist_Read_data_opst, by = c("timestamp"))%>%
  arrange(timestamp)%>%
  subset(timestamp >= as.POSIXct(start_timestamp, origin = "1970-01-01") & timestamp <= as.POSIXct(end_timestamp, origin = "1970-01-01"))%>%
  mutate(value.y = na.locf(value.y, na.rm = FALSE))%>%
  mutate(value.y = fct_explicit_na(value.y, na_level = "None"))%>%
  mutate(value.x = na.locf(value.x, na.rm=FALSE))%>%
  mutate(new_value = abs(value.x - lag(value.x)))%>%
  mutate(new_value = replace_na(new_value, 0))%>%
  mutate(new_value = cumsum(new_value))

plot <- ggplot() + 
  geom_path(data = Hist_Read_data4, mapping = aes(x = timestamp, y=value.x, color = value.y), na.rm = TRUE, linejoin = 'round' , size=1.5, group = 1) 



//Hist_Read_data4 is the dataframe which i need to return//
//plot is the return value of the function//


  output$HoverText <- renderText({  

    coordinfo <- input$PlotHover
    nearpts <- nearPoints(Hist_Read_data4, coordinfo, xvar= "timestamp", yvar = "value.y", threshold = 20)
     })

need Hist_Read_data4 in inside nearpoints. But it cannot be accessed as its inside a function named chooseDevice() in a separate script file named data_funcs.R I do not want to change the return value of the chooseDevice function from plot to returning this dataframe as it will complicate the whole code and 2 months work will be wasted.

  • Possible duplicate of [Extract data from a ggplot](https://stackoverflow.com/questions/25378184/extract-data-from-a-ggplot) – Grada Gukovic Aug 01 '19 at 19:05
  • Sorry, Did not get you. Can you be bit explanative? –  Aug 01 '19 at 19:49
  • Your ```plot``` is a ```ggplot``` object as in the linked post – Grada Gukovic Aug 01 '19 at 19:50
  • That helps somewhat, but there many many layers of data added and I need all the dataframes to be combined into one. Is there any way to do it? –  Aug 01 '19 at 20:24
  • Now I dont understand you. All data in the ```ggplot``` object can be extracted from the ```plot``` object ```pg <- ggplot_build(plot)``` as the accepted answer to the linked question says. If something is in the plot it is somwere in ```pg```. – Grada Gukovic Aug 01 '19 at 20:42
  • i have many subplots like 4-5 subplots which makes this final plot. So doing that will provide 5 sepearte datatables, and I have to convert that to form a single dataframe again. –  Aug 01 '19 at 20:50
  • Even if i succeed in doing that, as this is inside a function in another script file, i need a way to call that dataframe to the shiny server. How do i do that? –  Aug 01 '19 at 20:51
  • Do you use plot in the shiny server? You can access the data from ```pg``` in tha same way. - – Grada Gukovic Aug 01 '19 at 21:05
  • Yes. I use the plot in the server. my main goal is display the hover points on the plot using the dataframe Hist_read_data4, which is present inside another function. –  Aug 01 '19 at 21:11

0 Answers0