R Shiny:app.r在分为ui.R和server.R两个脚本时不工作

x4shl7ld  于 9个月前  发布在  其他
关注(0)|答案(1)|浏览(86)
library(shiny)

sales <- vroom::vroom("sales_data_sample.csv", na = "")

library(shiny)
ui <- fluidPage(
  titlePanel("Sales Dashboard"),
  sidebarLayout(
    sidebarPanel(
      selectInput("territory", "Territory", choices = unique(sales$TERRITORY)),
      selectInput("customername", "Customer", choices = NULL),
      selectInput("ordernumber", "Order number", choices = NULL, size = 5, selectize = FALSE),
    ),
    mainPanel(
      uiOutput("customer"),
      tableOutput("data")
    )
  )
)
server <- function(input, output, session) {
  territory <- reactive({
    req(input$territory)
    filter(sales, TERRITORY == input$territory)
  })
  customer <- reactive({
    req(input$customername)
    filter(territory(), CUSTOMERNAME == input$customername)
  })

  output$customer <- renderUI({
    row <- customer()[1, ]
    tags$div(
      class = "well",
      tags$p(tags$strong("Name: "), row$CUSTOMERNAME),
      tags$p(tags$strong("Phone: "), row$PHONE),
      tags$p(tags$strong("Contact: "), row$CONTACTFIRSTNAME, " ", row$CONTACTLASTNAME)
    )
  })

  order <- reactive({
    req(input$ordernumber)
    customer() %>%
      filter(ORDERNUMBER == input$ordernumber) %>%
      arrange(ORDERLINENUMBER) %>%
      select(PRODUCTLINE, QUANTITYORDERED, PRICEEACH, SALES, STATUS)
  })

  output$data <- renderTable(order())

  observeEvent(territory(), {
    updateSelectInput(session, "customername", choices = unique(territory()$CUSTOMERNAME), selected = character())
  })
  observeEvent(customer(), {
    updateSelectInput(session, "ordernumber", choices = unique(customer()$ORDERNUMBER))
  })

}
shinyApp(ui, server)

字符串
有人做过吗?谢谢!

bhmjp9jg

bhmjp9jg1#

以下是我的split code:

global.R

library(shiny)
sales <- vroom::vroom("sales_data_sample.csv", na = "")

字符串

ui.R

library(shiny)

shinyUI(fluidPage(
  titlePanel("Sales Dashboard"),
  sidebarLayout(
    sidebarPanel(
      selectInput("territory", "Territory", choices = unique(sales$TERRITORY), 
                  verbatimTextOutput("value1")),
      selectInput("customername", "Customer", choices = NULL),
      selectInput("ordernumber", "Order number", choices = NULL, size = 5, selectize = FALSE),
    ),
    mainPanel(
      uiOutput("customer"),
      tableOutput("data"),
    )
  )
))

server.R

server <- function(input, output, session) {
  
  observeEvent(input$territory, {
    customers <- sales$CUSTOMERNAME[which(sales$TERRITORY == input$territory)]
    updateSelectInput(session, "customername", choices = customers, selected = customers[1])
  })
  
  observeEvent(input$customername, {
    orders <- unique(sales$ORDERNUMBER[which(sales$CUSTOMERNAME == input$customername)])
    updateSelectInput(session, "ordernumber", choices = orders, selected = orders[1])
  })
  
  territory <- reactive({
    req(input$territory)
    filter(sales, TERRITORY == input$territory)
  })
  customer <- reactive({
    req(input$customername)
    filter(territory(), CUSTOMERNAME == input$customername)
  })
  
  output$customer <- renderUI({
    row <- customer()[1, ]
    tags$div(
      class = "well",
      tags$p(tags$strong("Name: "), row$CUSTOMERNAME),
      tags$p(tags$strong("Phone: "), row$PHONE),
      tags$p(tags$strong("Contact: "), row$CONTACTFIRSTNAME, " ", row$CONTACTLASTNAME)
    )
  })
 
  order <- reactive({
    req(input$ordernumber)
    customer() %>%
      filter(ORDERNUMBER == input$ordernumber) %>%
      arrange(ORDERLINENUMBER) %>%
      select(PRODUCTLINE, QUANTITYORDERED, PRICEEACH, SALES, STATUS)
  })
  
  output$data <- renderTable(order())
  
}

相关问题