【问题标题】:Is it possible to have different types of output in shiny R?在闪亮的 R 中是否可以有不同类型的输出?
【发布时间】:2018-06-23 01:56:23
【问题描述】:

我一直在想这个问题是否有答案。

所以我在 server.R 中有这段代码

desc <- reactive({

  for (i in 1:input$txt1){
    print(paste("Cluster ke", i))
    clust <- ensemble()

    newdata <- clust[which(clust$Cluster==i), 1]
    print(paste(newdata))

    barm <- vector("numeric")
    x <- ncol(clust)-2

    for (j in 2:x){
      datdesc <- clust[which(clust$Cluster==i), j]
      m <- mean(datdesc)
      colnam <- colnames(clust[j])
      print(paste("Rata-rata produktivitas", colnam,":", m))

      for(k in j-1){
        barm[k] <- m
      }
    }
    print(paste(barm))
    barplot(barm)
  }
})

output$desc <- renderPrint({
    desc()
})

这个在 ui 中

conditionalPanel(condition="input.choice==3", verbatimTextOutput("desc"))

到目前为止,我可以获得我想要的所有输出,描述性文本和条形图。但是,条形图显示在 R 控制台而不是浏览器上。

有什么方法可以让文本和条形图显示在同一页面上?

我可以使用 renderPrintverbatimTextOutput 的其他功能吗?

或者其他方式?

我一直在想一些解决方案,比如将 desc() 分开,使其有两个输出,文本和条形图。但如果有一种方法可以一次性完成,我非常想学习这种方法。

【问题讨论】:

    标签: r shiny multipleoutputs


    【解决方案1】:

    当然,有许多不同的输出类型是很常见的。有关更多示例,请参阅 Shiny 库。

    根据您提供的代码和模板:

    UI.R

    bootstrapPage(
    
      selectInput(
        "choice", "txt1 - descriptive text?",
        c(my_text = "my_text",
          your_text = "your_text")),
    
      selectInput(inputId = "n_breaks",
                  label = "Number of bins in histogram (approximate):",
                  choices = c(10, 20, 35, 50),
                  selected = 20),
    
      checkboxInput(inputId = "individual_obs",
                    label = strong("Show individual observations"),
                    value = FALSE),
    
      checkboxInput(inputId = "density",
                    label = strong("Show density estimate"),
                    value = FALSE),
    
      plotOutput(outputId = "main_plot", height = "300px"),
    
      conditionalPanel(condition="input.choice=='my_text'", verbatimTextOutput("desc"))
    
    
    )
    

    Server.R

    function(input, output) {
      desc <- reactive({
        "some text goes here, I guess!"
    
      })
    
      output$desc <- renderPrint({
        desc()
      })
    
      output$main_plot <- renderPlot({
    
        hist(faithful$eruptions,
             probability = TRUE,
             breaks = as.numeric(input$n_breaks),
             xlab = "Pandjie Soerja",
             main = "Pandjie Soerja")
    
        if (input$individual_obs) {
          rug(faithful$eruptions)
        }
    
        if (input$density) {
          dens <- density(faithful$eruptions,
                          adjust = input$bw_adjust)
          lines(dens, col = "blue")
        }
    
      })
    }
    

    【讨论】:

    • 非常感谢。下火车后我会试试的。所以,我假设我也可以设置任何我想要的顺序,对吧?像文本 > 绘图 > 文本 > 绘图等...因为如您所见,我使用循环来生成输出。
    • @psdm 不客气,是的,你可以。任何顺序从上到下和从左到右。你知道它非常灵活——你可以做任何网站可以做的事情。你应该看看这里的很多例子shiny.rstudio.com/gallery
    猜你喜欢
    • 2017-01-12
    • 2021-12-24
    • 2021-09-02
    • 2014-08-30
    • 2014-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多