【问题标题】:Error: cannot coerce class "c("shiny.render.function", "function")" to a data.frame错误:无法将类“c("shiny.render.function", "function")" 强制转换为 data.frame
【发布时间】:2017-07-09 07:27:20
【问题描述】:

我正在构建一个闪亮的网络应用程序,该应用程序从用户那里获取输入值,例如姓名和性别。我想将这些值转换为数据框,但我收到 “错误:无法将类“c("shiny.render.function", "function")" 强制转换为 data.frame" 错误。

这是我在 server.R 文件中的内容:

    nText<- eventReactive(input$click_counter, valueExpr = {
    a<- renderText(input$name)
    b<- renderText(input$gender)
    c<- renderText(input$college)
    d<- renderText(input$team)
    e<- renderText(input$score)

    return(data.frame(player=a,college=b,gender=c,team=d,score=e))
  })

  output$nText<- renderDataTable({
    nText()
  })

这就是我在 ui.R 文件中的内容:

actionButton("click_counter","Submit"),
dataTableOutput("nText")

【问题讨论】:

  • 你到底在想什么是a、b、c、d或e的结构?您是否意识到您不能拥有带有语言元素的数据框列?也许您应该使用列表?
  • 只是不要用renderText()包装所有inputs

标签: r dataframe shiny


【解决方案1】:

正如@Yang 所说,您需要从输入中删除“renderText”。如果您想在每次点击“click_counter”时存储输入,您可以使用“reactiveValues”。根据您在输入后要对数据执行的操作,我在下面提供了两个选项的工作示例:

ui <- fluidPage(title = "",

                sidebarLayout(

                  sidebarPanel(width = 6,
                               textInput("name", "Name"),
                               textInput("gender", "Gender"),
                               actionButton("click_counter","Submit")
                  ),
                  mainPanel(
                    dataTableOutput("nText"),
                    dataTableOutput("nText2")

                ))

)

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

  values <- reactiveValues()
  values$df <- data.frame()

  mdf <- eventReactive(input$click_counter,{    
    name <- input$name
    gender <- input$gender

    da <- data.frame(name, gender)
    return(da)
  })

  observeEvent(input$click_counter, {
    name <- input$name
    gender <- input$gender

    da <- data.frame(name, gender)

    values$df <- rbind(values$df, da)
  })

  output$nText<- renderDataTable({
    mdf()
  })  

  output$nText2<- renderDataTable({
    values$df
  })
}

shinyApp(ui = ui, server = server)

【讨论】:

  • 感谢您分享您的示例 :)
  • 你知道一个功能,可以让你点击一个按钮,让文本框中的文字消失吗?
  • 你想要updateTextInput,看这个问题:stackoverflow.com/questions/24265980/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-20
  • 2017-04-19
  • 1970-01-01
  • 2020-03-11
相关资源
最近更新 更多