如何在R Shiny服务器函数中正确设置一个id列?

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

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

如何在应用程序内修复这个问题?在下面找到完整的代码:

# 全局部分

# 初始化程序

# 在控制台中打印:全局脚本开始运行
print("global.R")

# 允许特定错误在屏幕上显示,而不是显示通用错误
options(shiny.sanitize.errors = FALSE)

# 加载所需的包
source('additional_scripts/packages.R')

# 加载LDA模型结果、主题名称和原始数据
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)

# 用户界面

# 告诉用户服务器脚本开始运行
print("ui.R")

ui <- fluidPage(
  theme = shinytheme("cerulean"),
  
  navbarPage("Analysis",
             
             #--- 主页标签(全局视图)
             tabPanel("Global View",
                      sidebarPanel(),
                      DT::dataTableOutput("mela1"),
                      DT::dataTableOutput("mela2")
                      ), #tabPanel - Global View  
             
             tabPanel("",
                      ) #tabPanel - 
             
  ) #navbarPage
) #fluidPage

# 服务器部分
# 告诉用户服务器脚本开始运行
print("server.R")

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

  dtd <<- NULL
  observe({                                                         # 将重要变量收集到一个数据集中
    dtd <<- as.data.frame(as.matrix(doc_top_dist))
    colnames(dtd) <<- colnames(top10_lamda0)                         # 设置主题名称
    highest <<- apply(dtd, 1, which.max)                             # 设置每个文档的最高概率主题
    
    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)                          
    topic <<- swap(highest, 1:length(topic_names), topic_names)     # 为每个文档添加名称
    dtd <<- cbind(topic, dtd)                                         # + 最大主题
    
    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")  # + 重要变量
    dtd$id <<- seq(1:nrow(dtd))                                      # + id列
    print(typeof(dtd$id))
    dtd <<- merge(vars, dtd, by = "id")
    dtd <<- as.data.frame(dtd)
    
    print(dtd$id)
  })
  
  # 用于检查的输出。稍后删除
  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)

我认为通过将其更改为数值型应该足够,但是...看来并不是正确的解决方案!

英文:

I am using the server function below to read initial working data with visualization purposes. When I run the id code in the console, it prints it out as it should (1,2,3...) but when i print it inside server, it prints this one out:

[1] &quot;integer&quot;
[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

How can I fix this inside the App? Find full code below:

#  Global 
# Initialize program 
# Print in console: global script is beginning to run
print(&quot;global.R&quot;)
# Allow specific errors to be displayed on screen, instead of displaying a generic error
options(shiny.sanitize.errors = FALSE)
# Load needed packages 
source(&#39;additional_scripts/packages.R&#39;)
# Load LDA model outcome, topic names &amp; raw data
model &lt;- readRDS(&quot;LDA_output.2020-01-03.rds&quot;)
lda_model    &lt;&lt;- model[[1]][1]
doc_top_dist &lt;&lt;- as.data.frame(as.matrix(model[[1]][2]));doc_top_dist &lt;- doc_top_dist[[1]][[1]]
top10_lamda0 &lt;&lt;- as.data.frame(as.matrix(model[[2]]))
full_data    &lt;&lt;- as.data.frame(as.matrix(model[[3]])); full_data$id &lt;- seq(1:nrow(full_data))
rm(model)
# User Interface 
# Tell user server script is beginning to run
print(&quot;ui.R&quot;)
ui &lt;- fluidPage( 
theme = shinytheme(&quot;cerulean&quot;), 
navbarPage(&quot;Analysis&quot;,
#--- Home Tab (Global View)
tabPanel(&quot;Global View&quot;,
sidebarPanel(),
DT::dataTableOutput(&quot;mela1&quot;),
DT::dataTableOutput(&quot;mela2&quot;)
), #tabPanel - Global View  
tabPanel(&quot;&quot;,
) #tabPanel - 
) #navbarPage
) #fluidPage
#Server 
# Tell user server script is beginning to run
print(&quot;server.R&quot;)
server &lt;- function(input, output, session) {
dtd &lt;&lt;- NULL
observe({                                                         #Gathering important variables in one only data set
dtd &lt;- as.data.frame(as.matrix(doc_top_dist))
colnames(dtd) &lt;- colnames(top10_lamda0)                         #set topic names
highest &lt;- apply(dtd, 1, which.max)                             #set highest prob topic per document
swap &lt;&lt;- function(vec, from, to) {                              #Swap from number to name 
tmp &lt;- to[ match(vec, from) ]
tmp[is.na(tmp)] &lt;- vec[is.na(tmp)]
return(tmp)
}
topic_names &lt;&lt;- colnames(top10_lamda0)                          
topic &lt;- swap(highest , 1:length(topic_names), topic_names)     #Add names to each doc
dtd &lt;- cbind(topic,dtd)                                         #+ max topic
vars &lt;- 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) &lt;- c(&quot;year&quot;, &quot;month&quot;, &quot;parent&quot;, &quot;id&quot;, &quot;cost&quot;, &quot;country&quot;)  #+ important variables  
dtd$id &lt;- seq(1:nrow(dtd))                                      #+ id column
print(typeof(dtd$id))
dtd &lt;- merge(vars, dtd,  by = &quot;id&quot;)
dtd &lt;&lt;- as.data.frame(dtd)
print(dtd$id)
})
#Outputs to check. DELETE LATER
output$mela2 &lt;- DT::renderDataTable({
DT::datatable(full_data, options = list(pageLength = 3, lengthMenu = c(3,30,60)))
})
output$mela1 &lt;- DT::renderDataTable({
DT::datatable(dtd, options = list(pageLength = 3, lengthMenu = c(3,30,60)))
})
}
shinyApp(ui, server)

Thought that by making it numeric would be enough, but it is... just not with the right track!

答案1

得分: 1

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

So try:

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

Why are you doing so much super-assigning though?

英文:

I've inserted comments to explain;

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

So try:

 dtd &lt;&lt;- as.data.frame(dtd, stringsAsFactors = F)
dtd$id &lt;- as.integer(dtd$id)

Why are you doing so much super-assigning though?

huangapple
  • 本文由 发表于 2020年1月4日 01:47:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/59583069.html
匿名

发表评论

匿名网友

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

确定