Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Adjuts number of digits for small pvalue to be displayed in title of a plot using renderPlot in Shiny in R

In R one can use options(digits=n) to adjust the number of digits in the output value. However, this does not work in Shiny, and sprintf() does not work with very low numbers either since it would only display zeros. So, how could i obtain in Shiny something like

options(digits = 4)

p.val$p.value
[1] 3.724e-23

So that i could use it in the title of the plot in renderPlot?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Instead of changing options, we can simply use signif to set our required number of digits. The following mini app can be modified as necessary to work for plot titles.

library(shiny)
ui <- fluidPage(
  fluidRow(
    column(6,
           
           numericInput(
             "hello_options",
             label = "Number",
             value = 3.145677177118
           )),
    
    column(6, 
           numericInput("digits",
                        label = "Digits",
                        value = 4
                        )
           )
  ),
  
  
  textOutput("out_text")

)

server <- function(input, output, server){
    output$out_text <- renderText(
      signif(input$hello_options, digits = input$digits)
    )
}

shinyApp(ui = ui, server = server)

If you want to format to scientific, then change your server to this. Note however that this gives you characters that should be converted back.

server <- function(input, output, server){
    output$out_text <- renderText(
      format(signif(input$hello_options, digits = input$digits),
             scientific = TRUE)
    )
}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading