gpt4 book ai didi

html - 隐藏 R 中 Shiny 应用程序的 renderPrint 预标记输出

转载 作者:搜寻专家 更新时间:2023-10-31 21:50:32 29 4
gpt4 key购买 nike

我正在用 R 开发一个 Shiny 的应用程序。对于某些 renderPrint 输出,我希望 Shiny 的用户界面不显示任何内容。有点像下面显示的 HTML5 示例中 pre 或 div 标签的隐藏选项:

http://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_global_hidden

下面是我 Shiny 的示例代码。简要说明:您可以使用下拉菜单选择两个变量(因子变量或连续变量)之一。如果您选择因子变量,我想显示标题和 table 输出。如果您选择连续变量,我不想看到任何东西。现在,如果您插入空白字符串 "" 作为对 renderText 的返回,标题就会消失。但是,我不知道如何让 renderPrint 不显示任何内容。我试过:

  1. “”。不起作用,因为它返回实际的空白字符串
  2. NULL。不起作用,因为它返回字符串 NULL
  3. 不可见()。目前最好,但仍然无法正常工作,因为它返回灰色格式的框。

目标是不显示任何内容。 Shiny 的 ui.r 和 server.r 代码如下:图书馆( Shiny )

##
## Start shiny UI here
##
shinyUI(pageWithSidebar(
headerPanel("Shiny Example"),
sidebarPanel(
wellPanel(
selectInput( inputId = "variable1",label = "Select First Variable:",
choices = c("Binary Variable 1" = "binary1",
"Continuous Variable 1" = "cont1"),
selected = "Binary Variable 1"
)
)


),
mainPanel(
h5(textOutput("caption2")),
verbatimTextOutput("out2")
)
))

##
## Start shiny server file and simulated data here
##
binary1 <- rbinom(100,1,0.5)
cont1 <- rnorm(100)
dat <- as.data.frame(cbind(binary1, cont1))
dat$binary1 <- as.factor(dat$binary1)
dat$cont1 <- as.numeric(dat$cont1)

library(shiny)

shinyServer(function(input, output) {

inputVar1 <- reactive({
parse(text=sub(" ","",paste("dat$", input$variable1)))
})

output$caption2 <- renderText({
if ( (is.factor(eval(inputVar1()))==TRUE) ) {
caption2 <- "Univariate Table"
} else {
if ( (is.numeric(eval(inputVar1()))==TRUE) ) {
caption2 <- ""
}
}
})

output$out2 <- renderPrint({
if ( (is.factor(eval(inputVar1()))==TRUE) ) {
table(eval(inputVar1()))
} else {
if ( (is.numeric(eval(inputVar1()))==TRUE) ) {
invisible()
}
}
})
})

几个问题...

  1. 为什么 renderText 处理隐藏/不可见显示的方式与 renderPrint 不同?是不是因为前者把文本输出为pre tag;然而,后者在 div 标签中显示格式化输出?
  2. 致那些 HTML 专家(前面,我不是专家)...什么选项最好让我的输出不显示任何内容?隐藏选项最好嵌入到 pre 或 div 标签中吗(我知道它在 IE 浏览器中不起作用)。我应该试试别的吗? CSS 选项等?
  3. 假设隐藏是最好的方法(或者我得到上面 2. 的答案),我如何通过 shiny 中的 renderPrint 函数传递这个选项/参数?还是我需要使用不同的 shiny 函数来实现此功能?

顺便说一句...我的 R 版本是:version.string R version 3.0.1 (2013-05-16) 我正在使用 shiny version {R package version 0.6.0 }。在此先感谢您的帮助。

最佳答案

我不确定我是否理解您的问题,但请尝试以下操作:首先是 Ui:

library(shiny)
ui <- fluidPage(pageWithSidebar(
headerPanel("Shiny Example"),
sidebarPanel(
wellPanel(
selectInput(inputId = "variable1",label = "Select First Variable:",
choices = c("Binary Variable 1" = "binary1",
"Continuous Variable 1" = "cont1"),
selected = "Binary Variable 1"
)
)
),
mainPanel(
h5(textOutput("caption2")),
verbatimTextOutput("out2", placeholder=TRUE)
)
))

在此处启动 Shiny 的服务器文件和模拟数据:

binary1 <- rbinom(100,1,0.5)
cont1 <- rnorm(100)
dat <- as.data.frame(cbind(binary1, cont1))
dat$binary1 <- as.factor(dat$binary1)
dat$cont1 <- as.numeric(dat$cont1)

server <- (function(input, output) {
inputVar1 <- reactive({
parse(text=sub(" ","",paste("dat$", input$variable1)))
})

output$caption2 <- renderText({
if ((is.factor(eval(inputVar1()))==TRUE) ) {
caption2 <- "Univariate Table"
} else {
if ((is.numeric(eval(inputVar1()))==TRUE) ) {
caption2 <- "Continous"
}
}
})

output$out2 <- renderPrint({
if ((is.factor(eval(inputVar1()))==TRUE) ) {table(eval(inputVar1()))}
})
})

最后:

shinyApp(ui = ui, server = server)

关于html - 隐藏 R 中 Shiny 应用程序的 renderPrint 预标记输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19123576/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com