R Shiny不显示输出数据表

时间:2017-09-18 15:46:36

标签: r shiny

我无法将数据表视为输出,既不是整个数据,也不是使用反应函数选择的数据表。数据集由函数Merton在Shiny应用程序内生成。我试图为数据集“mod”使用几个对象类,但它没有用。 这是代码:

 library(shiny)
 server <- function(input,output, session) {

      library(ggplot2) 
      library(DT) 
      library(CreditRisk)

      Maturity <- c(0.5, 1, 2, 3, 5, 10, 15, 20, 25, 30, 35)
      mod <- Merton(L = 10, V0 = 20, sigma = 0.2, r = 0.005, t = Maturity)


      output$plot <- renderPlot({

        ggplot(mod, aes(Maturity, mod$Surv))+ geom_point()

      })


      dat <- reactive({

        user_brush <- input$user_brush
        sel.data <- brushedPoints(mod, user_brush)
        return(sel.data)

      })


      output$table <- DT::renderDataTable(DT::datatable(dat()))

      output$mydownload <- downloadHandler(
        filename = "plotextract.csv",
        content = function(file) {
          write.csv(dat(), file)})
    }

    ui <- fluidPage(

      h3("Exporting Data as .csv"),
      plotOutput("plot", brush = "user_brush"),
      dataTableOutput("table"),
      downloadButton(outputId = "mydownload", label = "Download Table")
    )

    shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:0)

您必须将xvaryvar传递给brushedPoints
brushedPoints(mod, user_brush, xvar = "Maturity", yvar = "Survival")

为简单起见,我更改了ggplotaes(Maturity, Survival)

中的相应变量
library(shiny)

server <- function(input,output, session) {

    library(CreditRisk)
    library(DT) 
    library(ggplot2) 

    Maturity <- c(0.5, 1, 2, 3, 5, 10, 15, 20, 25, 30, 35)
    mod <- Merton(L = 10, V0 = 20, sigma = 0.2, r = 0.005, t = Maturity)

    output$plot <- renderPlot({
        ggplot(mod, aes(Maturity, Survival)) + geom_point()
    })

    dat <- reactive({
        user_brush <- input$user_brush
        brushedPoints(mod, user_brush, xvar = "Maturity", yvar = "Survival")
    })

    output$table <- DT::renderDataTable({DT::datatable(dat())})

    output$mydownload <- downloadHandler(
        filename = "plotextract.csv",
        content = function(file) {write.csv(dat(), file)}
    )
}

ui <- fluidPage(
    h3("Exporting Data as .csv"),
    plotOutput("plot", brush = "user_brush"),
    dataTableOutput("table"),
    downloadButton(outputId = "mydownload", label = "Download Table")
)

shinyApp(ui = ui, server = server)

enter image description here