gpt4 book ai didi

file - Shiny 地从上传的数据框中选择特定的列

转载 作者:行者123 更新时间:2023-12-02 00:52:11 37 4
gpt4 key购买 nike

我合并了不同的代码源来制作一个允许上传文件(数据框)的应用程序。

但是,除此之外,我还希望能够从数据框中选择特定的列并对其进行分析。然而,这很困难,因为必须预定义给定的数据框才能在 ui.R 脚本中引用它....因此,当将以前未定义的数据框上传到站点时,不能像在服务器中定义的那样在 ui.R 中尊重它....

预定义变量

vchoices <- 1:ncol(mtcars)
names(vchoices) <- names(mtcars)

ui.R

    runApp(
ui = basicPage(
h2('The uploaded file data'),
dataTableOutput('mytable'),
fileInput('file', 'Choose info-file to upload',
accept = c(
'text/csv',
'text/comma-separated-values',
'text/tab-separated-values',
'text/plain',
'.csv',
'.tsv'
)
),
actionButton("choice", "incorporate external information"),

selectInput("columns", "Select Columns", choices=vchoices, inline = T),
#notice that the 'choices' in selectInput are set to the predefined
#variables above whereas I would like to set them equal to the
#not yet defined uploaded file below in server.R

tableOutput("table_display")
))

请注意,selectInput 中的“选择”设置为上面的预定义变量,而我想将它们设置为等于下面 server.R 中尚未定义的上传文件

服务器.R

  server = function(input, output) {

info <- eventReactive(input$choice, {
inFile <- input$file
if (is.null(inFile))
return(NULL)
isolate(f<-read.table(inFile$datapath, header = T,
sep = "\t"))
f
})
output$table_display<-renderTable({
f<-info()
f<-subset(f, select=input$columns) #subsetting takes place here
head(f)
})
}

有谁知道一种方法来引用在服务器、用户界面中定义的变量,从而允许交互式操作?

最佳答案

您可以使用一系列函数 update*Input - 在这种情况下 updateSelectInput .它的第一个参数必须是 session你还必须添加 sessionserver <- function(input, output)能够更新您的小部件。

您可以在单击 actionButton 后立即更新小部件- 所以,你必须使用 updateSelectInputeventReactive 内.


让我们来看看如何做到这一点:

首先,您可以将新上传的数据集的列名保存在一个变量中,比如 vars然后将其传递给函数 updateSelectInput .(selectInput 的选项最初设置为 NULL - 我们之前不需要指定它们,因为它们无论如何都会被更新)

info <- eventReactive(input$choice, {
inFile <- input$file
# Instead # if (is.null(inFile)) ... use "req"
req(inFile)

# Changes in read.table
f <- read.table(inFile$datapath, header = input$header, sep = input$sep, quote = input$quote)
vars <- names(f)
# Update select input immediately after clicking on the action button.
updateSelectInput(session, "columns","Select Columns", choices = vars)

f
})

我添加了一个小的 upload interface到你的代码。

另一种方法是在服务器端定义小部件,然后通过 renderUI 将它们传递给客户端。功能。你可以找到here一个例子。


完整示例:

library(shiny)

ui <- fluidPage(
h2('The uploaded file data'),
dataTableOutput('mytable'),
fileInput('file', 'Choose info-file to upload',
accept = c(
'text/csv',
'text/comma-separated-values',
'text/tab-separated-values',
'text/plain',
'.csv',
'.tsv'
)
),
# Taken from: http://shiny.rstudio.com/gallery/file-upload.html
tags$hr(),
checkboxInput('header', 'Header', TRUE),
radioButtons('sep', 'Separator',
c(Comma=',',
Semicolon=';',
Tab='\t'),
','),
radioButtons('quote', 'Quote',
c(None='',
'Double Quote'='"',
'Single Quote'="'"),
'"'),
################################################################

actionButton("choice", "incorporate external information"),

selectInput("columns", "Select Columns", choices = NULL), # no choices before uploading

tableOutput("table_display")
)

server <- function(input, output, session) { # added session for updateSelectInput

info <- eventReactive(input$choice, {
inFile <- input$file
# Instead # if (is.null(inFile)) ... use "req"
req(inFile)

# Changes in read.table
f <- read.table(inFile$datapath, header = input$header, sep = input$sep, quote = input$quote)
vars <- names(f)
# Update select input immediately after clicking on the action button.
updateSelectInput(session, "columns","Select Columns", choices = vars)

f
})

output$table_display <- renderTable({
f <- info()
f <- subset(f, select = input$columns) #subsetting takes place here
head(f)
})
}
shinyApp(ui, server)

关于file - Shiny 地从上传的数据框中选择特定的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38852916/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com