Renderplot和renderdatatable在同一输出页面

时间:2019-09-05 04:39:46

标签: r shiny

我在执行此代码时遇到问题。逻辑运行良好。单击“数据集下的因子”下的“值”时,将正确显示图。但是,当您单击“数据集下的因子”下的“数据”表时,该表将显示在底部。我可以将其显示在较早显示绘图的顶部吗?

df <- structure(list(A = structure(c(1L, 4L, 6L, 1L, 8L, 2L, 7L, 3L, 
                             5L, 5L, 1L, 8L, 2L, 7L, 2L), .Label = 
     c("asd", "dfg", "fgdsgd", 

     "fsd", "gdfgd", "gs", "sdfg", "sf"), class = 
   "factor"), B = c(29L, 

     24L, 
  46L, 50L, 43L, 29L, 32L, 24L, 35L, 39L, 33L, 47L, 53L, 26L, 

   31L), 
    C = structure(c(8L, 5L, 1L, 6L, 3L, 2L, 9L, 7L, 6L, 3L, 

   2L, 9L, 8L, 8L, 4L), .Label = c("asd", "er", "fg", "gf", "gfd", 

    "gfg", "qw", "sf", "tr"), class = "factor"), D = c(36L, 56L, 

  39L, 26L, 56L, 35L, 27L, 31L, 33L, 45L, 34L, 27L, 43L, 40L, 56L

  ), E = structure(c(9L, 4L, 3L, 4L, 2L, 7L, 10L, 8L, 6L, 2L, 1L, 

   10L, 9L, 9L, 5L), .Label = c("er", "fg", "g", "gd", "gf", "gfg", 

 "gtd", "qw", "sf", "tr"), class = "factor"), F = c(44L, 34L,  

    37L, 23L, 37L, 51L, 28L, 36L, 33L, 31L, 39L, 43L, 25L, 37L, 43L

   ), num = 1:15), row.names = c(NA, -15L), class = "data.frame")

theNames <- names(df)
  MyList  <- vector(mode = "list")
  for(i in theNames){
  MyList[[i]] <- df[,i]
}

library(ggplot2)
library(dplyr)
library(shiny)

