在《 Shiny》中,如何查看以后会发生什么动作?

J

在我的应用程序中,用户可以将其文件作为输入数据上传(使用文件上传小部件)。如果他们没有他们的数据,我将提供一些演示数据(用户可以通过单击actionButton选择演示数据)。

如何制作变量=上传或演示,以较晚者为准?任何帮助表示赞赏。

服务器

library(shiny)

DemoData = data.frame('Col.1'=c('Demo','Demo'),
                      'Col.2'=c('Data','Data'))

shinyServer(function(input, output) {

    # option 1: use demo data
    getDemo = eventReactive(input$Demo,{
        DemoData
    })

    # option 2: user upload data
    getUpload = reactive({
        inFile = input$file
        if (is.null(inFile)) return(NULL)
        read.csv(inFile$datapath)
    })

    # need getData() to be option 1 or 2, whichever happened later
    # should respond to multiple times of changing between option 1 and 2
    getData = # ??? getDemo() or getUpload(), whichever is later

    # show the data
    output$InputData = renderDataTable({
        as.data.frame( getData() )
    })

})

用户界面

library(shiny)

shinyUI(fluidPage(

    titlePanel(h2("Hello Shiny")),

    sidebarLayout(

        sidebarPanel(

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

            actionButton('Demo', 'Use Demo Data')

        ),
        mainPanel(

            tabsetPanel(            
                tabPanel(title=h4('Data'),
                         column(5, tags$h3('Input Data'), dataTableOutput('InputData'))
                )


            )

        )
    )
))

UserData.R(也许可以使您更轻松地进行测试)

getwd()
setwe()

UserData = data.frame('Col.1'=c('User','User'),
                      'Col.2'=c('Data','Data'))

write.csv(UserData, file="UserData.csv", row.names=FALSE)
罗夏

您可以使用观察者,一个观察者观看演示按钮,另一个观察者观看文件上传。两者都会更新相同的反应性数据,因此您会看到最后发生的任何一个的影响。

library(shiny)

DemoData <- data.frame('Col.1'=1:10,
    'Col.2'=rnorm(10))

shinyApp(
    shinyUI(fluidPage(
        titlePanel(h2("Hello Shiny")),
        sidebarLayout(
            sidebarPanel(
                fileInput('file', 
                          'Choose CSV File (two columns: Town and State)',
                          accept=c('text/csv',
                              'text/comma-separated-values,text/plain',
                              '.csv')
                          ),
                actionButton('Demo', 'Use Demo Data')
            ),
            mainPanel(
                tabsetPanel(            
                    tabPanel(title=h4('Data'),
                             column(5, tags$h3('Input Data'), tableOutput('InputData'))
                             )
                )
            )
        )
    )),
    shinyServer(function(input, output) {
        values <- reactiveValues()  # store values to be changed by observers
        values$data <- data.frame()

        ## Observer for uploaded file
        observe({
            inFile = input$file
            if (is.null(inFile)) return(NULL)
            values$data <- read.csv(inFile$datapath)
        })

        ## Observer for demo data button
        observe({
            if (input$Demo > 0)  # otherwise demo data shows on startup
                values$data <- DemoData
        })

        ## show the data
        output$InputData = renderTable({
            values$data
        })

    })
)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章