2018年6月16日 星期六

Shiny 使用者上傳資料並建立互動式資料 --- 腳本

程式碼

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

shinyUI(fluidPage(
  titlePanel("Analysis Produce Price"),
  sidebarLayout(
 
    # Input(s)
    sidebarPanel(width = 2,
                  fileInput("file1", "Choose CSV File",
                  accept = c("text/csv",
                             "text/comma-separated-values,text/plain",
                             ".csv")),
               
                 # Select variable for x-axis
                 uiOutput("x.selector"),
               
                 # Select variable for y-axis
                 uiOutput("y.selector"),
               
                 # Select color
                 uiOutput("col.selector"),
               
                 # Select point size
                 sliderInput(inputId = "size",
                             label = "Size:",
                             min = 0, max = 5,
                             value = 2),
               
                 # Select which markets on to plot
                 uiOutput("market.selector"),
               
                 # Show data table
                 uiOutput("showdata.selector")
    ),
    # Output(s)
    mainPanel(width = 10,
              tabsetPanel(id = "tabspanel", type = "tabs",
                          tabPanel(title = "Plot",
                                   plotOutput(outputId = "scatterplot")),
                       
                          tabPanel(title = "Data",
                                   DT::dataTableOutput(outputId = "table"))
              )
    )
  )
))


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

shinyServer(function(input, output, session) {
  
  price <- reactive({
    myfile <- input$file1
    if(is.null(myfile))
      {return()}
    mydata <- read.table(myfile$datapath, header = TRUE, sep = ",")
    mydata <- mydata[, 1:9]
    colnames(mydata) <- c("date", "market", "product", "high", "medium", "low", "price_avg", "volume", "value")
    mydata$date <- as.Date(paste(as.numeric(substr(mydata$date, 1, 3)) + 1911, substr(mydata$date, 4, 9), sep = ""))
    mydata$volume <- as.numeric(mydata$volume)
    mydata$market <- as.character(mydata$market)
    mydata$product <- as.character(mydata$product)
    mydata <- mydata[!is.na(mydata), ]
    return(mydata)
  })
  
  #Create UI condition
    # Select variable for x-axis
    output$x.selector <- renderUI({
    selectInput(inputId = "x", 
              label = "X-axis:",
              choices = names(price()),
              selected = names(price())[1])
    })
    
    # Select variable for y-axis
    output$y.selector <- renderUI({
    selectInput(inputId = "y", 
              label = "Y-axis:",
              choices = names(price()), 
              selected = names(price())[7])
    })
    
    # Select color
    output$col.selector <- renderUI({
    selectInput(inputId = "z",
              label = "Color by:",
              choices = names(price()),
              selected = names(price())[3])
    })
    
    # Select which markets on to plot
    output$market.selector <- renderUI({
    checkboxGroupInput(inputId = "selected_market",
                       label = "Select market(s):",
                       choices = levels(factor(price()$market)),
                       selected = levels(factor(price()$market)))
    })
    
    # Show data table
    output$showdata.selector <- renderUI({
    checkboxInput(inputId = "show_data",
                  label = "Show data table",
                  value = TRUE)
    })
    
  # Create a subset of data filtering for selected markets
  price_subset <- reactive({
    req(input$selected_market)
    filter(price(), market %in% input$selected_market)
  })
  
  # Create scatterplot object the plotOutput function is expecting
  output$scatterplot <- renderPlot({
    ggplot(data = price_subset(), aes_string(x = input$x, y = input$y, color = input$z)) +
      geom_point(size = input$size)
  })
  
  # Update code below to render data table regardless of current state of input$show_data
  output$table <- DT::renderDataTable({
    DT::datatable(data = price()[, 1:9], 
                  options = list(pageLength = 10), 
                  rownames = FALSE)
  })
  
  # Display data table tab only if show_data is checked
  observeEvent(input$show_data, {
    if(input$show_data){
      showTab(inputId = "tabspanel", target = "Data", select = TRUE)
    } else {
      hideTab(inputId = "tabspanel", target = "Data")
    }
  })
})

成果



下篇文章再來說說這個 Shiny 的功能
已經撰寫完成:

沒有留言:

張貼留言

<房市老手21堂超強實戰課:快速看穿房屋買賣陷阱>閱讀筆記

https://www.taaze.tw/apredir.html?131322949/https://www.taaze.tw/products/11100982076.html?a=b 賣房篇 1.      頂樓加蓋對於房價 頂樓加蓋還是可以計入房價,多為房屋單坪價格的 1...