【发布时间】:2018-07-24 16:58:55
【问题描述】:
我是闪亮的新手,我正在尝试构建一个应用程序,但我一直在这个问题上停留了一段时间。该应用程序的目的是让用户可以上传他们的数据,选择他们的自变量和因变量,选择他们的树数......等等,并最终通过随机森林脚本运行并显示输出。
但是,现在我坚持设置下拉输入,用户可以在其中选择他们的变量(来自他们上传的数据的标题)。它需要是响应式的,因此他们首先上传他们的数据,然后应用程序会自动知道在下拉菜单中放置什么,否则它将为 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")
)
)
)
)
服务器.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 后的样子:
【问题讨论】:
-
您可能正朝着随机森林的方向前进,但删除了该标签,因为您似乎还没有接近那个标签。您应该包含启动闪亮会话的代码。
-
@Sawyer Keels,答案满意吗?
-
是的!帮助很大。应用程序现在可以正常运行@skoh
-
@Sawyer Keeps,很高兴;你能把答案标记为正确吗?