如何在 Shiny 中将列名设置为 data.frame 中的数学公式?
How to set column names as mathematical formulas in data.frame in Shiny?
我是 Shiny 的新手,我 运行 遇到以下问题:
我想以数学形式输出列名,这意味着,在下面的table中,y1---> y 1......和y_--->?。不知道这样行不行,我用了很多方法,还没找到更好的。
我的部分代码如下:
yValues <- reactive({
data.frame(
Group=c("1"),
y1=as.character(c(input$meansy1)),
y2=as.character(c(input$meansy2)),
y3=as.character(c(input$meansy3)),
y4=as.character(c(input$meansy4)),
y5=as.character(c(input$meansy5)),
y6=as.character(c(input$meansy6)),
y_=as.character(c(y_mean())),
y.Variance=as.character(c(y_Variance())),
stringsAsFactors=FALSE)
})
现在是table:
这是 KaTeX 的一个方法。将你的数学代码放在两对 %%
.
之间
library(shiny)
js <- "
$(document).on('shiny:value', function(event) {
if(event.name === 'table'){
var matches = event.value.match(/(%%+[^%]+%%)/g);
var newvalue = event.value;
for(var i=0; i<matches.length; i++){
var kcode;
var x = matches[i].slice(2,-2);
try{
var code = '\\' + x;
kcode = katex.renderToString(code)
} catch(error){
kcode = katex.renderToString(x)
}
newvalue = newvalue.replace(matches[i], kcode);
}
event.value = newvalue;
}
})
"
ui <- fluidPage(
tags$head(
tags$link(rel="stylesheet", href="https://cdn.jsdelivr.net/npm/katex@0.10.0-beta/dist/katex.min.css", integrity="sha384-9tPv11A+glH/on/wEu99NVwDPwkMQESOocs/ZGXPoIiLE8MU/qkqUcZ3zzL+6DuH", crossorigin="anonymous"),
tags$script(src="https://cdn.jsdelivr.net/npm/katex@0.10.0-beta/dist/katex.min.js", integrity="sha384-U8Vrjwb8fuHMt6ewaCy8uqeUXv4oitYACKdB0VziCerzt011iQ/0TqlSlv8MReCm", crossorigin="anonymous"),
tags$script(HTML(js))
),
titlePanel("Hello Shiny!"),
mainPanel(
tableOutput("table")
)
)
server <- function(input, output) {
yValues <- reactive({
data.frame(
Group = "1",
"%%y_1%%" = LETTERS[1:3],
"%%bar{y}%%" = letters[1:3],
stringsAsFactors = FALSE,
check.names = FALSE)
})
output[["table"]] <- renderTable({
yValues()
}, rownames = TRUE)
}
shinyApp(ui, server)
我是 Shiny 的新手,我 运行 遇到以下问题:
我想以数学形式输出列名,这意味着,在下面的table中,y1---> y 1......和y_--->?。不知道这样行不行,我用了很多方法,还没找到更好的。
我的部分代码如下:
yValues <- reactive({
data.frame(
Group=c("1"),
y1=as.character(c(input$meansy1)),
y2=as.character(c(input$meansy2)),
y3=as.character(c(input$meansy3)),
y4=as.character(c(input$meansy4)),
y5=as.character(c(input$meansy5)),
y6=as.character(c(input$meansy6)),
y_=as.character(c(y_mean())),
y.Variance=as.character(c(y_Variance())),
stringsAsFactors=FALSE)
})
现在是table:
这是 KaTeX 的一个方法。将你的数学代码放在两对 %%
.
library(shiny)
js <- "
$(document).on('shiny:value', function(event) {
if(event.name === 'table'){
var matches = event.value.match(/(%%+[^%]+%%)/g);
var newvalue = event.value;
for(var i=0; i<matches.length; i++){
var kcode;
var x = matches[i].slice(2,-2);
try{
var code = '\\' + x;
kcode = katex.renderToString(code)
} catch(error){
kcode = katex.renderToString(x)
}
newvalue = newvalue.replace(matches[i], kcode);
}
event.value = newvalue;
}
})
"
ui <- fluidPage(
tags$head(
tags$link(rel="stylesheet", href="https://cdn.jsdelivr.net/npm/katex@0.10.0-beta/dist/katex.min.css", integrity="sha384-9tPv11A+glH/on/wEu99NVwDPwkMQESOocs/ZGXPoIiLE8MU/qkqUcZ3zzL+6DuH", crossorigin="anonymous"),
tags$script(src="https://cdn.jsdelivr.net/npm/katex@0.10.0-beta/dist/katex.min.js", integrity="sha384-U8Vrjwb8fuHMt6ewaCy8uqeUXv4oitYACKdB0VziCerzt011iQ/0TqlSlv8MReCm", crossorigin="anonymous"),
tags$script(HTML(js))
),
titlePanel("Hello Shiny!"),
mainPanel(
tableOutput("table")
)
)
server <- function(input, output) {
yValues <- reactive({
data.frame(
Group = "1",
"%%y_1%%" = LETTERS[1:3],
"%%bar{y}%%" = letters[1:3],
stringsAsFactors = FALSE,
check.names = FALSE)
})
output[["table"]] <- renderTable({
yValues()
}, rownames = TRUE)
}
shinyApp(ui, server)