如何将被读取为列表的shiny输出对象转换为数据框?

huangapple go评论101阅读模式
英文:

How can I turn shiny output objects that are read as a list to data frame?

问题

  1. 我正在尝试在R Shiny应用程序之间进行一些交互。我从我的第一个应用程序中获得了第一个输出([1]列表、[2]数据框和[3]数据框)。我想在新应用程序中使用它来应用新的功能,最终将其用作我的最终仪表板。问题是,现在这些对象在全局环境中是非反应性的,它们都是列表类型,尽管我使用了as.data.frame函数(如下所示)。我不知道这是否可能是原因,但我觉得不对劲,它在我的服务器函数中找不到。有什么想法为什么会这样?

full_data工作正常,但dtd出现警告:Error in crosstalk::is.SharedData: 找不到对象'dtd'

英文:

I am trying to do some interplay between R shiny apps. I've got my first output ([1] list, [2] data.frame & [3] data.frame) from my first App. I want to use it for applying new funtions in a new app that will end up being my final dashboard. Thing is, now that objects are non reactive in the global enviroment, they are all type of list, even tho I use as.data.frame function (as you may see below). I don't know if that might be the reason, but I just find it wrong, and it isn't being found inside my server function. Any idea why?

  1. # --------------------------------------- Global --------------------------------------- #
  2. # Set working directory -> Ctrl+Shift+H & Open the app folder
  3. setwd("~/Programación en R/Shiny app/Client dashboard app")
  4. # --------------------- Initialize program --------------------- #
  5. # Print in console: global script is beginning to run
  6. print("global.R")
  7. # Allow specific errors to be displayed on screen, instead of displaying a generic error
  8. options(shiny.sanitize.errors = FALSE)
  9. # Load needed packages
  10. source('additional_scripts/packages.R')
  11. # Load LDA model outcome, topic names & raw data
  12. model <- readRDS("LDA_output.2020-01-02.rds")
  13. as.data.frame(model[[1]][3])
  14. lda_model <<- model[[1]][1]
  15. dtm <<- as.data.frame(as.matrix(model[[1]][2]))
  16. doc_top_dist <<- as.data.frame(as.matrix(model[[1]][3]));doc_top_dist <- doc_top_dist[[1]][[1]]
  17. top10_lamda0 <<- as.data.frame(as.matrix(model[[2]]))
  18. full_data <<- as.data.frame(as.matrix(model[[3]]))
  19. rm(model)
  20. #--------------------------------------- User Interface ---------------------------------------#
  21. ui <- fluidPage(
  22. theme = shinytheme("cerulean"),
  23. navbarPage("Analysis",
  24. #--- Home Tab (Global View)
  25. tabPanel("Global View",
  26. sidebarPanel(),
  27. DT::dataTableOutput("mela1"),
  28. DT::dataTableOutput("mela2")
  29. ), #tabPanel - Global View
  30. tabPanel("",
  31. ) #tabPanel -
  32. ) #navbarPage
  33. ) #fluidPage
  34. #--------------------------------------- Server ---------------------------------------#
  35. server <- function(input, output, session) {
  36. reactive({ #Gathering important variables in one only data set
  37. dtd <- doc_top_dist
  38. print(dtd)
  39. highest <- apply(dtd, 1, which.max) #set highest prob topic
  40. print(highest)
  41. swap <<- function(vec, from, to) {
  42. tmp <- to[ match(vec, from) ]
  43. tmp[is.na(tmp)] <- vec[is.na(tmp)]
  44. return(tmp)
  45. }
  46. topic_names <<- colnames(top10_lamda0)
  47. print(topic_names)
  48. topic <- swap(highest , 1:length(topic_names), topic_names)
  49. dtd <- cbind(topic,dtd) #+ max topic
  50. dtd$id <- as.character(1:nrow(dtd)) #+ id column
  51. vars <- cbind(data$year, data$Ultimate.Parent, data$id, data$IP.Cost, data$Publication.Country)
  52. colnames(vars) <- c("year", "parent", "id", "cost", "country") #+ important variables
  53. print(colnames)
  54. dtd <- merge(vars, dtd, by = "id")
  55. dtd <<- as.data.frame(dtd)
  56. })
  57. output$mela2 <- DT::renderDataTable({
  58. DT::datatable(full_data, options = list(pageLength = 3, lengthMenu = c(3,30,60)))
  59. })
  60. output$mela1 <- DT::renderDataTable({
  61. DT::datatable(dtd, options = list(pageLength = 3, lengthMenu = c(3,30,60)))
  62. })
  63. }
  64. shinyApp(ui, server)

full_data is working alright but dtd is giving Warning: Error in crosstalk::is.SharedData: object 'dtd' not found
[No stack trace available]

答案1

得分: 1

dtd只在reactive表达式内部有效,因此对于renderDataTable不可见。在reactive之前添加dtd <- NULL以在server函数的范围内创建一个dtd变量。

英文:

dtd only has scope within the reactive expression so it is not visible to renderDataTable. Add dtd <- NULL before the reactive to create a dtd variable in the scope of the server function.

huangapple
  • 本文由 发表于 2020年1月3日 22:59:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580768.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定