我有一个闪亮的应用程序,加载后包含6个csv文件(DB,TC,RH,DGI,DCH,LDP)。这6个文件正在处理中,并提供最终的数据帧finalDF()
,如下所示:
然后我加载第7个文件,我将其标为“新建”,处理后的结果是这样的:
我的问题是我希望能够使用textInput()
动态更改“新”名称,但不能使其正常工作。
下面,我提供了我的应用的实验版本(无效)。
#ui.r
# Load R libraries
library(shiny) # Create interactive Shiny visualizations
library(tidyverse) # Collection of packages for processing data
library(rowr) # Facilitates data manipulation
library(rlist) # Enables the append method
library(shinydashboard)# Creates the shinydashboard
library(ggplot2)
dashboardPage(
dashboardHeader(title = "Stacked Bar Plot to Compare Datasets",
#width of the title
titleWidth = 375),
dashboardSidebar(
#same width for the sidebar
width = 375,
# Allow the user to select the variable to compare
# Bold and bigger section title
h4(strong("Select the identifier & datasets to compare:")),
selectInput("comparisonSelector", h5("Select identifier:"), choices = c("None",
"Drug Name",
"ChEMBL ID",
"DrugBank ID",
"Drug SMILES",
"Target Symbol"),
#select Drug with selected =
selected = "Drug Name"),
uiOutput("checkbox"),
# Render the options for the Reference dataset
uiOutput("reference"),
h4(strong("Upload and process new dataset")),
# Input: Select a file ----
fileInput("file1", h5("Upload a file to compare"),
multiple = FALSE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")),
textInput("filename2",h5("Set Filename"),"Set Dataset Name")
),
dashboardBody(
fluidRow(
tableOutput("contents")
)
)
)
#server.r
function (input, output, session){
# Load in the datasets and return a dataframe with 5 columns, corresponding to the 5 variables of interest.
# Note that the columns extracted from each file are hard-coded values specific to each file's format.
DB <- reactive({
if("DB"%in% input$datasetSelector){
x <- read.csv("1_DB_Subset_v1.csv", stringsAsFactors = F)
}
})
TC <- reactive({
if("TC" %in% input$datasetSelector){
#x <- read.csv("2_TC_Subset_v1.csv", stringsAsFactors = F)
}
})
RH <- reactive({
if("RH" %in% input$datasetSelector){
x <- read.csv("3_RH_Subset_v1.csv", stringsAsFactors = F)
}
})
New <- reactive({
req(input$file1)
df <- read.csv(input$file1$datapath)
})
output$text2<-renderUI({
textInput("filename2",
"Specify name for uploaded dataset"
)
})
#Create a list with all the datasets which will be updated when we upload a new one
fileOptions <- reactiveValues(currentOptions=c("DB","TC","RH"))
#Use an observer to watch for file uploads
observeEvent(input$file1, {
fileOptions$currentOptions = list.append(fileOptions$currentOptions,"New")
})
# Output a UI drop-down box that allows a user to select the Reference database to compare all other datasets to.
# This set of options consists of the user-selected datasets. Select reference dataset with selected=
output$reference <- renderUI({
selectInput("referenceDataset", h5("Select the Reference Dataset"), choices = input$datasetSelector,selected = "DB")
})
# Here I set fileOptions as choices in both choices and in selected. Selected choices are used to create the plot when the app starts.
output$checkbox<-renderUI({
checkboxGroupInput("datasetSelector",h5("Specify the datasets to compare:"), choices = fileOptions$currentOptions,
selected = c("DB","TC","RH"))
})
# Check for which variable has been selected by the user to be analyzed, and output a value to signify this.
# The number it outputs is used as an index for the dataframes.
Variable <- reactive({
if(input$comparisonSelector == "Drug Name"){
1
}
else if(input$comparisonSelector == "ChEMBL ID"){
2
}
else if(input$comparisonSelector == "DrugBank ID"){
3
}
else if(input$comparisonSelector == "Drug SMILES"){
4
}
else if(input$comparisonSelector == "Target Symbol"){
5
}
else{
NULL
}
})
# Return a string for the name of the Reference dataset.
referenceSet <- reactive({
x <- input$referenceDataset
y <- eval(parse(text = paste0(x,"()")))
})
# Remove the name of the Reference dataset from the list of all datasets selected.
# e.g., if DrugBank is the Reference dataset, DrugBank is removed from the list of datasets loaded.
# This ensures that the Reference dataset is not compared to itself in the resulting plot.
comparisonSets <- reactive({
x <- input$datasetSelector[!input$datasetSelector %in% input$referenceDataset]
})
# The following 3 blocks (125 - 168) calculate the overlap between datasets and the unique values of each dataset.
# This code generates a logical vector of whether a value is in both compared datasets or not.
# Then it counts the number of True values and appends that value to a vector.
# This is looped for each dataset contained within the comparisonSets vector. All of the values are appended, and the vector is returned.
# When calculating the unique values, just subtract the overlap value from the number of values for each database.
OverlapVector <- reactive({
y <- as.vector(NULL)
for(i in 1:len(comparisonSets())){ # For each of the Comparison datasets specified by the user
# Convert the string name received from the checkbox input into a variable name
# Subset the dataframe to the specific column holding the user-selected variable
# Remove duplicates
# Compare the Comparison set vector with the Reference set vector using %in%. This checks each value in the referenceSet() against all of the values in the comparisonSets(), and returns a vector of the same length as the referenceSet().
x <- unique(referenceSet()[,Variable()]) %in% unique(eval(parse(text = paste0(comparisonSets()[i],"()")))[,Variable()])
x <- table(x) # Create a frequency table to report the # of True and False values present in vector x
x <- x[names(x) == TRUE] # Filter the vector for only the True values; returns the number of True values
y <- append(y,x) # Append the # of True values to the vector
}
y
})
UniqueToReferenceVector <- reactive({
y <- as.vector(NULL)
for(i in 1:len(comparisonSets())){ # For each of the Comparison datasets specified by the user
x <- unique(referenceSet()[,Variable()]) %in% unique(eval(parse(text = paste0(comparisonSets()[i],"()")))[,Variable()])
x <- table(x)
z <- len(unique(referenceSet()[,Variable()])) - (x[names(x) == TRUE])
y <- append(y,z)
}
y
})
UniqueToComparisonVector <- reactive({
y <- as.vector(NULL)
for(i in 1:len(comparisonSets())){ # For each of the Comparison datasets specified by the user
x <- unique(referenceSet()[,Variable()]) %in% unique(eval(parse(text = paste0(comparisonSets()[i],"()")))[,Variable()])
x <- table(x)
z <- len(unique(eval(parse(text = paste0(comparisonSets()[i],"()")))[,Variable()])) - (x[names(x) == TRUE])
y <- append(y,z)
}
y
})
# This section generates the dataframe structure that the plotting function will use.
# finalDF is an N x 4 dataframe in which each row is the name of the set being compared, and its 3 values (2 unique values and 1 overlap value).
**finalDF <- reactive({
x <- cbind.data.frame(UniqueToReferenceVector(),
OverlapVector(),
UniqueToComparisonVector(),
comparisonSets())
#The input$referenceDataset takes its value from the reference dataset selected every time by the user.
colnames(x) <- (c(paste("Unique to",input$referenceDataset),"Overlap","Unique to Comparison Dataset",'ComparisonDatabaseName'))
# Reorganize the data to prepare it for plotting
finalDF <- x %>%
gather(c(paste("Unique to",input$referenceDataset),"Overlap","Unique to Comparison Dataset"), key = 'Class', value = count) %>% # Transform the dataframe x into key-value pairs
select(ComparisonDatabaseName, Class, count) %>%
rename(Name = ComparisonDatabaseName) %>%
arrange(Name, Class) %>% # Rearrange the order of the Class column to match the following order: Class1, Overlap, Class 2
mutate(
Class = factor(Class, levels=c(paste("Unique to",input$referenceDataset),"Overlap","Unique to Comparison Dataset",'Name'), ordered = T),
Name = factor(Name, levels = (comparisonSets()), ordered = T))
finalDF$Name <- factor(finalDF$Name, levels = comparisonSets())
View(finalDF)
finalDF
})**
output$contents <- renderTable({
finalDF()
})
}