【问题标题】:R Shiny app with excel like features using rhandsontable使用 rhandsontable 具有类似 excel 功能的 R Shiny 应用程序
【发布时间】:2019-04-13 11:09:18
【问题描述】:

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

Reproducible Minimal App:当前图显示 col1 与 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. 用户为预测变量选择范围:

  1. 用户选择响应范围:

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

另外,在我的原始应用程序中,用户从我使用 rhandsontable 显示的 excel 文件中上传数据。 excel文件没有特定的格式(数据可以从文件中的任何地方开始),从而增加了问题的复杂性。否则,我曾想过使用colnames 之类的东西来生成两个selectInput 下拉列表,并使用nrow 来创建两个sliderInputs 来帮助用户选择变量和行范围。

【问题讨论】:

    标签: r shiny rhandsontable


    【解决方案1】:

    自我回答

    要使表格可编辑并访问选定的值,rhandsontable() 中的readOnlyselectCallback 参数应分别设置为FALSETRUE。我逐行迭代选定的值,使用input$table_select$data 来获取属于选定列的值。 $data[i][[1]][[1]][[1]][[2]] 等顺序给出第i 行中的所有元素,其中[[1]][[n]] 是第n 列中的值。

    我使用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)
      

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-24
      • 2017-12-02
      • 1970-01-01
      • 2016-10-09
      相关资源
      最近更新 更多