如何修复两种输入方式之间的冲突(Shiny,R)(w / UPDATE)

时间:2015-06-30 07:57:39

标签: r shiny

我使用文件上传小部件来接受输入数据。 在server.R中:

getData = reactive({
        inFile = input$file1
        if (is.null(inFile)) return(NULL)
        read.csv(inFile$datapath)
    })

但有时用户可能没有数据,所以我想用我的样本数据进行演示。在server.R中:

getData = eventReactive(input$Demo,{
    read.csv('Sample.csv') 
})

如果我只在代码中添加一个选项,它们每个都有效。将它们组合在一起时,eventReactive似乎掩盖了reactive,并且文件加载无效。有人知道我该怎么办?谢谢!

完整代码: server.R

library(shiny)

source('1_Prepare.R')
source('Clean.R')


shinyServer(function(input, output) {

    getData = reactive({
        inFile = input$file1
        if (is.null(inFile)) return(NULL)
        read.csv(inFile$datapath)
    })

    getData = eventReactive(input$Demo,{
        read.csv('TownShort.csv') 
    })

    output$DataBefore = renderDataTable({
        as.data.frame( getData() )     
    })

    result = reactive({
        if(!is.null(getData())) Clean(getData())
    })

    output$DataAfter = renderDataTable({
        as.data.frame( result()[[1]] )
    })

    output$Aggregation = renderDataTable({
        as.data.frame( result()[[2]] )
    })

    output$DataBad = renderDataTable({
        as.data.frame( result()[[3]] )
    })

})

ui.R:

library(shiny)

shinyUI(fluidPage(

    titlePanel(h2("Data Cleaning: Town and State")),

    sidebarLayout(

        sidebarPanel(

            fileInput('file1', 'Choose CSV File (two columns: Town and State)',
                      accept=c('text/csv', 
                               'text/comma-separated-values,text/plain', 
                               '.csv')
                      ),

            tags$hr(),

            actionButton('Demo', 'Demo with sample data')

        ),
        mainPanel(

            tabsetPanel(            
                tabPanel(title=h4('Data'),
                         column(5, tags$h3('Input Data'), dataTableOutput('DataBefore')),
                         column(5, tags$h3('After Clean'), dataTableOutput('DataAfter'))
                ),
                tabPanel(title=h4('Aggregation'),
                         dataTableOutput('Aggregation')
                ),
                tabPanel(title=h4('Unrecognized'),
                         dataTableOutput('DataBad')                         
                )

            )

        )
    )
))

UPDATE 我测试了两个输入方法getData = reactive({...})getData = eventReactive(),在代码中落后于它,它会掩盖前面的那个。

所以我认为,getData是一个变量,我定义了两次,当然它忘记了第一个定义。这是固定代码:

server.R

library(shiny)

source('1_Prepare.R')
source('Clean.R')
DemoData = read.csv('TownShort.csv')

shinyServer(function(input, output) {

    # one way to get data
    getDemo = eventReactive(input$Demo, {DemoData
    })

    # the other way to get data
    getUpload = reactive({
        inFile = input$file1
        if (is.null(inFile)) return(NULL)
        read.csv(inFile$datapath)
    })

    # condition to choose data
    getData = reactive({
        if (!is.null(getUpload())) return(getUpload())
        else if (!is.null(getDemo())) return(getDemo())
    })


    output$DataBefore = renderDataTable({
        as.data.frame( getData() )

    })

    result = reactive({
        if(!is.null(getData())) Clean(getData())
    })

    output$DataAfter = renderDataTable({
        as.data.frame( result()[[1]] )

    })

    output$Aggregation = renderDataTable({
        as.data.frame( result()[[2]] )
    })

    output$DataBad = renderDataTable({
        as.data.frame( result()[[3]] )
    })



})

仍有问题:我可以在getData()getDemo()之间往返一次。具体来说,我可以使用其中一个,切换到另一个。就是这样。我无法更改getData()及更多内容的价值。

有没有办法刷新getData的值?请告诉我。谢谢!

0 个答案:

没有答案