我对Shiny并不陌生,我正在尝试构建一个应用程序,但是一段时间以来我一直在这个问题上停留。该应用程序的目的是使用户可以上传数据,选择其独立变量和因变量,选择树的数量等,并最终通过随机森林脚本运行并显示输出。
但是,现在我仍然停留在设置下拉输入的位置,用户可以在其中选择变量(上传数据中的标题)。它需要是反应性的,因此他们首先上传数据,然后应用程序会自动知道要在下拉菜单中放置什么内容,否则它将为NULL。这是我的ui.R和server.R文件的副本。如果您知道可能出了什么问题,将非常感谢您的帮助。另外,还要感谢上周为我提供帮助的人们。我没有上传实际的R代码(仅是图片),因此对他们来说更具挑战性。
ui.R
library(shiny)
shinyUI(fluidPage(
headerPanel(title = "Upload File"),
sidebarLayout(
sidebarPanel(
fileInput("file","Upload the file"),
h5("Max file size is 5 MB"),
tags$hr(),
radioButtons("sep","Seperator", choices = c(Comma = ",", Period = ".", Tilde = "~",minus = "-")),
tags$hr(),
checkboxInput("header","Header", TRUE),
tags$hr(),
uiOutput("vx"),
tags$hr(),
uiOutput("vy"),
tags$hr(),
numericInput("MTRY", "Set the MTRY", 0, min = 0, max = 500, step = 1,
width = 100),
helpText("The MTRY should default to 0"),
numericInput("numtree", "Number of Trees", 500, min = 30, max = 10000, step = 100,
width = 100)
),
mainPanel(
tableOutput("input_file")
)
)
)
)
Server.R
library(shiny)
shinyServer(function(input, output) {
output$input_file <- renderTable({
file_to_read = input$file
if(is.null(file_to_read)){
return()
}
dat1 <- read.table(file_to_read$datapath, sep = input$sep, header = input$header)
return(dat1)
})
reactive1 = reactive({
if(is.null(dat1))
return()
D <- colnames(dat1)
reactive1[[1]] = D
reactive1[[2]] = D[1]
reactive1
})
output$vx <- renderUI({
selectInput("cols", "Select Dependent Variable",
choices = colnames(reactive1()[[1]]), selected = reactive1()[[2]][1])
})
output$vy <- renderUI({
selectInput("cols", "Select Independent Variables",
choices = colnames(reactive1()[[1]]), selected = reactive1()[[2]][1], multiple = T)
})
})
以下是上传csv后应用程序的外观:
答案 0 :(得分:0)
关键是使输入数据具有反应性,并根据反应性数据帧更新updateSelectInput。见下文:
ui <- fluidPage(
titlePanel("Uploading Files"),
sidebarLayout(
sidebarPanel(
fileInput("file1", "Choose CSV File",
multiple = FALSE,
accept = c("text/csv",
"text/comma-separated-values,text/plain",
".csv")),
tags$hr(),
checkboxInput("header", "Header", TRUE),
radioButtons("sep", "Separator",
choices = c(Comma = ",",
Semicolon = ";",
Tab = "\t"),
selected = ","),
tags$hr(),
selectInput("vars", "Label",choices = c("NULL"))
),
mainPanel(
tableOutput("contents")
)
)
)
server <- function(input, output, session) {
df1 <- reactive({
req(input$file1)
read.csv(input$file1$datapath,
header = input$header,
sep = input$sep,
quote = input$quote)
})
observeEvent(df1(), {
updateSelectInput(session, "vars", choices = names(df1()))
})
}
shinyApp(ui, server)
如果这能回答您的问题,请发表评论。