英文:
How can I turn shiny output objects that are read as a list to data frame?
问题
我正在尝试在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?
# --------------------------------------- Global --------------------------------------- #
# Set working directory -> Ctrl+Shift+H & Open the app folder
setwd("~/Programación en R/Shiny app/Client dashboard app")
# --------------------- Initialize program --------------------- #
# Print in console: global script is beginning to run
print("global.R")
# Allow specific errors to be displayed on screen, instead of displaying a generic error
options(shiny.sanitize.errors = FALSE)
# Load needed packages
source('additional_scripts/packages.R')
# Load LDA model outcome, topic names & raw data
model <- readRDS("LDA_output.2020-01-02.rds")
as.data.frame(model[[1]][3])
lda_model <<- model[[1]][1]
dtm <<- as.data.frame(as.matrix(model[[1]][2]))
doc_top_dist <<- as.data.frame(as.matrix(model[[1]][3]));doc_top_dist <- doc_top_dist[[1]][[1]]
top10_lamda0 <<- as.data.frame(as.matrix(model[[2]]))
full_data <<- as.data.frame(as.matrix(model[[3]]))
rm(model)
#--------------------------------------- User Interface ---------------------------------------#
ui <- fluidPage(
theme = shinytheme("cerulean"),
navbarPage("Analysis",
#--- Home Tab (Global View)
tabPanel("Global View",
sidebarPanel(),
DT::dataTableOutput("mela1"),
DT::dataTableOutput("mela2")
), #tabPanel - Global View
tabPanel("",
) #tabPanel -
) #navbarPage
) #fluidPage
#--------------------------------------- Server ---------------------------------------#
server <- function(input, output, session) {
reactive({ #Gathering important variables in one only data set
dtd <- doc_top_dist
print(dtd)
highest <- apply(dtd, 1, which.max) #set highest prob topic
print(highest)
swap <<- function(vec, from, to) {
tmp <- to[ match(vec, from) ]
tmp[is.na(tmp)] <- vec[is.na(tmp)]
return(tmp)
}
topic_names <<- colnames(top10_lamda0)
print(topic_names)
topic <- swap(highest , 1:length(topic_names), topic_names)
dtd <- cbind(topic,dtd) #+ max topic
dtd$id <- as.character(1:nrow(dtd)) #+ id column
vars <- cbind(data$year, data$Ultimate.Parent, data$id, data$IP.Cost, data$Publication.Country)
colnames(vars) <- c("year", "parent", "id", "cost", "country") #+ important variables
print(colnames)
dtd <- merge(vars, dtd, by = "id")
dtd <<- as.data.frame(dtd)
})
output$mela2 <- DT::renderDataTable({
DT::datatable(full_data, options = list(pageLength = 3, lengthMenu = c(3,30,60)))
})
output$mela1 <- DT::renderDataTable({
DT::datatable(dtd, options = list(pageLength = 3, lengthMenu = c(3,30,60)))
})
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论