2017-08-30 30 views
0

我有一個像下面這樣的應用程序。我想要從文件上傳中讀入數據或使用內置數據。我以爲我可以把一個動作按鈕,如果有人點擊它,輸入數據將掛載並進入下一個級別。我的問題是後來在我的真實應用程序中,一些小部件(如selectInput)必須更新,並且我希望在用戶決定是使用上傳的數據還是內置的數據之前爲空。如何使用內置的數據而不是上傳的數據

library(shiny) 

x <- mtcars 

ui <- fluidPage(
    fileInput(inputId = "uploadcsv", "", accept = '.csv'), 
    actionButton(inputId = "a", label = "action button"), 
     selectInput("select",label = h3("Select box"),choices = "",selected = 1) 
) 

server <- function(input, output, session) { 

    data <- reactive({ 
    infile <- input$uploadcsv 

    if (is.null(infile)) 
     return(NULL) 

    read.csv(infile$datapath, header = TRUE, sep = ",") 
    }) 

    DataToUse <- NULL 

    observe(!is.null(input$uploadedcsv), 
       DataToUse <- data() 
) 

    observeEvent(input$a, 
       DataToUse <- x 
) 

    observe({ 
    req(DataToUse) 
    if (max(DataToUse$cyl) %% 4 == 0){ 
     numberofinterval <- max(DataToUse$cyl) %/% 4 
    } else { 
     numberofinterval <- (max(DataToUse$cyl) %/% 4)+1 
    } 

    NumPeriod <- seq(0, numberofinterval) 

    updateSelectInput(session, inputId = "select", 
          choices = NumPeriod, 
          selected = NumPeriod) 
    }) 

} 
shinyApp(ui = ui, server = server) 

回答

0

像這樣的東西應該做的:

library(shiny) 

x <- mtcars 

ui <- fluidPage(
    fileInput(inputId = "uploadcsv", "", accept = '.csv'), 
    actionButton(inputId = "a", label = "action button"), 
    selectInput("select",label = h3("Select box"),choices = "",selected = 1) 
) 

server <- function(input, output, session) { 

    data <- reactive({ 
    infile <- input$uploadcsv 

    if (is.null(infile)) 
     return(NULL) 

    read.csv(infile$datapath, header = TRUE, sep = ",") 
    }) 

    v <- reactiveValues() 
    v$DataToUse <- NULL 

    observeEvent(input$uploadcsv,{ 
    if(!is.null(input$uploadcsv)){ 
     v$DataToUse <- data() 
    } 
    }) 

    observeEvent(input$a,v$DataToUse <- x) 

    observeEvent(v$DataToUse,{ 
    req(v$DataToUse) 
    if (max(v$DataToUse$cyl) %% 4 == 0){ 
     numberofinterval <- max(v$DataToUse$cyl) %/% 4 
    } else { 
     numberofinterval <- (max(v$DataToUse$cyl) %/% 4)+1 
    } 

    NumPeriod <- seq(0, numberofinterval) 

    updateSelectInput(session, inputId = "select", 
         choices = NumPeriod, 
         selected = NumPeriod) 
    }) 

} 
shinyApp(ui = ui, server = server) 

enter image description here

相關問題