具备Excel的R Shiny应用(例如使用rhandsontable功能的

时间:2018-11-09 20:59:42

标签: r shiny rhandsontable

我想创建一个应用程序,用户可以根据他/她选择的数据查看回归结果。我希望用户选择两个数据范围(每个范围都属于一列,就像您在excel中所做的那样),我的应用程序应该创建一个散点图并显示线性回归系数。我在数据选择部分遇到困难。此外,用户还应该具有更新数据的选项,然后单击操作按钮以更新绘图和结果。到目前为止,我已经使用this example实现了数据更新功能。另外,我知道我可以通过执行this answer这样的操作来获取选定的数据。但是,我需要两个选择范围而不是一个。我该如何构建?我从rhandsontable开始,因为它看起来像是适合此类功能的库。我乐于接受建议,这些建议可以使我指向其他可以提供帮助的库。

可复制的最小应用程序:当前图显示的是col1 vs col2。

library(shiny)
library(rhandsontable)
library(plotly)

test_data <- structure(list(Id = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14), 
               col1 = c(12.4, 12.5, 14.3, 14.8, 8.4, 8.1, 12, 12.4, 11.8, 11.9, 13.6, 13, 11, 11.2), 
               col2 = c(12.54, 11.96, 14.92, 14.11, 7.97, 7.91, 11.41, 12.18, 12.12, 12.53, 12.69, 13.18, 11.01, 11.24), 
               col3 = c(98, 98.7, 95, 95.2, 103.7, 104, 89.1, 89.5, 85.8, 85.3, 91, 90.3, 84.4, 83.6), 
               col4 = c(109.61, 109.9, 105.51, 103.35, 124.49, 120.42, 101, 101.7, 97.54, 90.45, 103.27, 97.37, 93.04, 80.54)), 
               row.names = c(NA, -14L), class = c("tbl_df", "tbl", "data.frame"))
# UI
ui <- tabsetPanel(
  tabPanel("Regression Analysis",
           fluidPage(
             sidebarPanel(actionButton("go", "Plot"),
                      hr(),
                      width = 3
             ),
             # Output
             mainPanel(
               br(),
               plotlyOutput("reg.plot"),
               hr(),
               rHandsontableOutput("data.as.hot"),
               hr() 
             )
           ))
)
# Server
server <- function(input, output, session){
  output$data.as.hot <- renderRHandsontable({
    rhandsontable(test_data)
  })

  mydata <- reactiveValues()

  observe({
    if(!is.null(input$data.as.hot))
      mydata$data <- hot_to_r(input$data.as.hot)
  })

  vals <- eventReactive(input$go, {
    return(mydata$data)
  })

  output$reg.plot <- renderPlotly({
    # Create plot
    plot_ly() %>%
      add_trace(data = vals(), x = vals()$col1, y = vals()$col2,
                type = 'scatter', mode = 'markers')
  })
}

# Create a Shiny app object
shinyApp(ui = ui, server = server)

我想要的

  1. 用户选择预测变量的范围:

enter image description here

  1. 用户选择响应范围:

enter image description here

  1. 用户单击操作按钮,应用程序将显示散点图和回归系数。

此外,在我的原始应用中,用户从excel文件中上传数据,而我使用rhandsontable显示了该文件。 excel文件没有特定的格式(数据可以从文件中的任何位置开始),从而增加了问题的复杂性。否则,我曾考虑过使用类似colnames的东西来生成两个selectInput的下拉菜单,并使用nrow来创建两个sliderInput来帮助用户选择变量和行范围。

1 个答案:

答案 0 :(得分:0)

自我答案

要使表格可编辑并访问选定的值,应分别将readOnly中的selectCallbackrhandsontable()参数设置为FALSETRUE。我使用input$table_select$data逐行遍历所选值,以获取属于所选列的值。 $data[i]i[[1]][[1]]的顺序给出第[[1]][[2]]行中的所有元素,依此类推,其中[[1]][[n]]是第n列中的值。 / p>

我使用eventReactive将选定的值分配给可以绘制的向量,用于拟合回归模型等。

  1. 用户选择要分配为预测变量的值的范围,然后单击“设置预测变量”操作按钮。
  2. 用户选择要分配为响应的值的范围,然后单击“设置响应”操作按钮。情节等已生成。

    library(shiny)
    library(rhandsontable)
    library(plotly)
    
    test_data <- structure(list(Id = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14), 
               col1 = c(12.4, 12.5, 14.3, 14.8, 8.4, 8.1, 12, 12.4, 11.8, 11.9, 13.6, 13, 11, 11.2), 
               col2 = c(12.54, 11.96, 14.92, 14.11, 7.97, 7.91, 11.41, 12.18, 12.12, 12.53, 12.69, 13.18, 11.01, 11.24), 
               col3 = c(98, 98.7, 95, 95.2, 103.7, 104, 89.1, 89.5, 85.8, 85.3, 91, 90.3, 84.4, 83.6), 
               col4 = c(109.61, 109.9, 105.51, 103.35, 124.49, 120.42, 101, 101.7, 97.54, 90.45, 103.27, 97.37, 93.04, 80.54)), 
                   row.names = c(NA, -14L), class = c("tbl_df", "tbl", "data.frame"))
    
    # UI
    ui <- tabsetPanel(
      tabPanel("Regression Analysis",
                fluidPage(
                 sidebarPanel(
                              actionButton("button.fv", "Set Predictor"),
                              hr(),
                              actionButton("button.sv", "Set Response"),
                              width = 3
                 ),
                 # Output
                 mainPanel(
                   br(),
                   plotlyOutput("reg.plot"),
                   hr(),
                   rHandsontableOutput("hot"),
                   hr() 
                 )
               ))
    )
    
    # Server
    server <- function(input, output, session){
     output$hot <- renderRHandsontable({
      rhandsontable(test_data, readOnly = F, selectCallback = TRUE)
    })
    
    # Create vector of selected values
    first.vector <- eventReactive(
      input$button.fv, {
        req(input$hot_select)
        start.row <- input$hot_select$select$r
        end.row <- input$hot_select$select$r2
        selected.col <- input$hot_select$select$c
    
        selected.vector <- list()
    
      for (i in start.row:end.row){
        value <- input$hot_select$data[i][[1]][[selected.col]]
        selected.vector[i] <- value
      }
      return(unlist(selected.vector))
    }
    )
    
    second.vector <- eventReactive(
    input$button.sv, {
      req(input$hot_select)
      start.row <- input$hot_select$select$r
      end.row <- input$hot_select$select$r2
      selected.col <- input$hot_select$select$c
    
      selected.vector <- list()
    
      for (i in start.row:end.row){
        value <- input$hot_select$data[i][[1]][[selected.col]]
        selected.vector[i] <- value
      }
      return(unlist(selected.vector))
    }
    )
    
    output$reg.plot <- renderPlotly({
    req(input$hot_select)
    validate(
      need(length(first.vector()) == length(second.vector()), "Selected ranges should be equal in length")
    )
    plot_ly(x = first.vector(), y = second.vector(), type = 'scatter', mode = 'markers')
    })
    }
    
    # Create a Shiny app object
    shinyApp(ui = ui, server = server)