【问题标题】:printing multiple lines of renderText in Shiny在 Shiny 中打印多行 renderText
【发布时间】:2014-12-04 02:11:18
【问题描述】:

我正在尝试使用对 renderText 函数的两次连续调用在主页中打印两行反应值。我现在只看到第二个值。

如果我删除第二个 renderText 函数,程序会愉快地打印第一个值,但如果我将第二个 renderText 函数返回给代码,第一个值不会打印,第二个会打印。

我想要一个显示两个反应值(敏感性和特异性)的主页;一旦这个工作正常,我会抛出一些额外的情节。

这是错误的 render* 函数,还是错误的 pageWithSidebar() 函数?

这里是 UI.R 和 server.R 文件。

#ui.R

library(shiny)

# Define UI for application that draws a histogram
shinyUI(pageWithSidebar(
  headerPanel("Header"),
  mainPanel("Main"),

  # Sidebar with a slider input for the number of bins
  sidebarLayout(
    sidebarPanel(
      sliderInput("sensitivity",
                  "Sensitivity",
                  min = 0.0,
                  max = 1.0,
                  value = 0.8),
       sliderInput("specificity",
               "Specificity",
               min = 0.0,
               max = 1.0,
               value = 0.4)
    ),


    # Show a plot of the generated distribution
    mainPanel({
      textOutput("caption1")
      textOutput("caption2")
    })
  )
 )
)


and server.R

#server.R

library(shiny)

shinyServer(function(input, output) {
    output$caption1 <- renderText(input$sensitivity)
    output$caption2 <- renderText(input$specificity)
}
)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您对mainPanel() 的调用中的语法略有偏差。

    要修复它,请使用此

    # Show a plot of the generated distribution
    mainPanel(
      textOutput("caption1"),
      textOutput("caption2")
    )
    

    而不是这个

    # Show a plot of the generated distribution
    mainPanel({
      textOutput("caption1")
      textOutput("caption2")
    })
    

    您的版本的问题在于,在 R 中,每当计算大括号中的表达式时,只会返回其中最后一条语句的结果。您可以通过尝试以下方式看到更普遍的情况:

    {5
     6
     9}
    # [1] 9
    

    【讨论】:

    • 非常感谢!两个错误.. 在第一次和第二次 textOutput() 调用之间使用 {} 和失败。
    • @user2292410 很高兴有帮助。如果这里的答案解决了您的问题,请接受。这样其他人就会知道它已经解决了。或者,如果没有,请告诉我们它是如何失败的,以便人们可以更深入地挖掘。 (要接受,只需单击答案旁边的复选标记。如果愿意,您也可以投票(使用向上的箭头),尽管这不太重要。)
    猜你喜欢
    • 2017-06-19
    • 2022-11-19
    • 2020-10-28
    • 2013-11-17
    • 2017-02-20
    • 2018-06-09
    • 2014-09-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多