ui <- fluidPage(
  tabsetPanel(tabPanel(
  "Factor_Bivariate_Analysis",
   sidebarLayout(sidebarPanel(
   fluidRow(
      column(h6(
  selectInput("se4", "Factors under the
              datasets", choices = c("", "Values","Data table"))
  ), width = 5, offset =
    0),
br(),
column(h6(
  actionButton("Val", "See the Values", width =
                 200, offset =
                 -1)
), width = 5, offset = 0),
br(),
column(h6(selectInput(
  "state", "Filters", choices = c("",MyList)
)), width = 5, offset = 0)
), width =
 1000
 ),
  mainPanel(
  h5(plotOutput(
  "Plot4", width = "1000px", height =
    "1000px"
), width = 1000), h5(dataTableOutput("Plot5"), width = 1000)
 ))
 )))


 server <- function(input, output, session) {
   f_data <- reactive({
     wanted_case <- input$state
     cat("selected case ", wanted_case, "\n\n")
     if (wanted_case == ""){
     fd <- df
  } else {
   fd <- df %>% filter_if(.predicate = is.factor,.vars_predicate = any_vars (. == 
                                                                          wanted_case))
print(fd)
  }
  return(fd)
   }) 
    Plot4 <- reactive({
     if (input$se4 == "Values") {
       print(ggplot(data = 
             f_data(),aes(x=num,y=B,fill=A))+geom_line()+facet_wrap("A",ncol=1,nrow=8, 
   scales = 
                                                                      "free"))
} else if (input$se4 == "NULL") {
 ""
 }
  })
    output$Plot4 <- renderPlot({
Plot4()
   })
   Plot5 <- reactive({
if (input$se4 == "Data table") {
print(data.frame(df))
} else if (input$se4 == "NULL") {
""
   }
       })
     output$Plot5 <- renderDataTable({
Plot5()
})
}

 shinyApp(ui, server)

1 个答案:

答案 0 :(得分:0)

您可以结合使用uiOutputrenderUI来做到这一点。

df <- structure(list(A = structure(c(1L, 4L, 6L, 1L, 8L, 2L, 7L, 3L, 
                                     5L, 5L, 1L, 8L, 2L, 7L, 2L), .Label = 
                                     c("asd", "dfg", "fgdsgd", 

                                       "fsd", "gdfgd", "gs", "sdfg", "sf"), class = 
                                     "factor"), B = c(29L, 

                                                      24L, 
                                                      46L, 50L, 43L, 29L, 32L, 24L, 35L, 39L, 33L, 47L, 53L, 26L, 

                                                      31L), 
                     C = structure(c(8L, 5L, 1L, 6L, 3L, 2L, 9L, 7L, 6L, 3L, 

                                     2L, 9L, 8L, 8L, 4L), .Label = c("asd", "er", "fg", "gf", "gfd", 

                                                                     "gfg", "qw", "sf", "tr"), class = "factor"), D = c(36L, 56L, 

                                                                                                                        39L, 26L, 56L, 35L, 27L, 31L, 33L, 45L, 34L, 27L, 43L, 40L, 56L

                                                                     ), E = structure(c(9L, 4L, 3L, 4L, 2L, 7L, 10L, 8L, 6L, 2L, 1L, 

                                                                                        10L, 9L, 9L, 5L), .Label = c("er", "fg", "g", "gd", "gf", "gfg", 

                                                                                                                     "gtd", "qw", "sf", "tr"), class = "factor"), F = c(44L, 34L,  

                                                                                                                                                                        37L, 23L, 37L, 51L, 28L, 36L, 33L, 31L, 39L, 43L, 25L, 37L, 43L

                                                                                                                     ), num = 1:15), row.names = c(NA, -15L), class = "data.frame")

theNames <- names(df)
MyList  <- vector(mode = "list")
for(i in theNames){
  MyList[[i]] <- df[,i]
}

library(ggplot2)
library(dplyr)
library(shiny)

ui <- fluidPage(
  tabsetPanel(tabPanel(
    "Factor_Bivariate_Analysis",
    sidebarLayout(sidebarPanel(
      fluidRow(
        column(h6(
          selectInput("se4", "Factors under the
                      datasets", choices = c("", "Values","Data table"))
          ), width = 5, offset =
            0),
        br(),
        column(h6(
          actionButton("Val", "See the Values", width =
                         200, offset =
                         -1)
        ), width = 5, offset = 0),
        br(),
        column(h6(selectInput(
          "state", "Filters", choices = c("",MyList)
        )), width = 5, offset = 0)
        ), width =
        1000
      ),
      mainPanel(uiOutput("plots")
      ))
  )))


server <- function(input, output, session) {
  f_data <- reactive({
    wanted_case <- input$state
    cat("selected case ", wanted_case, "\n\n")
    if (wanted_case == ""){
      fd <- df
    } else {
      fd <- df %>% filter_if(.predicate = is.factor,.vars_predicate = any_vars (. == 
                                                                                  wanted_case))
      print(fd)
    }
    return(fd)
  })

  output$plots <- renderUI({
    req(input$se4)
    if(input$se4 == "Values"){
      h5(plotOutput(
           "Plot4", width = "1000px", height =
             "1000px"
         ), width = 1000)
    }else if(input$se4 == "Data table"){
      h5(dataTableOutput("Plot5"), width = 1000)
    }
  })

  output$Plot4 <- renderPlot({
    req(input$se4,f_data())
    if (input$se4 == "Values") {
      print(ggplot(data = 
                     f_data(),aes(x=num,y=B,fill=A))+geom_line()+facet_wrap("A",ncol=1,nrow=8, 
                                                                            scales = 
                                                                              "free"))
    }
  })

  output$Plot5 <- renderDataTable({
    req(input$se4)
    if (input$se4 == "Data table") {
      data.frame(df)
    }
  })

}

shinyApp(ui, server)