【问题标题】:Dynamic mathjax formula in ShinyShiny中的动态mathjax公式
【发布时间】:2018-07-05 16:43:46
【问题描述】:

我正在尝试根据输入动态更改闪亮应用程序中的公式。

目前我可以使用默认的起始设置构建公式,但是每当我更改输入时,公式都会返回到原始的 mathjax 输入,而不是显示公式。

是否可以保持公式动态但保持其呈现为公式?

请看下面的代码:

library(shiny)

ui <- fluidPage(
  fluidPage(
    titlePanel("T-test example"),
    fluidRow(
      column(3, wellPanel(
        sliderInput("mean1", "Group 1 mean:", 
                    min = 10, max = 1000, value = 200,
                    step = 10),
        sliderInput("sd1", "Group 1 variation:", 
                    min = 10, max = 100, value = 50,
                    step = 10),
        sliderInput("mean2", "Group 2 mean:", 
                    min = 10, max = 1000, value = 200,
                    step = 10),
        sliderInput("sd2", "Group 2 variation:", 
                    min = 10, max = 100, value = 50,
                    step = 10)
      )),
      column(6,
             plotOutput("plot1", width = 400, height = 300),
             br(),
             p(
               withMathJax(
                 "$$t = \\frac{\\bar{x}_1 - \\bar{x}_2}{\\sqrt{\\frac{s_1^2}{N_1}+\\frac{s_2^2}{N_2}}}$$"
                 )),
             textOutput("formula")

      )
    )
  )
)

server <- function(input, output, session) {

  output$plot1 <- renderPlot({
    x = rnorm(500, mean = input$mean1,
              sd = 20)
    y = rnorm(500, mean = input$mean2,
              sd = 20)
    boxplot(x,y)
  })

  output$formula <- reactive({
          str = sprintf("$$t = \\frac{%d - %d}{\\sqrt{\\frac{%d}{N_1}+\\frac{%d}{N_2}}}$$", 
                input$mean1, 
                input$mean2,
                input$sd1,
                input$sd2
                )
        str
  })

  observeEvent(input$change, 
               { 
              output$formula <- renderText({ 
                formula()
  })
  })

}

shinyApp(ui, server)

【问题讨论】:

    标签: r shiny mathjax


    【解决方案1】:

    一种可能的选择是不使用renderText,而是使用renderUI。此外,您使用observeEventreactive 的方式似乎有点不对劲。 reactive 不需要 observeEvent 来更新,因为如果其中一个输入自动更改,它就会失效。如果您想在某些事情发生变化时产生更大的影响,可以使用reactiveVal 而不是reactive

    无论如何,这是一个可行的示例,希望对您有所帮助!

    library(shiny)
    
    ui <- fluidPage(
      fluidPage(
        titlePanel("T-test example"),
        fluidRow(
          column(3, wellPanel(
            sliderInput("mean1", "Group 1 mean:", 
                        min = 10, max = 1000, value = 200,
                        step = 10),
            sliderInput("sd1", "Group 1 variation:", 
                        min = 10, max = 100, value = 50,
                        step = 10),
            sliderInput("mean2", "Group 2 mean:", 
                        min = 10, max = 1000, value = 200,
                        step = 10),
            sliderInput("sd2", "Group 2 variation:", 
                        min = 10, max = 100, value = 50,
                        step = 10)
          )),
          column(6,
                 plotOutput("plot1", width = 400, height = 300),
                 br(),
                 p(
                   withMathJax(
                     "$$t = \\frac{\\bar{x}_1 - \\bar{x}_2}{\\sqrt{\\frac{s_1^2}{N_1}+\\frac{s_2^2}{N_2}}}$$"
                   )),
                 uiOutput("formula")
    
          )
        )
      )
    )
    
    server <- function(input, output, session) {
    
      output$plot1 <- renderPlot({
        x = rnorm(500, mean = input$mean1,
                  sd = 20)
        y = rnorm(500, mean = input$mean2,
                  sd = 20)
        boxplot(x,y)
      })
    
      output$formula <- renderUI({ 
        p(
          withMathJax(sprintf("$$t = \\frac{%d - %d}{\\sqrt{\\frac{%d}{N_1}+\\frac{%d}{N_2}}}$$", 
                              input$mean1, 
                              input$mean2,
                              input$sd1,
                              input$sd2
          )))
      })
    
    
    }
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 2017-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-26
      • 2014-10-08
      • 1970-01-01
      • 1970-01-01
      • 2019-07-12
      相关资源
      最近更新 更多