【问题标题】:Loop and renderText in R shinyR中的循环和renderText闪亮
【发布时间】:2020-05-08 22:10:34
【问题描述】:

我正在尝试循环遍历数据框并在 R 闪亮中打印输出。这是运行良好的代码的独立示例。但是一旦我在闪亮的空白输出中使用renderText。我在这里做错了什么。

renderText({


emp.data <- data.frame(
  emp_id = c (1:5), 
  emp_name = c("Rick","Dan","Michelle","Ryan","Gary"),
  salary = c(623.3,515.2,611.0,729.0,843.25), 

  start_date = as.Date(c("2012-01-01", "2013-09-23", "2014-11-15", "2014-05-11",
                         "2015-03-27")),
  stringsAsFactors = FALSE
)
for (row in 1:nrow(emp.data)) {
  name <- emp.data[row, "emp_name"]
  salary  <- emp.data[row, "salary"]



 print( paste(" Employee ", name ,"has a total of ",salary," dollars"))

}   
          }


    })

我在 UI 中使用textOutput

【问题讨论】:

  • 您不能将 data.frame 呈现为文本,而是呈现一个表格
  • 我在使用 renderUI 时遇到了同样的问题。我要做的就是将表格输出显示为句子
  • 我会将所有短语保存为 df 的行,使用 dplyr 中的 pull() 或 $ 运算符将它们拉出,然后打印渲染此文本变量,您无法在 shinny 中渲染打印,打印逃到你的环境中而不是闪闪发光

标签: r shiny


【解决方案1】:

类似的东西

library(tidyverse)


renderText({

emp.data <- data.frame(
  emp_id = c (1:5), 
  emp_name = c("Rick","Dan","Michelle","Ryan","Gary"),
  salary = c(623.3,515.2,611.0,729.0,843.25), 

  start_date = as.Date(c("2012-01-01", "2013-09-23", "2014-11-15", "2014-05-11",
                         "2015-03-27")),
  stringsAsFactors = FALSE
)

df_text <- c()
for (row in 1:nrow(emp.data)) {
  name <- emp.data[row, "emp_name"]
  salary  <- emp.data[row, "salary"]
  df_text[row] <- paste(" Employee ", name ,"has a total of ",salary," dollars")
}  

  df_text

  })

【讨论】:

    猜你喜欢
    • 2021-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-12
    • 1970-01-01
    • 2021-12-20
    • 2018-06-11
    相关资源
    最近更新 更多