【问题标题】:R Shiny error when using renderText() with testOutput()将renderText()与testOutput()一起使用时出现R Shiny错误
【发布时间】:2017-01-25 04:21:52
【问题描述】:

我正在 R Shiny 中开发一个应用程序,用户可以在其中输入或选择文本,然后用于填充报告,然后可以将其保存为文档。文本应如下所示:

% 标题 % 创建者 % Created 2016-09-16 #

我的 ui.R 代码:

library(shiny)
library(hash)
shinyUI(mainPanel(
    textInput("report_author", label="Author")))

server.R 代码:

library(shiny)
source("generate_report.R", local=TRUE)  
shinyServer(function(input, output) {
output$final_report <- reactive({
    input_report_author <- input$report_author
    input_report_year = ' ' 
    input_report_month = ' ' 
    input_report_panel = ' '  
    renderText({
      make_report(
        author=input_report_author,
        year=input_report_year,
        month=input_report_month,
        panel=input_report_panel)})})})

和 generate_report.R:

make_report <- function(panel, author, year, month, date=defaultDate) {
  author = toString(author)
  report_text = paste(
    "% Title\n",
    "% Created by ", author, "\n",
    "% Created ", date, "\n\n",       
    "#", panel, "\n\n",
     sep="")
  return(report_text)}

返回的文本不是我想要的:

结构(函数(...),{,如果(长度(输出参数)!= 0 && !hasExecuted$get()) {, warning("未使用的参数:outputArgs。 参数 outputArgs 只是 ", , ",在嵌入时使用 ", , "R Markdown 代码块中 Shiny 代码的 sn-ps (使用 运行时:闪亮)。运行“, ,”完整的 Shiny 应用程序时,请设置 直接在",,"对应的输出函数中输出参数 你的 UI 代码。"), hasExecuted$set(TRUE), }, if (is.null(formals(origRenderFunc))) , origRenderFunc(), 否则 origRenderFunc(...),}, class= "函数", outputFunc = 函数 (outputId, container = if (inline) span else div, , inline = FALSE) ,{, 容器(id = outputId, class= "闪亮文本输出"),}, outputArgs = list(), hasExecuted = )

如果我替换 - input$report_author' with a string,,我得到同样的错误,所以错误很可能在代码的 output$final_report&lt;-reactive({..}) 部分。

如果我尝试在没有 reactive({}) 包装器的情况下运行应用程序,我会在 input_report_author=input$report_author 代码段处收到错误:

.getReactiveEnvironment()$currentContext() 中的错误:操作不正确 在没有主动响应上下文的情况下允许。

代码:

input_report_author = input$report_author
input_report_year = ' ' 
input_report_month = ' ' 
input_report_panel = ' ' 
output$final_report <- renderText({
    make_report(author=input_report_author, year=input_report_year,
        month=input_report_month, panel=input_report_panel)})

谁能告诉我如何将文本传递给闪亮的应用程序并让它返回一个自定义字符串?

【问题讨论】:

  • 您应该将input$report_author 放在renderText 中,这是一个反应式表达式。在这种情况下,您不需要将其放在 reactive 函数中。

标签: r shiny reactive-programming


【解决方案1】:

正如@Geovany 所说,您不必将renderText 包裹在reactive 中。以下单文件应用程序适用于我:

library(shiny)

make_report <- function(panel, author, year, month, date=Sys.Date()) {
  author = toString(author)
  report_text = paste(
    "% Title\n",
    "% Created by ", author, "\n",
    "% Created ", date, "\n\n",       
    "#", panel, "\n\n",
     sep="")
  return(report_text)}

ui <- fluidPage(mainPanel(
  textInput("report_author", label="Author"),
  textOutput("final_report")
  )
)

server <- function(input, output) {

  output$final_report <- renderText({
    input_report_author <- input$report_author
    input_report_year = ' ' 
    input_report_month = ' ' 
    input_report_panel = ' '        
    make_report(
      author=input_report_author,
      year=input_report_year,
      month=input_report_month,
      panel=input_report_panel)}
    )
}

shinyApp(ui = ui, server = server)

输出:

【讨论】:

    猜你喜欢
    • 2021-04-16
    • 2015-03-02
    • 2020-07-25
    • 2018-05-13
    • 2021-06-25
    • 2018-06-16
    • 1970-01-01
    • 2020-07-03
    • 2018-11-18
    相关资源
    最近更新 更多