闪亮的R是否可能具有不同类型的输出?

时间:2018-06-23 01:56:24

标签: r shiny multipleoutputs

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

所以我在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()
})

这在用户界面中

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

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

有什么方法可以使文本和barplot显示在同一页面上?

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

或其他任何方式?

我一直在想一些解决方案,例如划分 desc(),使其具有两个输出,即text和barplot。但是,如果有一种方法可以一次性完成,那么我非常想学习这种方式。

1 个答案:

答案 0 :(得分:0)

当然,有很多不同的输出类型很常见。请参阅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")
    }

  })
}

enter image description here