2017-08-30 8 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

関連する問題