【问题标题】:R Shiny | Set up an editable data frame to be able to make a plot laterR 闪亮 |设置一个可编辑的数据框,以便稍后制作绘图
【发布时间】:2020-12-21 17:49:19
【问题描述】:

我正在尝试根据 sliderInput 的输出设置数据框。

这是我的代码:

runApp(
  list(
    ui = dashboardPage(
      dashboardHeader(title = "Crédit"),
      dashboardSidebar(
        sidebarMenu(
          menuItem("Visualisation des données", tabName = "visualization", icon = icon("poll"))
        )),
      dashboardBody(
        tabItems(
          # visualization
          tabItem(tabName = "visualization",
                  h1("Visualisation des données"),
                  h2("Exploration du tableau"),
                  dataTableOutput('dataTable'),
                  h2("Graphiques"),
                  fluidRow(
                    column(5, plotOutput("Plot1")),
                    column(4,dateRangeInput("dates", label = h3("Date range"),
                                            start = as.Date("1972-09-30"))
                       ),
                    column(4,
                           sliderInput("slider", label = h3("Slider"), min = 0, 
                                       max = 5, value = 1, step = 0.5),
                    column(6,
                          dataTableOutput('my_table')
                    )))
          )
        )))
    , server = function(input, output) {
      myReactives <- reactiveValues(credit = Credit_Defaut)
      output$dataTable = DT::renderDataTable(Credit_Defaut[1:100,])
      a <- reactive({as.data.frame(matrix(data = NA, nrow = (length(seq(0,10,input$slider))-1), ncol = 1))})
      b <- reactive({as.data.frame(matrix(data = NA, nrow = (length(seq(0,10,input$slider))-1), ncol = 1))})
      df <- reactive({as.data.frame(matrix(data = c(paste0("[", seq(0,10,input$slider)[1],";",seq(0,10,input$slider)[2],"]"),
                                                    for (i in 2:(length(seq(0,10,input$slider))-2)){
                                                      a()[i-1] <- paste0("]", seq(0,10,input$slider)[i],";",seq(0,10,input$slider)[i+1],"]")},a(),
                                                    paste0("]", max(seq(0,10,input$slider)),";","Inf","["),
                                                    nrow(myReactives$credit[which(myReactives$credit$Defaut>=seq(0,10,input$slider)[1] & myReactives$credit$Defaut<=seq(0,10,input$slider)[2]),]),
                                                    for (i in 2:(length(seq(0,10,input$slider))-2)){
                                                      b()[i-1] <- nrow(myReactives$credit[which(myReactives$credit$Defaut>seq(0,10,input$slider)[i] & myReactives$credit$Defaut<=seq(0,10,input$slider)[i+1]),])},
                                                    b(),
                                                    nrow(myReactives$credit[which(myReactives$credit$Defaut>max(seq(0,10,input$slider))),])),nrow = (length(seq(0,10,input$slider))-1), ncol = 2))})
      output$my_table  <- renderDataTable({
        df()
      })
    }))

首先,我只想检查我得到的数据帧是否正确。但是我有以下错误:

Warning: Error in <-: invalid (NULL) left side of assignment

我认为这是由于我的 for 循环中的“a”和“b”。我不知道我是否在我的向量中正确调用了它们。

有人遇到过这个问题吗?

提前感谢那些将投资解决我的问题的人。

【问题讨论】:

  • 如果我听说“Shiny 的可编辑 data.frame”我会想到 rhandsontable
  • 看了之后,我觉得这不是我要找的。我只想考虑 SliderInput 的输出,以便在设计时使用它来创建数据框。但是谢谢你提供这个。

标签: r dataframe shiny reactive


【解决方案1】:

您不能为反应函数的结果赋值:

# Correct:
a <- reactive({as.data.frame(matrix(data = NA, nrow = (length(seq(0,10,input$slider))-1), ncol = 1))})

# Wrong:
a()[i-1] <- paste0(...)

你可以创建一个临时数组然后填充它:

reactive({
  a_ <- a()
  a_[i-1] <- paste0(...)
  ...
})

b() 也有同样的问题

【讨论】:

  • 感谢您的回答,我会考虑您的解决方案。但是,我的印象是,当我没有反应时,这些操作是我无法做到的。 a &lt;- reactive({as.data.frame(matrix(data = NA, nrow = (length(seq(0,10,input$slider))-1), ncol = 1))}) b &lt;- reactive({as.data.frame(matrix(data = NA, nrow = (length(seq(0,10,input$slider))-1), ncol = 1))}) a_ &lt;- a() b_ &lt;- b()
  • 这是正确的,你只能在其他反应式表达式中使用反应式函数,我澄清了我的编辑
  • 太好了,谢谢!现在我的矩阵的内容也有问题。向量只取第一个值并复制它。这是错误:Warning in [(*tmp*, , j, value = list("[0;1]", c("]1;2]", : provided 10 variables to replace 1 variables Warning in [(*tmp*, , j, value = list(31385L, c(36492L, : provided 10 variables to replace 1 variables
  • 我的印象是在for循环期间,有exit。在错误中我们可以很容易地看到它找到了我想要的术语,但是由于错误它没有将它放入向量中。
  • 好的,请考虑接受我的部分答案,以便其他人也可以从中受益。
猜你喜欢
  • 2015-05-22
  • 1970-01-01
  • 1970-01-01
  • 2010-10-25
  • 2017-03-07
  • 2019-08-01
  • 2021-06-21
  • 2018-12-02
  • 2019-05-20
相关资源
最近更新 更多