2017-06-13 92 views
0

我正在嘗試編寫一個腳本來上傳.csv文件並在UI中引發子選項(以複選框的形式)。上傳後,顯示以前不可見的複選框,顯示實際的數據值。同時,還會出現一個動作按鈕,在選定的數據值(通過複選框)(可能)改變之後被點擊。r閃亮 - 通過選擇值減少上傳的數據集

那麼我有什麼?
1)上傳 - 確定。
2)選擇並顯示覆選框 - 確定。
3)顯示操作按鈕 - 確定。
4)正確執行所需的操作(=數據子集) - 不正確;子集顯然會返回空或NULL數據幀。

任何幫助將不勝感激。

這是代碼。

我會補充說,我基於子集的Dte字段是一個日期字段,它最初在csv中爲dd/mm/yyyy格式且不含引號。

library(shiny) 
library(lubridate) 

ui = fluidPage(sidebarPanel(width = 2, 
          fileInput('files', '', accept = c('text/csv','text/comma-separated-values','text/tab-separated-values','text/plain','.csv','.tsv')), 
          checkboxInput('header', 'Header', TRUE), 
          radioButtons('sep', 'Separator', c(Comma=',',Semicolon=';',Tab='\t'), selected=';'), 
          radioButtons('quote', 'Quote', c(None='','Double Quote'='"','Single Quote'="'"), selected='"'), 
          uiOutput('apply')), 
       mainPanel(width = 10, uiOutput('years'), dataTableOutput("tabel4") 
       ) 
) 

server = function(input, output, session) { 
    ### if a csv is selected (through fileInput()), it is read 
    vis0 <- eventReactive(input$files, { 
    vis <- read.csv(input$files$datapath, header = input$header, sep = input$sep, quote = input$quote, 
          stringsAsFactors =FALSE) 
    vis$Dte <- as.Date(as.character(vis$Dte), "%d/%m/%Y") 
    vis$year <- factor(year(vis$Dte)) 
    vis 
    }) 
    ### button appearing only after 
    output$apply <- renderUI({ 
    if (is.null(input$files)) return() 
    actionButton("Load", "APPLY DATA") 
    }) 
    ### get checkbox values from data and throw them back to UI 
    output$years <- renderUI({ 
    df <- vis0() 
    df$year <- as.character(df$year) 
    if (is.null(df)) return(NULL) 
    items= sort(unique(df$year)) 
    names(items)=items 
    checkboxGroupInput("dependent","Check or uncheck years to be included or excluded", 
         choices=items, selected = items, inline = TRUE) 
    }) 
    ### use selected checkbox values to reduce initial dataset 
    vis1 <- reactive({ 
    if (is.null(input$Load)) return() 
    if (input$Load==0) return() 
    data <- vis0() 
    data <- subset(data, as.character(year) %in% input$years) 
    data 
    }) 
    ### using the checkbox-reduced data to create a table 
    output$tabel4 <- renderDataTable({ 
    vis1() 
    }) 
    } 

shinyApp(ui = ui, server = server, options = list(launch.browser=TRUE)) 
+0

你確定你正在使用'uiOutput'嗎? [docs](https://shiny.rstudio.com/articles/dynamic-ui.html)表示*在ui.R中,使用uiOutput來告訴Shiny應該呈現這些控件的位置。*也就是說,我不知道輸入$ years是否真的返回服務器的工作狀態。 – patrick

+0

我會說我做了你引用的......?另請參閱我遵循的這個工作示例(最常見的答案)。 https://www.google.be/url?sa=t&source=web&rct=j&url=https://stackoverflow.com/questions/27769186/shiny-show-buttons-only-after-file-has-been-uploaded&ved= 0ahUKEwiejZ6XzrzUAhVGK1AKHWOyBzkQFggcMAA&usg = AFQjCNH1YpN-jPY2OEGwWlOBdxw6YxYIKA&sig2 = BHJrx4mYZiiBF4VfiUZQxg –

+0

我試圖直接在UI輸出中輸入$ year作爲文本輸出。看起來你是對的,或者至少選定的年份沒有顯示。任何想法,替代方案......? –

回答

0

我找到了解決方案!

一個愚蠢的錯誤(至少)是dependent必須是years。也許有更多(特別是在if檢查),我修正了,但沒有正確跟蹤,但下面的代碼工作。

library(shiny) 
library(lubridate) 

ui = fluidPage(sidebarPanel(width = 2, 
          fileInput('files', '', accept = c('text/csv','text/comma-separated-values','text/tab-separated-values','text/plain','.csv','.tsv')), 
          checkboxInput('header', 'Header', TRUE), 
          radioButtons('sep', 'Separator', c(Comma=',',Semicolon=';',Tab='\t'), selected=';'), 
          radioButtons('quote', 'Quote', c(None='','Double Quote'='"','Single Quote'="'"), selected='"'), 
          uiOutput('apply')), 
       mainPanel(width = 10, uiOutput('years'),tags$br(),tags$br(), 
         textOutput('showyrs'),tags$br(),tags$br(), 
         textOutput('yrz'),tags$br(),tags$br(), 
         dataTableOutput("tabel4") 
       ) 
) 


###################################################################### 

server = function(input, output, session) { 
    ### if a csv is selected (through fileInput()), it is read 
    vis0 <- reactive({ 
    req(input$files) 
    vis <- read.csv(input$files$datapath, header = input$header, sep = input$sep, quote = input$quote, 
          stringsAsFactors =FALSE) 
    vis$Dte <- as.Date(as.character(vis$Dte), "%d/%m/%Y") 
    vis$year <- factor(year(vis$Dte)) 
    vis 
    }) 

    ### button appearing only after 
    output$apply <- renderUI({ 
    if (is.null(input$files)) return() 
    actionButton("Load", "APPLY DATA") 
    }) 

    ### get checkbox values from data and throw them back to UI 
    output$years <- renderUI({ 
    req(vis0) 
    df <- vis0() 
    df$year <- as.character(df$year) 
    if (is.null(df)) return(NULL) 
    items= sort(unique(df$year)) 
    names(items)=items 
    checkboxGroupInput("years","Check or uncheck years to be included or excluded", 
         choices=items, selected = items, inline = TRUE) 
    }) 

    ### show input$years 
    output$showyrs <- renderText({ 
    req(input$files) 
    if (is.null(input$years)) { 
     "Nothing..." 
    } else if (input$years==0) { 
     "Nothing..." 
    } else {input$years} 
    }) 

    ### use selected checkbox values to reduce initial dataset 
    vis1 <- reactive({ 
    req(input$years) 
    if (is.null(input$Load)) return() 
    if (input$Load==0) return() 
    data <- vis0() 
    data <- subset(data, as.character(year) %in% input$years) 
    data 
    }) 

    ### using the checkbox-reduced data to create a table 
    output$tabel4 <- renderDataTable({ 
    vis1() 
    }) 
    } 

shinyApp(ui = ui, server = server, options = list(launch.browser=TRUE))