我想構建一個以表格格式顯示.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