我在下面有一个闪亮的仪表板,用户可以使用两个csv文件DB和TC(实际上它们是6,但我只将2用于Q),还可以上传自己的csv。此新的csv在上载后被命名为“新建”,并在selectInput()
“选择参考数据集”中作为选项自动给出,并且在复选框组中作为选项可见。但是然后,我希望能够使用textInput()
“设置文件名”对其进行重命名,以便将其用于进一步处理。因此,我试图做类似input$filename2<-reactive()
的事情,这似乎是错误的。
#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)
dashboardPage(
dashboardHeader(title = "Stacked Bar Plot to Compare Datasets",
titleWidth = 375),
dashboardSidebar(
#tags$head(tags$style(HTML('.info-box {min-height: 45px;} .info-box-icon {height: 45px; line-height: 45px;} .info-box-content {padding-top: 0px; padding-bottom: 0px;}'))),
width = 375,
uiOutput("checkbox"),
# Render the options for the Reference dataset
uiOutput("reference"),
tags$hr(),
# Bold and bigger section title
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")),
#div(style="display: inline-block;vertical-align:top;width: 820px;",valueBox(h5(strong("Format of uploaded file")),"•Column-formatted CSV file", icon = icon("credit-card"))),
# Set Name textinput
textInput("filename2",h5("Set Filename"),"Set Dataset Name")
),
dashboardBody(
)
)
#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.
#use backquites to separate letters for example `D B`
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)
}
})
New <- reactive({
#input$file1 will be NULL initially. After the user selects
#and uploads a file, head of that data file by default,
#or all rows if selected, will be shown.
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"))
#Use an observer to watch for file uploads
observeEvent(input$file1, {
fileOptions$currentOptions = list.append(fileOptions$currentOptions,"New")
})
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.
# The input$goButton here means that this set of options is triggered only when I press the action button
output$checkbox<-renderUI({
checkboxGroupInput("datasetSelector",h5("Specify the datasets to compare:"), choices = fileOptions$currentOptions,
selected = c("DB","TC"))
})
}