Shiny 中的动态 mathjax 公式
Dynamic mathjax formula in Shiny
我正在尝试根据输入动态更改闪亮应用程序中的公式。
目前我可以使用默认的起始设置构建公式,但每当我更改输入时,公式都会返回到原始 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)
一个可能的选择是不使用 renderText
,而是使用 renderUI
。另外,您使用 observeEvent
和 reactive
的方式似乎有点不对劲。 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)
我正在尝试根据输入动态更改闪亮应用程序中的公式。
目前我可以使用默认的起始设置构建公式,但每当我更改输入时,公式都会返回到原始 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)
一个可能的选择是不使用 renderText
,而是使用 renderUI
。另外,您使用 observeEvent
和 reactive
的方式似乎有点不对劲。 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)