如何在 R 闪亮服务器函数中正确设置 id 列?

How do I properly set an id column inside a R shiny server function?

我正在使用下面的服务器功能来读取具有可视化目的的初始工作数据。当我 运行 控制台中的 id 代码时,它会按原样打印出来 (1,2,3...) 但是当我在服务器内部打印它时,它会打印出这个:

[1] "integer"
[1] 630 630
  [1] 1   10  100 101 102 103 104 105 106 107 108 109 11  110 111 112 113 114 115 116 117 118 119 12  120 121 122 123 124 125 126 127
 [33] 128 129 13  130 131 132 133 134 135 136 137 138 139 14  140 141 142 143 144 145 146 147 148 149 15  150 151 152 153 154 155 156
 [65] 157 158 159 16  160 161 162 163 164 165 166 167 168 169 17  170 171 172 173 174 175 176 177 178 179 18  180 181 182 183 184 185
 [97] 186 187 188 189 19  190 191 192 193 194 195 196 197 198 199 2   20  200 201 202 203 204 205 206 207 208 209 21  210 211 212 213
[129] 214 215 216 217 218 219 22  220 221 222 223 224 225 226 227 228 229 23  230 231 232 233 234 235 236 237 238 239 24  240 241 242
[161] 243 244 245 246 247 248 249 25  250 251 252 253 254 255 256 257 258 259 26  260 261 262 263 264 265 266 267 268 269 27  270 271
630 Levels: 1 10 100 101 102 103 104 105 106 107 108 109 11 110 111 112 113 114 115 116 117 118 119 12 120 121 122 123 124 ... 99

如何在应用程序中解决这个问题?在下面找到完整的代码:

#  Global 

# 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-03.rds")
lda_model    <<- model[[1]][1]
doc_top_dist <<- as.data.frame(as.matrix(model[[1]][2]));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]])); full_data$id <- seq(1:nrow(full_data))
rm(model)

# User Interface 

# Tell user server script is beginning to run
print("ui.R")

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 
# Tell user server script is beginning to run
print("server.R")

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

  dtd <<- NULL
  observe({                                                         #Gathering important variables in one only data set
    dtd <- as.data.frame(as.matrix(doc_top_dist))
    colnames(dtd) <- colnames(top10_lamda0)                         #set topic names
    highest <- apply(dtd, 1, which.max)                             #set highest prob topic per document

    swap <<- function(vec, from, to) {                              #Swap from number to name 
      tmp <- to[ match(vec, from) ]
      tmp[is.na(tmp)] <- vec[is.na(tmp)]
      return(tmp)
    }

    topic_names <<- colnames(top10_lamda0)                          
    topic <- swap(highest , 1:length(topic_names), topic_names)     #Add names to each doc
    dtd <- cbind(topic,dtd)                                         #+ max topic


    vars <- cbind(as.character(full_data$year), as.character(full_data$month), as.character(full_data$Ultimate.Parent), as.character(full_data$id), 
                  as.character(full_data$IP.Cost), as.character(full_data$Publication.Country))
    colnames(vars) <- c("year", "month", "parent", "id", "cost", "country")  #+ important variables  
    dtd$id <- seq(1:nrow(dtd))                                      #+ id column
    print(typeof(dtd$id))
    dtd <- merge(vars, dtd,  by = "id")
    dtd <<- as.data.frame(dtd)

    print(dtd$id)
  })

  #Outputs to check. DELETE LATER
  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)

以为将其设为数字​​就足够了,但...只是不正确!

我已经插入评论来解释;

dtd$id <- seq(1:nrow(dtd))  # dtd$id is type integer
print(typeof(dtd$id))
dtd <- merge(vars, dtd,  by = "id") # dtd is a character matrix
dtd <<- as.data.frame(dtd) # dtd is now a data.frame of factors, since stringsAsFactors is TRUE

所以尝试:

 dtd <<- as.data.frame(dtd, stringsAsFactors = F)
 dtd$id <- as.integer(dtd$id)

你为什么要做这么多超级分配?