2017-03-27 4 views
0

탐색 모음을 사용할 때 파일 업로드 기능이 제대로 작동하지 않습니다. 여기에 내가 server.R을 위해 무엇을 가지고 :R의 Navbar에서 파일 업로드가 작동하지 않습니다. Shiny

library(shiny) 
library(markdown) 

function(input, output) { 

    df <- NULL 
    current_file <- NULL 
    in_data <- reactive({ 
    inFile <- input$file1 
    if (is.null(inFile)){ 
    return(NULL)}  
else {df <<- read.csv(inFile$datapath, header=input$header, sep=input$sep, 
        quote=input$quote) 
     current_file <<- inFile$name 
} 

return(NULL) 
    }) 


output$fileInfo <- renderTable({ 
call.me = in_data() 
data.frame(current_file) 
}) 




} 

을 그리고 ui.RI에 대한이 있습니다

library(shiny) 
library(markdown) 

navbarPage('Navigation Bar', 
     tabPanel('File Information', verbatimTextOutput('fileInfo'), 
       sidebarLayout(
        sidebarPanel(fileInput('file1', 'Choose CSV File', 
             accept=c('text/csv', 
                'text/comma-separated-values,text/plain', 
                '.csv')), 
        checkboxInput('header', 'Header', TRUE), 
        radioButtons('sep', 'Separator', 
           c(Comma=',', 
            Semicolon=';', 
            Tab='\t'), 
           ','), 
        radioButtons('quote', 'Quote', 
           c(None='', 
            'Double Quote'='"', 
            'Single Quote'="'"), 
           '"'), 
        tags$hr()), 
        mainPanel() 
       ))) 

나는 결국 탐색 모음에서 여러 탭을 갖고 싶어, 나는 할 수 있도록하려면 각 탭에서 데이터에 액세스합니다. 지금 내가하고 싶은 것은 그것이 업로드 된 파일의 이름을 내게주는 것입니다. 이것은 네비게이션 바를 넣지 않으려 고 할 때 훌륭하게 작동합니다.하지만 이제는 일종의 HTML 출력처럼 보입니다. 어떤 팁? 매우 감사.

답변

0

여기서 글로벌 할당 <<-의 사용을 등록하는 것은 매우 나쁜 생각입니다. 반응성을 사용하는 경우 데이터가 전체 앱에서 전 세계적으로 사용 가능할 것입니다. 즉, 반짝이는 방식입니다.

또한 입력을 확인하려면 shiny::validate (아래 예제 사용)을 살펴보십시오.

library("shiny") 

options(shiny.maxRequestSize = 9*1024^2) 

server <- function(input, output) { 

    # the data read in by read.csv will now be accessible throughout the app 
    # by calling this reactive, e.g. in_data(). 
    in_data <- reactive({ 
    shiny::validate(
     need(input$file1, "Select a file!") 
    ) 

    read.csv(input$file1$datapath, header = input$header, 
      sep = input$sep, quote = input$quote) 
    }) 

    # an example, we're calling the reactive to access the data loaded 
    output$file_info <- renderTable({ 
    data.frame(in_data()) 
    }) 

} 

ui <- navbarPage('Navigation Bar', 
    tabPanel('File Information', verbatimTextOutput('fileInfo'), 
    sidebarLayout(
     sidebarPanel(
     fileInput('file1', 'Choose CSV File', accept = c('text/csv', 'text/comma-separated-values,text/plain', '.csv')), 
     checkboxInput('header', 'Header', TRUE), 
     radioButtons('sep', 'Separator', c(Comma = ',', Semicolon = ';', Tab = '\t'), ','), 
     radioButtons('quote', 'Quote', c(None = '', 'Double Quote'='"', 'Single Quote' = "'"),'"'), 
     tags$hr() 
     ), 
     mainPanel(
     tableOutput("file_info") 
     ) 
    ) 
    ) 
) 

shinyApp(ui, server) 

enter image description here

+0

감사합니다! 나는 꽤 R R 반짝입니다. 글로벌 변수를 사용하는 것이 왜 나쁜 생각입니까? –

+0

전역 (http://rstudio.github.io/shiny/tutorial/#scoping 참조)을 사용하려는 경우가 있지만이 경우 한 세션의 데이터를 다른 세션에 유지하지 않으려는 경우가 있습니다. 로드 된 후에 데이터로 수행하는 작업에 따라 심각하게 이상한 결과가 발생할 수 있습니다. 예를 들어 오래된 데이터로 의사 계산을 수행 할 수 있으며 사용자는 아무런 아이디어도 얻지 못합니다. – mlegge