R语言 shiny包中的交互调用函数renderUI

2021-05-31 16:22:25 浏览数 (3)

本次展示shiny的功能有:

1、读取本地数据;

2、交互展示数据(view)

3、动态交互作图(自动读取上传数据的列名)

体验网址:https://yanshenli.shinyapps.io/Desktop/

代码语言:javascript复制
library(shiny)
library(ggplot2)

ui <- fluidPage(
  navbarPage("User Interface:",tabPanel("Upload",
                                        titlePanel("Uploading Files"),
                                        sidebarLayout(
                                          sidebarPanel(
                                            fileInput("file1", "Choose CSV File",
                                                      multiple = TRUE,
                                                      accept = c("text/csv",
                                                                 "text/comma-separated-values,text/plain",
                                                                 ".csv")),
                                            tags$hr(),
                                            checkboxInput("header", "Header", TRUE),
                                            radioButtons("sep", "Separator",
                                                         choices = c(Comma = ",",
                                                                     Semicolon = ";",
                                                                     Tab = "t"),
                                                         selected = ","),
                                            tags$hr(),
                                            radioButtons("disp", "Display",
                                                         choices = c(Head = "head",
                                                                     All = "all"),
                                                         selected = "head"),
                                            radioButtons("quote", "Quote",
                                                         choices = c(None = "",
                                                                     "Double Quote" = '"',
                                                                     "Single Quote" = "'"),
                                                         selected = '"')),
                                          mainPanel(
                                            verbatimTextOutput("summar"),
                                            tableOutput("contents")
                                          ))), 
             tabPanel("Graphing",
                      titlePanel("Plotting Graphs"),
                      sidebarLayout(
                        sidebarPanel( uiOutput("variable_x"),
                                      uiOutput("variable_y")),
                        mainPanel(
                          h3(textOutput("caption")),
                          plotOutput("plot")
                        )
                      ))
  ))

server <- function(input, output, session) {
  onSessionEnded(stopApp)
  data <- reactive({
    req(input$file1)
    df <- read.csv(input$file1$datapath, header = input$header, sep = input$sep, quote = input$quote)
    return(df)
  })

  output$contents <- renderTable({
    if (input$disp == "head") {
      return(head(data()))
    }
    else {
      return(data())
    }
  })
  output$summar <- renderPrint({
    req(input$file1)
    summary(data())
    }
  )

  output$variable_x <- renderUI({
    selectInput("variableNames_x", label = "Variable_X", choices = names(data()))  
  })
  output$variable_y <- renderUI({
    selectInput("variableNames_y", label = "Variable_Y", choices = names(data()) ) 
  })
  dat <- reactive({
    test <- data.frame(data()[[input$variableNames_x]], data()[[input$variableNames_y]])
    colnames(test) <- c("X", "Y")
    return(test)
  })

  output$plot <- renderPlot({
    if (is.null(data)) { return(NULL)
    } else {
      ggplot(dat(),aes(x = X,y = Y))   geom_point(colour = 'red')  
        labs(y = input$variableNames_y,
             x = input$variableNames_x,
             title = "ggplot")
    }
  })
}

shinyApp(ui, server)

一个完整的shiny,

在ui中通过*input调整数值参数、上传数据等,并将数据传递给server进行相应计算;

然后,Server对数据进行计算、绘图,并将计算结果(图片、表格等)返回给ui

最后,通过ui中的*output来展示server计算返回的结果(图片、表格等)

0 人点赞