R 如何通过传递环境变量来呈现模块中的代码?
我试图通过闪亮的模块从代码输入呈现UI。但我不知道为什么它不起作用。没有错误,因此很难理解反应性在哪里断裂 代码R 如何通过传递环境变量来呈现模块中的代码?,r,shiny,R,Shiny,我试图通过闪亮的模块从代码输入呈现UI。但我不知道为什么它不起作用。没有错误,因此很难理解反应性在哪里断裂 代码 库(shinyAce) 库(日志) reactlog_enable() codeUI您遇到了几个问题: 在div中,id必须是add_here,而不是#add_here。insertUI中的#用于jQuery env是正常变量,不是函数。所以它是env而不是env() 调用模块时,使用code=input$code。这意味着你通过了一个被评估的反应,所以它不再是反应。因此,您需要在
库(shinyAce)
库(日志)
reactlog_enable()
codeUI您遇到了几个问题:
- 在
div
中,id
必须是add_here
,而不是#add_here
。insertUI
中的#
用于jQuery
env
是正常变量,不是函数。所以它是env
而不是env()
- 调用模块时,使用
code=input$code
。这意味着你通过了一个被评估的反应,所以它不再是反应。因此,您需要在模块中使用code
而不是code()
库(shinyAce)
图书馆(shinyjs)
图书馆(闪亮)
科迪
library(shinyAce)
library(reactlog)
reactlog_enable()
codeUI <- function(id) {
ns <- NS(id)
tagList(htmlOutput(ns("output")))
}
codeSE <- function(id, active_id, code, env) {
moduleServer(id,
function(input, output, session) {
output$output <- renderUI({
req(id == active_id(), cancelOutput = TRUE)
eval()
eval_code <- paste0("\n```{r echo = TRUE, comment = NA}\n", code(), "\n```\n")
HTML(knitr::knit2html(text = eval_code, fragment.only = TRUE, quiet = TRUE, env = env()))
})
})
}
ui <- fluidPage(
htmlOutput("output"),
aceEditor("code", mode = "r", height = "50px"),
actionButton("eval", "Evaluate"),
div(id = "#add_here")
)
env <- environment()
server <- function(input, output, session) {
counter <- 1
active_id <- reactiveVal()
observeEvent(input$eval, {
req(code)
current_id <- paste0("out_", counter)
active_id(current_id)
codeSE(id = current_id, active_id = active_id, code = input$code, env = env)
insertUI(selector = "#add_here",ui = codeUI(current_id))
counter <<- counter + 1
runjs('
document.getElementById("end").scrollIntoView();
')
}) }
shinyApp(ui, server)
library(shinyAce)
library(shinyjs)
library(shiny)
codeUI <- function(id) {
ns <- NS(id)
tagList(htmlOutput(ns("output")))
}
codeSE <- function(id, active_id, code, env) {
moduleServer(id,
function(input, output, session) {
output$output <- renderUI({
req(id == active_id(), cancelOutput = TRUE)
eval_code <- paste0("\n```{r echo = TRUE, comment = NA}\n", code, "\n```\n")
HTML(knitr::knit2html(text = eval_code, fragment.only = TRUE, quiet = TRUE, envir = env))
})
})
}
ui <- fluidPage(
htmlOutput("output"),
aceEditor("code", mode = "r", height = "50px"),
actionButton("eval", "Evaluate"),
div(id = "add_here")
)
env <- environment()
server <- function(input, output, session) {
counter <- 1
active_id <- reactiveVal()
observeEvent(input$eval, {
req(code)
current_id <- paste0("out_", counter)
active_id(current_id)
codeSE(id = current_id, active_id = active_id, code = input$code, env = env)
insertUI(selector = "#add_here",ui = codeUI(current_id))
counter <<- counter + 1
runjs('
document.getElementById("end").scrollIntoView();
')
}) }
shinyApp(ui, server)