R:使用csv中的数据作为闪亮ui函数中的参数
这涉及到闪亮,并且是相当概念化的,因此没有可复制的例子,因为我甚至不知道从哪里开始 问题:是否可以使用某些csv文件列中给定的值作为某些ui函数的参数,例如R:使用csv中的数据作为闪亮ui函数中的参数,r,shiny,R,Shiny,这涉及到闪亮,并且是相当概念化的,因此没有可复制的例子,因为我甚至不知道从哪里开始 问题:是否可以使用某些csv文件列中给定的值作为某些ui函数的参数,例如numericInput()?如果是,我会怎么做 概念示例:假设我有一个名为foo的csv文件,其中一列名为x,其中一行的值为5。是否可以将此csv文件用作函数的参数,如numericInput()。可能是这样的: # read the data (not sure where to do that. In the server file?
numericInput()
?如果是,我会怎么做
概念示例:假设我有一个名为foo
的csv文件,其中一列名为x
,其中一行的值为5
。是否可以将此csv文件用作函数的参数,如numericInput()
。可能是这样的:
# read the data (not sure where to do that. In the server file? as a reactive?
mydata <- read.csv("foo.csv")
# Use data as argument
numericInput("some id", label = NULL, value = mydata$x)
#读取数据(不确定在何处执行此操作。在服务器文件中?作为响应?
mydata我认为,fileInput()
(ui函数)和observe({update\uu Input()})
(服务器函数;例如,updateNumericInput()
)的组合可以根据csv文件给出的单个值更新某些ui函数的参数。但据我所知,除了少数例外(例如,selectInput()的参数,选项),多个长度向量不能是大多数参数。因此我使用selectInput()
将列中的所有值用作参数。
下面是我的示例app.R。每次输入csv文件时,一些ui函数的参数都会根据列中给定的值更新(selectInput(“row.value”)
)的选择;numeriInput(“col”)
和sliderInput(“row”)
的最大值、sliderInput(“range”)
的最小值、最大值和值取决于单个值(例如,nrow(data)
,mean(a column)
)。每次更改“col”、“row.value”和“range”都会更新。我认为这段代码还有很大的改进空间
应用程序R
库(闪亮)
用户界面
library(shiny)
ui <- shinyUI(fluidPage(
sidebarLayout(
sidebarPanel(
fileInput("file", label="Input File:"),
br(),
numericInput("col", "col :", min = 1, max = 2, value = 1, step = 1),
sliderInput("row", "row :", min = 1, max = 2, value = 1, step = 1),
selectInput("row.value", "row.value :", choices = 1, selectize = F, size = 10),
br(),
sliderInput("range", "range :", min = 1, max = 2, value = 1)
),
mainPanel(
br(),
h5("Result :"),
verbatimTextOutput("result1"),
br(),
h5("Data Table :"),
dataTableOutput("table1")
)
)
))
server <- shinyServer(function(input, output, session) {
observe({
file <- input$file
if(is.null(file)) { } else {
data <- read.csv(file$datapath)
col.data <- as.numeric(data[,input$col])
updateNumericInput(session, "col", "col :", min = 1, max = ncol(data), value = NULL, step = 1)
updateSliderInput(session, "row", "row :", min = 1, max = nrow(data), value = NULL, step = 1)
updateSelectInput(session, "row.value", "row.value :", choices = sort(unique(col.data)))
updateSliderInput(session, "range", "range & mean :", min = min(col.data),
max = max(col.data), value = mean(col.data))
}
})
output$result1 <- renderText({
file <- input$file
if(is.null(file)) { } else {
data <- read.csv(file$datapath)
c( paste0(iconv(file$name), "["), input$row, ",",input$col, "] is", data[input$row, input$col],
"; Index of", paste0(iconv(file$name), "[value =="), input$row.value, ",", input$col, "] is",
which(as.numeric(data[,input$col]) == input$row.value))
}
})
output$table1 <- renderDataTable({
file <- input$file
if(is.null(file)) { } else {
read.csv(file$datapath)
}
})
})
shinyApp(ui = ui, server = server)