我想构建一个以表格格式显示.csv文件的应用程序。用户可以使用单选按钮选择以下两种方式之一来显示表格。我已经用filedata()
和data_ranked_words()
反应定义了这两种方法。两个Reactives错误一元运算符无效的参数
要重现此错误,请先运行此代码块,让我的数据的一小部分:
test = rbind(
c(0.00000009, 0.00000009, 0.00046605, 0.00015541, 0.00215630),
c(0.00000016, 0.00137076, 0.00000016, 0.00000016, 0.00000016),
c(0.00012633, 0.00000014, 0.00000014, 0.00000014, 0.00075729),
c(0.00000013, 0.00000013, 0.00000013, 0.00000013, 0.00062728)
)
colnames(test) = c('church', 'appearance', 'restrain', 'parity', 'favor')
rownames(test) = NULL
test = as.data.frame(test)
write.csv(test, 'test.csv', row.names = FALSE)
你会看到,你只要程序启动得到Error invalid argument to binary operator
。然后在您的工作目录中选择test.csv
off您的文件系统,您将看到在选择“Word视图”时错误仍然存在,但在选中“概率视图”时表格正确显示。
这个程序很简单。问题发生在第66行temp = matrix(row.names(data)[apply(-data, 2, order)], nrow(data))
。它不喜欢apply
中的-data
。然而,尽可能地尝试,我一直没有能够重现这个错误,只是在R控制台工作,闪亮之外。在常规R中,这条线运行得很好。
我想要做的是当用户选择单选按钮时显示两个不同的表。 '概率视图'是原始表,“字视图”是其上有一些操作的表(第61-71行)。我无法弄清楚这一点!
这里是我的应用程序:
library(shiny)
library(markdown)
library(DT)
library(D3TableFilter)
options(shiny.maxRequestSize=50*1024^2)
# ui.R
#-------------------------------------------------------------------------------------
ui <- shinyUI(
navbarPage("Topic Model App v1.0",
tabPanel("From CSV",
sidebarLayout(
sidebarPanel(
# Define what's in the sidebar
fileInput("file",
"Choose CSV files from directory",
multiple = TRUE,
accept=c('text/csv',
'text/comma-separated-values,text/plain',
'.csv')),
radioButtons('toggle', 'Choose one:',
list('Word View', 'Probability View'))
),
# Define what's in the main panel
mainPanel(
title = 'Topic Model Viewer',
# How wide the main table will be
fluidRow(
column(width = 12, d3tfOutput('data'))
)
)
)
)
)
)
# server.R
#-------------------------------------------------------------------------------------
server <- shinyServer(function(input, output, session) {
# Set up the dataframe for display in the table
# Define 'filedata()' as the .csv file that is uploaded
filedata <- reactive({
infile <- input$file
if (is.null(infile)) {
# User has not uploaded a file yet
return(NULL)
}
# Read in .csv file and clean up
data = read.csv(infile$datapath)
data = t(data)
data = as.data.frame(data)
colnames(data) = paste0(rep('topic', ncol(data)), 1:ncol(data))
data = format(data, scientific = FALSE)
data
})
#PROBLEM
# The ranked and ordered csv file
data_ranked_words <- reactive({
# Sort each column by probability, and substitute the correct word into that column
# This will essentially rank each word for each topic
# This is done by indexing the row names by the order of each column
data = filedata()
temp = matrix(row.names(data)[apply(-data, 2, order)], nrow(data))
temp = as.data.frame(temp)
colnames(temp) = paste0(rep('topic', ncol(data)), 1:ncol(data))
temp
})
# Create table
output$data <- renderD3tf({
tableProps <- list(
rows_counter = TRUE,
rows_counter_text = "Rows: ",
alternate_rows = TRUE
);
# Radio buttons
# The reason why the extensions are in this if() is so that sorting can be
# activated on Probability View, but not Word View
if(input$toggle=='Word View'){
df = data_ranked_words()
extensions <- list(
list(name = "colsVisibility",
text = 'Hide columns: ',
enable_tick_all = TRUE
),
list(name = "filtersVisibility",
visible_at_start = FALSE)
)
} else if(input$toggle=='Probability View'){
df = filedata()
extensions <- list(
list(name = "sort"), #this enables/disables sorting
list(name = "colsVisibility",
text = 'Hide columns: ',
enable_tick_all = TRUE
),
list(name = "filtersVisibility",
visible_at_start = FALSE)
)
}
if(is.null(filedata())){
} else{
d3tf(df,
tableProps = tableProps,
extensions = extensions,
showRowNames = TRUE,
tableStyle = "table table-bordered")
}
})
# This line will end the R session when the Shiny app is closed
session$onSessionEnded(stopApp)
})
# Run app in browser
runApp(list(ui=ui,server=server), launch.browser = TRUE)
就是这样。非常感谢您的帮助。问题是我不知道'req(input $ file)'。我想知道阻止反应被触发的方式是什么,就是这样!对我未来的闪亮编程非常有帮助。对科学记数法很奇怪,即使我使用'options()'关闭它,我也遇到了问题,所以我只是将它评论出来。但这个解决方案是理想的! – tsouchlarakis