简体   繁体   中英

R shiny rendering forestplot output

I am using forestplot to generate forestplot visualization for my odds ratio data. I am trying to publish the forestplot in shiny app. In shiny, you can use can use plotlyoutput() to show the plot generate by plotly and ggplotly() function to convert ggplot to plotly object.

I am trying either to convert the forestplot output to plotly object so that I can use plotlyoutput() or use function that renders the forestplot object directly.

Here is the sample example I am using to generate the forestplot -> https://cran.r-project.org/web/packages/forestplot/vi.nettes/forestplot.html

 p <- forestplot(labeltext = tabletext,
             mean = data.matrix(output_df$mean),
             lower = data.matrix(output_df$lower),
             upper = data.matrix(output_df$upper),
             clip = c(0.1, 10), 
             xlog = F, 
             col = fpColors(box = "royalblue",
                            line = "darkblue")) 

plotly example that I am able to render. I want to do something similar so that I can render and output forestplot.

# drug cleaveland plot
  output$drug_cleveland_plot = renderPlotly({
    
    df <- df_drug_plot()
    
    df <- sqldf("select distinct concept_name,w_cond_rate as rate,'Diagnosed' as grp from df 
                union
                select distinct concept_name,w_exp_rate as rate,'Expected' as grp from df
                ")
    
    df <- df %>% 
      arrange(rate) %>% mutate(grp = factor(grp)) %>%
      mutate(concept_name=factor(concept_name))
    
    p <- df %>%
      arrange(grp, rate, desc(concept_name)) %>%
      ggplot(aes(rate, fct_inorder(concept_name))) +
      geom_line(aes(group = concept_name)) +
      geom_point(aes(color = grp)) +
      scale_x_continuous(breaks = seq(0, 1.1, by = 0.1)) +
      theme_bw() + 
      theme(panel.grid.major.x = element_line( linetype = "dotted", size = 0.2, color = 'grey' ))  +
      scale_colour_manual(values=c("#d91e4a", "#939597")) + 
      theme (legend.title=element_blank())
    
    m <- list(
      l = 200,
      r = 100,
      b = 100,
      t = 100,
      pad = 5
    )
    
    fig <- ggplotly(p,height = 800, width = 1500) %>% layout(title = "Drugs: Observed vs Expected Rate",
                                                             autosize = F, 
                                                             margin = m,
                                                             yaxis = list(title = "",
                                                                          automargin = TRUE),
                                                             legend = list(title=list(text='<b> Group </b>')))
    fig
    
    
  })

I was able to render plot using renderPlot() function and display the output using plotoutput() funciton.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM