Search code examples
rshinyreactive-programmingreactivetypeof

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


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]


Solution

  • 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.