用于闪亮表格中数学模式的外部 js
External js for math mode in shiny tables
此问题与 有关。
编辑
我重新表述了问题以在更复杂的应用程序中重现该问题。
我正在尝试在 table 中包含数学模式。 @Stéphane Laurent 在 EDIT 2 中使用 katex 的解决方案效果很好。我编辑了代码,因为我的应用程序包含许多 table,其名称包括字符串 coef_
。
library(shiny)
js <- "
$(document).on('shiny:value', function(event) {
if(event.name.indexOf(event.name.match(/\b\w*coef_\w+\b/g)) > -1){
if(event.value.match(/(%%+[^%]+%%)/g) !== null) {
var matches = event.value.match(/(%%+[^%]+%%)/g);
var newvalue = event.value;
for(var i=0; i<matches.length; i++){
var code = '\\' + matches[i].slice(2,-2);
newvalue = newvalue.replace(matches[i], katex.renderToString(code));
}
event.value = newvalue;
} else {
event.value;
}
}
});
"
如果 table 名称不包含字符串 coef_
或 table 名称包含字符串 coef_
不包含 [=16= 的术语], js
应该不会有影响吧。
# UI 1
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(
numericInput("mean", "Enter mean", value = 1),
tableOutput("coef_table1"),
tableOutput("coef_table2"),
tableOutput("table")
))
# SERVER
server <- function(input, output) {
output$table <- renderTable({
x <- rnorm(2)
y <- rnorm(2, input$mean)
tab <- data.frame(x = x, y = y, z = c("hello", "%%gamma%%%%delta%%"))
rownames(tab) <- c("%%alpha%%", "%%beta%%")
tab
}, rownames = TRUE)
}
但是,当我用 js
变量中的代码创建 js 文件,将其保存在 www
文件夹中并加载它时,它不起作用:
# UI 2
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(src="math_in_tables.js")
),
titlePanel("Hello Shiny!"),
mainPanel(
numericInput("mean", "Enter mean", value = 1),
tableOutput("coef_table1"),
tableOutput("coef_table2"),
tableOutput("table")
))
数学模式不再适用于第一个 table。我在这里错过了什么?浏览器没有错误。
我认为if(event.name.indexOf(event.name.match(/\b\w*coef_\w+\b/g)) > -1)
不正确。
想要测试event.name
是否包含字符串coef_
。我不擅长正则表达式,但这应该有用:
if((/\b\w*coef_\w*\b/g).test(event.name)){ ...
如果将 JS 代码放在外部文件中,请使用单反斜杠:
if((/\b\w*coef_\w*\b/g).test(event.name)){ ...
(和 var code = '\' + matches[i].slice(2,-2);
)。
此问题与
编辑
我重新表述了问题以在更复杂的应用程序中重现该问题。
我正在尝试在 table 中包含数学模式。 @Stéphane Laurent 在 EDIT 2 中使用 katex 的解决方案效果很好。我编辑了代码,因为我的应用程序包含许多 table,其名称包括字符串 coef_
。
library(shiny)
js <- "
$(document).on('shiny:value', function(event) {
if(event.name.indexOf(event.name.match(/\b\w*coef_\w+\b/g)) > -1){
if(event.value.match(/(%%+[^%]+%%)/g) !== null) {
var matches = event.value.match(/(%%+[^%]+%%)/g);
var newvalue = event.value;
for(var i=0; i<matches.length; i++){
var code = '\\' + matches[i].slice(2,-2);
newvalue = newvalue.replace(matches[i], katex.renderToString(code));
}
event.value = newvalue;
} else {
event.value;
}
}
});
"
如果 table 名称不包含字符串 coef_
或 table 名称包含字符串 coef_
不包含 [=16= 的术语], js
应该不会有影响吧。
# UI 1
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(
numericInput("mean", "Enter mean", value = 1),
tableOutput("coef_table1"),
tableOutput("coef_table2"),
tableOutput("table")
))
# SERVER
server <- function(input, output) {
output$table <- renderTable({
x <- rnorm(2)
y <- rnorm(2, input$mean)
tab <- data.frame(x = x, y = y, z = c("hello", "%%gamma%%%%delta%%"))
rownames(tab) <- c("%%alpha%%", "%%beta%%")
tab
}, rownames = TRUE)
}
但是,当我用 js
变量中的代码创建 js 文件,将其保存在 www
文件夹中并加载它时,它不起作用:
# UI 2
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(src="math_in_tables.js")
),
titlePanel("Hello Shiny!"),
mainPanel(
numericInput("mean", "Enter mean", value = 1),
tableOutput("coef_table1"),
tableOutput("coef_table2"),
tableOutput("table")
))
数学模式不再适用于第一个 table。我在这里错过了什么?浏览器没有错误。
我认为if(event.name.indexOf(event.name.match(/\b\w*coef_\w+\b/g)) > -1)
不正确。
想要测试event.name
是否包含字符串coef_
。我不擅长正则表达式,但这应该有用:
if((/\b\w*coef_\w*\b/g).test(event.name)){ ...
如果将 JS 代码放在外部文件中,请使用单反斜杠:
if((/\b\w*coef_\w*\b/g).test(event.name)){ ...
(和 var code = '\' + matches[i].slice(2,-2);
)。