根据闪亮条件

时间:2019-07-11 04:34:16

标签: r shiny

我有一个闪亮的应用程序,可以生成测试统计表。我想加粗小于用户提供的临界值的单元格。在下面的示例中,我在有效值旁边添加了*。我想将该数字改为粗体。我不确定执行此操作的最佳方法。也许DT:: datatable()胜任这项工作?

library(shiny)
library(ggplot2)
# Define UI for application that draws a histogram
ui <- fluidPage(

   # Sidebar with a slider input for number of bins 
   sidebarLayout(
      sidebarPanel(
        numericInput(inputId="pcrit", label="P crit", 
                     value=0.05,min=0,max=1,step=0.001)
      ),

      mainPanel(
         plotOutput("datPlot"),
         tableOutput("sigTable")
      )
   )
)

server <- function(input, output) {

  doDat <- reactive({
    n <- 5e2
    nTrials <- 10

    dat <- data.frame(x = rnorm(n),
                      w = seq(0.01,0.5,length.out = nTrials),
                      trial = 1:nTrials)

    dat$y <- dat$x * dat$w + rnorm(n)
    dat
  })
    doCorr <- reactive({
    dat <- doDat()
    res <- data.frame(trial=1:nTrials,corr=NA,pVal=NA)
    for(i in 1:nTrials){
      tmp <- cor.test(formula=~y+x,data=dat[dat$trial==i,])
      res$corr[i] <-tmp$estimate
      res$pVal[i] <-tmp$p.value
    }
    res  
  })

   output$datPlot <- renderPlot({
     dat <- doDat()
     p <- ggplot(data = dat,aes(x=x,y=y))
     p <- p + geom_point()
     p <- p + facet_wrap(~trial)
     p
   })
   #change this to produce bolded numbers rather than use the clunky *
   output$sigTable <- renderTable({
     res <- doCorr()
     res$corr <- round(res$corr,3)
     pcrit <- input$pcrit
     res$corr[res$pVal <= pcrit] <- paste(res$corr[res$pVal <= pcrit],
                                          "*",sep="")  
     res
   })
}

shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:1)

您可以尝试以下代码吗?...

library(shiny)
library(ggplot2)
library(DT)
# Define UI for application that draws a histogram
ui <- fluidPage(

  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
      numericInput(inputId="pcrit", label="P crit", 
                   value=0.05,min=0,max=1,step=0.001)
    ),

    mainPanel(
      plotOutput("datPlot"),
      DTOutput("sigTable")
    )
  )
)

server <- function(input, output) {

  doDat <- reactive({
    n <- 5e2
    nTrials <<- 10

    dat <- data.frame(x = rnorm(n),
                      w = seq(0.01,0.5,length.out = nTrials),
                      trial = 1:nTrials)

    dat$y <- dat$x * dat$w + rnorm(n)
    dat
  })
  doCorr <- reactive({
    dat <- doDat()
    res <- data.frame(trial=1:nTrials,corr=NA,pVal=NA)
    for(i in 1:nTrials){
      tmp <- cor.test(formula=~y+x,data=dat[dat$trial==i,])
      res$corr[i] <-tmp$estimate
      res$pVal[i] <-tmp$p.value
    }
    res  
  })

  output$datPlot <- renderPlot({
    dat <- doDat()
    p <- ggplot(data = dat,aes(x=x,y=y))
    p <- p + geom_point()
    p <- p + facet_wrap(~trial)
    p
  })
  #change this to produce bolded numbers rather than use the clunky *
  output$sigTable <- renderDT({
    res <- doCorr()
    res$corr <- round(res$corr,3)
    pcrit <- input$pcrit
    res$corr[res$pVal <= pcrit] <- paste(res$corr[res$pVal <= pcrit],
                                         "*",sep="")  
    datatable(res,rownames = FALSE) %>% 
      formatStyle('corr', fontWeight = styleInterval(input$pcrit, c('normal', 'bold'))) 
  })
}

shinyApp(ui = ui, server = server)

有关详细信息,请参阅本文档:https://rstudio.github.io/DT/